在MapKit中沿弧形设置可视元素的动画

che*_*ewy 5 objective-c mapkit mkmapview ios

如何在arckit中创建的弧形视觉元素添加动画和动画?

以下代码将在两点之间创建一个漂亮的弧.想象一下,动画视觉将代表沿着这条弧线飞行的飞机.

-(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];
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

小智 6

实际上,注释可能是最佳选择.使用可指定的坐标属性(或使用MKPointAnnotation)定义注记类.

令人惊讶的是,这个MKGeodesicPolyline类非常友好地提供它计算出来的各个点,通过points属性(给出MKMapPoints)或getCoordinates:range:方法(给出CLLocationCoordinate2Ds)来创建弧.

(实际上,该属性和方法属于MKMultiPointMKPolyline的子类,并且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's coordinate...
    thePlane.coordinate = nextCoord;

    //schedule the next update...    
    [self performSelector:@selector(updatePlanePosition) withObject:nil afterDelay:0.5];
}
Run Code Online (Sandbox Code Playgroud)