我如何沿着弧线添加和设置可视化元素的动画,该可视化元素是我在mapkit 内部创建的的?

下面的代码将在两点之间创建一条漂亮的弧线。想象一个动画的视觉效果,它将代表飞机沿此弧线飞行。

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

最佳答案

注释实际上可能是最好的选择。使用可分配的坐标属性定义注释类(或使用MKPointAnnotation)。

令人惊讶的是,MKGeodesicPolyline类足以提供它计算出的各个点,这些点是通过points属性(提供MKMapPoint)或getCoordinates:range:方法(提供CLLocationCoordinate2D)来创建弧的。

(实际上,该属性和方法在MKMultiPoint类中,MKPolylineMKGeodesicPolyline的子类,而MKPolylinecoordinate的子类。)

只需在计时器上更新注释的points属性, map View 就会自动移动注释。

注意:对于这样长的弧,将有成千上万的点。

这是一个使用getCoordinates:range:属性(比performSelector:withObject:afterDelay:方法更容易使用)和ojit_code的非常简单的粗略示例:

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

关于ios - 在MapKit中沿弧线对视觉元素进行动画处理,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22504822/

10-13 08:50