Monday, August 24, 2015

MKMapView animating positions on a route

Below code animates a marker on the map from source to destination. 

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CLLocationCoordinate2D centerCoord = { 37.774929, -122.419416 };
    [self.mapView setCenterCoordinate:centerCoord zoomLevel:3.0 animated:NO];
    
    CLLocationCoordinate2D sanFrancisco = { 37.774929, -122.419416 };
    CLLocationCoordinate2D newYork = { 40.714353, -74.005973 };
    CLLocationCoordinate2D pointsArc[] = { sanFrancisco, newYork };
    
    thePlane = [[MKPointAnnotation alloc] init];
    thePlane.coordinate = sanFrancisco;
    thePlane.title = @"Plane";
    [self.mapView addAnnotation:thePlane];
    geodesic = [MKGeodesicPolyline polylineWithCoordinates:&pointsArc[0]
                                                     count:2];
    planePositionIndex = 0;
    [self performSelector:@selector(updatePlanePosition) withObject:nil afterDelay:2.0];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)updatePlanePosition
{
    planePositionIndex = planePositionIndex + 50;
    if (planePositionIndex >= geodesic.pointCount)
    {
        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];

}


references:
http://stackoverflow.com/questions/22504822/animate-a-visual-element-along-an-arc-in-mapkit/22506771#22506771

No comments:

Post a Comment