在MapKit中沿着圆弧创build视觉元素

我如何添加和animation一个视觉元素,沿着一个弧,我已经在mapkit内创build

下面的代码将在两点之间创build一个不错的弧线。 想象一下,一个animation的视觉,将代表沿着这条弧线飞行的飞机。

-(void)addArc { CLLocationCoordinate2D sanFrancisco = { 37.774929, -122.419416 }; CLLocationCoordinate2D newYork = { 40.714353, -74.005973 }; CLLocationCoordinate2D pointsArc[] = { sanFrancisco, newYork }; // MKGeodesicPolyline *geodesic; geodesic = [MKGeodesicPolyline polylineWithCoordinates:&pointsArc[0] count:2]; // [self.mapView addOverlay:geodesic]; } 

在这里输入图像说明

实际上,注释可能是最好的select。 使用可指定的坐标属性定义注记类(或使用MKPointAnnotation )。

令人惊讶的是, MKGeodesicPolyline类足以提供通过points属性(给出MKMapPoint )或getCoordinates:range:方法(给出CLLocationCoordinate2D )创build圆弧的单个点。

(实际上,该属性和方法位于MKMultiPoint类的MKMultiPoint类中, MKPolylineMKGeodesicPolyline的一个子类。)

只需更新计时器上的注释coordinate属性,地图视图就会自动移动注释。

注意:对于这么长的弧线,会有数千个点。

这里有一个非常简单的例子,它使用了points属性(比getCoordinates:range:方法更容易使用)和performSelector:withObject:afterDelay: ::

 //declare these ivars: MKGeodesicPolyline *geodesic; MKPointAnnotation *thePlane; int planePositionIndex; //after you add the geodesic overlay, initialize the plane: thePlane = [[MKPointAnnotation alloc] init]; thePlane.coordinate = sanFrancisco; thePlane.title = @"Plane"; [mapView addAnnotation:thePlane]; planePositionIndex = 0; [self performSelector:@selector(updatePlanePosition) withObject:nil afterDelay:0.5]; -(void)updatePlanePosition { //this example updates the position in increments of 50... planePositionIndex = planePositionIndex + 50; if (planePositionIndex >= geodesic.pointCount) { //plane has reached end, stop moving return; } MKMapPoint nextMapPoint = geodesic.points[planePositionIndex]; //convert MKMapPoint to CLLocationCoordinate2D... CLLocationCoordinate2D nextCoord = MKCoordinateForMapPoint(nextMapPoint); //update the plane's coordinate... thePlane.coordinate = nextCoord; //schedule the next update... [self performSelector:@selector(updatePlanePosition) withObject:nil afterDelay:0.5]; }