Animate a visual element along an arc in MapKit

一个人想着一个人 提交于 2019-12-04 07:15:49

The annotation might be the best option actually. Define an annotation class with an assignable coordinate property (or use MKPointAnnotation).

Amazingly, the MKGeodesicPolyline class is kind enough to supply the individual points that it calculated to create the arc through the points property (gives the MKMapPoints) or the getCoordinates:range: method (gives the CLLocationCoordinate2Ds).

(Actually, that property and method are in the MKMultiPoint class which MKPolyline is a subclass of and MKGeodesicPolyline is a subclass of MKPolyline.)

Just update the annotation's coordinate property on a timer and the map view will automatically move the annotation.

Note: For an arc this long, there will be thousands of points.

Here's a very simple, crude example using the points property (easier to use than the getCoordinates:range: method) and 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];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!