0

MKMapView に三角形のポリゴンがあり、これをすばやく再描画したり、電話の特定の位置に固定したりしたいと考えています。

このポリゴンは常に電話の上部を指しますが、マップ自体は提供された見出しに回転します。現在、私が思いついた唯一の方法は、オーバーレイを削除してから再度追加することです。これに関する問題は、非常に途切れ途切れであることです。

私が思いついた次のアイデアは、地図の上に三角形の画像を追加し、必要に応じて地図のサイズを変更することでした。この三角形の画像は、地図のオーバーレイとして追加されたときに多角形がどうなるかを正確に視覚化するものです。これはほとんど機能しますが、あまり正確ではありません。なんらかの理由で、緯度ズーム レベルの変更やその他の問題にほとんど反応しません。

これに対する私の方法は次のとおりです。

- (void)setMapVisibleRect {
    //we have to mock the heading to 0 so that the distances show up in lat/long correctly
    //create a new instance of the cone model with a fake heading of 0 for calculations
    ConePolygonModel *conePolygonModel = [[ConePolygonModel alloc] init:[self.userLocationModel getLocation].coordinate
                                                            withHeading:0
                                                  withLatLongMultiplier:[self.conePolygonModel getLatLongMultiplier]
                                                 withDistanceMultiplier:[self.conePolygonModel getDistanceMultiplier]];
    //reset the map heading to 0
    [self.mapView.camera setHeading:0];

    //top left setup
    CLLocationCoordinate2D topLeftCoordinate;
    topLeftCoordinate.latitude = [[[conePolygonModel getXCoordinates] objectAtIndex:1] doubleValue];
    topLeftCoordinate.longitude = [[[conePolygonModel getYCoordinates] objectAtIndex:1] doubleValue];

    CLLocation *topLeftLocation = [[CLLocation alloc] initWithLatitude:topLeftCoordinate.latitude longitude:topLeftCoordinate.longitude];

    //top right setup
    CLLocationCoordinate2D topRightCoordinate;
    topRightCoordinate.latitude = [[[conePolygonModel getXCoordinates] objectAtIndex:2] doubleValue];
    topRightCoordinate.longitude = [[[conePolygonModel getYCoordinates] objectAtIndex:2] doubleValue];

    CLLocation *topRightLocation = [[CLLocation alloc] initWithLatitude:topRightCoordinate.latitude longitude:topRightCoordinate.longitude];

    //center setup
    CLLocationCoordinate2D topCenterCoordinate = [conePolygonModel midpointBetweenCoordinate:topLeftCoordinate andCoordinate:topRightCoordinate];

    CLLocation *topCenterLocation = [[CLLocation alloc] initWithLatitude:topCenterCoordinate.latitude longitude:topCenterCoordinate.longitude];

    //set distances
    CLLocationDistance latitudeDistance = [topLeftLocation distanceFromLocation:topRightLocation];
    CLLocationDistance longitudeDistance = [topCenterLocation distanceFromLocation:[self.userLocationModel getLocation]];

    //set the map zoom
    NSLog(@"zoom levels: %f %f", latitudeDistance, longitudeDistance);
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance([self.userLocationModel getLocation].coordinate, latitudeDistance, longitudeDistance);
    [self.mapView setRegion:viewRegion];

    //move the map to the actual heading
    [self.mapView.camera setHeading:self.currentHeading];
}

マップの回転に関係なく、オーバーレイを常に画面の上部に向ける方法はありますか? オーバーレイが画面上で常に同じサイズになるようにマップをズームすることも良いですが、他のポイントほど重要ではありません。

4

1 に答える 1

0

三角形を常に同じサイズと向きにする必要があるため、これをオーバーレイではなく MKAnnotationView として実装してみてください。

これを行う最も簡単な方法は、PNG ファイルを作成し、その UIImage をデリゲート メソッドの のプロパティに割り当てることですimage。または、Core Graphics を使用してプログラムで三角形を描画し、実行時に UIImage に変換することもできます。Core Graphics を使用して一度画像をレンダリングし、UIImage オブジェクトへの参照を保持できるため、viewForAnnotation が呼び出されるたびに再描画する必要はありません。MKAnnotationViewmapView:viewForAnnotation:

于 2014-02-06T22:00:52.227 に答える