Animating a visual element along an arc in MapKit

How to add and animate a visual element along an arc that I created inside mapkit ?

The following code will create a nice arc between two points. Imagine an animated visual, which will be a plane flying along this arc.

-(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];
}

enter image description here

+4
source share
1 answer

Abstract may be the best option. Define an annotation class with an assignable coordinate property (or use MKPointAnnotation).

, MKGeodesicPolyline , , , points ( MKMapPoint s) getCoordinates:range: ( ).

(, MKMultiPoint, MKPolyline MKGeodesicPolyline MKPolyline.)

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 coordinate...
    thePlane.coordinate = nextCoord;

    //schedule the next update...    
    [self performSelector:@selector(updatePlanePosition) withObject:nil afterDelay:0.5];
}
+5

Source: https://habr.com/ru/post/1532462/


All Articles