如何在不添加/删除地图的情况下移动MKAnnotation?

Dev*_*Dev 36 iphone objective-c mapkit mkmapview

是否可以移动MKAnnotation的坐标而无需在地图中添加和删除注释?

sps*_*ley 19

我找到了一个非常简单的方法.我想平滑地更新我的玩家的位置而不删除然后重新添加玩家注释.这适用于iOS4之前的应用程序,它的作用是移动引脚并将地图置于引脚新位置的中心:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[player setCoordinate:aLoc.coordinate];
[mapView setCenterCoordinate:aLoc.coordinate animated:NO];
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

这是相同的事情,但使用推荐的iOS4块功能:

[UIView animateWithDuration:0.5 animations:^{
    [player setCoordinate:aLoc.coordinate];
    [mapView setCenterCoordinate:aLoc.coordinate animated:NO];
}];
Run Code Online (Sandbox Code Playgroud)


Lin*_*Dao 11

在Swift 4.0和IOS 11.0中,我只是将动态属性添加到MKAnnotation类的子类的坐标属性中,并且它的工作原理是:如果更新了MKAnnotation对象的坐标值,则MapView上的所有注释都会更新其位置:

class CarAnnotation: NSObject, MKAnnotation {
    @objc dynamic var coordinate: CLLocationCoordinate2D
    //Add your custom code here
}
Run Code Online (Sandbox Code Playgroud)


100*_*ams 7

确实可以在不删除和添加注释的情况下使用.

您需要观察MKAnnotation的坐标并更改MKAnnotationView的位置.或者你可以(这可能是更优雅的方式)派生你自己的MKAnnotation和MKAnnotationView并使它们"可移动".

有关详细示例,请参阅Moving-MKAnnotationView,该示例还可以设置注释位置的动画.


Gui*_*ume -6

似乎不可能更改 MKAnnotation 对象的坐标,然后将更改通知 MKMapView。

但是,从地图中删除以前的注释、更改注释对象的坐标并将其添加回地图效果很好。

[theMapView removeAnnotation:myAnnotation]; 
[myAnnotation setLatitude:newLatitude];
[theMapView addAnnotation:myAnnotation];
Run Code Online (Sandbox Code Playgroud)

你为什么不想这样做呢?