在MapKit中沿弧线对视觉元素进行动画处理 [英] Animate a visual element along an arc in MapKit
问题描述
如何在我创建的内部贴图工具包中沿弧线添加视觉元素并为其设置动画?
How do I add and animate a visual element along an arc, which I have created inside mapkit?
以下代码将在两点之间创建一条漂亮的弧线.想象一个动画的视觉效果,它将代表飞机沿此弧线飞行.
The following code will create a nice arc between two points. Imagine an animated visual that will represent an airplane 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];
}
推荐答案
注释实际上可能是最好的选择.使用可分配的坐标属性定义注释类(或使用MKPointAnnotation
).
The annotation might be the best option actually. Define an annotation class with an assignable coordinate property (or use MKPointAnnotation
).
令人惊讶的是,MKGeodesicPolyline
类足够好,可以通过points
属性(给出MKMapPoint
s)或getCoordinates:range:
方法(给出CLLocationCoordinate2D
)来提供它计算出的用于创建圆弧的单个点. > s).
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 MKMapPoint
s) or the getCoordinates:range:
method (gives the CLLocationCoordinate2D
s).
(实际上,该属性和方法在MKMultiPoint
类中,其中MKPolyline
是MKPolyline
的子类,而MKGeodesicPolyline
是MKPolyline
的子类.)
(Actually, that property and method are in the MKMultiPoint
class which MKPolyline
is a subclass of and MKGeodesicPolyline
is a subclass of MKPolyline
.)
只需在计时器上更新注释的coordinate
属性,地图视图就会自动移动注释.
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.
这是一个使用points
属性(比getCoordinates:range:
方法更容易使用)和performSelector:withObject:afterDelay:
的简单,粗略的示例:
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];
}
这篇关于在MapKit中沿弧线对视觉元素进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!