iOS 在 MapBox 地图上移动注释
iOS Moving annotations on MapBox map
是否可以在不删除和添加新注释的情况下移动注释?
我想坚持使用 MapBox,因为将来会支持离线地图。
提前致谢!!
从 Mapbox iOS SDK v3.2.0 开始,无法更新已添加到地图的注释的坐标。 Here is the relevant ticket on Github.
您可以对移动标记进行自定义开发。我已经完成了,它按预期工作。
如果您想沿多段线移动标记,我会首先保存对 MGLPointAnnotation 的引用,然后在计时器上或当用户随它移动时更新其坐标 属性。
您可以通过保留对注释实例的引用然后更新该实例的坐标来实现。
如果我假设您要将地图 (MGLMapView) 添加到视图控制器,那么基本方法可能是将 属性 添加到该视图控制器,以便您可以跟踪引用。对于此示例,我将使用 MGLPointAnnotation:
class ViewControllerWithAMap: UIViewController {
var movingPointAnnotation: MGLPointAnnotation?
...
}
将此引用设为可选,以便您知道何时启动它,然后只更新坐标。最后一部分似乎有悖常理,但您现在真正需要做的就是再次向地图 添加 注释。例如:
if self.movingPointAnnotation == nil {
self.movingPointAnnotation = MGLPointAnnotation()
}
guard self.movingPointAnnotation != nil else {
print("What?! Could not initiate moving annotation.")
return // something weird, give up
}
self.movingPointAnnotation!.coordinate = myNewCLLocationCoordinate2D
self.mapView.addAnnotation(self.movingPointAnnotation!)
关于最后那个一遍又一遍加注解的问题,我试过只加一次注解,然后再更新坐标,但是注解没有动。
是否可以在不删除和添加新注释的情况下移动注释?
我想坚持使用 MapBox,因为将来会支持离线地图。
提前致谢!!
从 Mapbox iOS SDK v3.2.0 开始,无法更新已添加到地图的注释的坐标。 Here is the relevant ticket on Github.
您可以对移动标记进行自定义开发。我已经完成了,它按预期工作。
如果您想沿多段线移动标记,我会首先保存对 MGLPointAnnotation 的引用,然后在计时器上或当用户随它移动时更新其坐标 属性。
您可以通过保留对注释实例的引用然后更新该实例的坐标来实现。
如果我假设您要将地图 (MGLMapView) 添加到视图控制器,那么基本方法可能是将 属性 添加到该视图控制器,以便您可以跟踪引用。对于此示例,我将使用 MGLPointAnnotation:
class ViewControllerWithAMap: UIViewController {
var movingPointAnnotation: MGLPointAnnotation?
...
}
将此引用设为可选,以便您知道何时启动它,然后只更新坐标。最后一部分似乎有悖常理,但您现在真正需要做的就是再次向地图 添加 注释。例如:
if self.movingPointAnnotation == nil {
self.movingPointAnnotation = MGLPointAnnotation()
}
guard self.movingPointAnnotation != nil else {
print("What?! Could not initiate moving annotation.")
return // something weird, give up
}
self.movingPointAnnotation!.coordinate = myNewCLLocationCoordinate2D
self.mapView.addAnnotation(self.movingPointAnnotation!)
关于最后那个一遍又一遍加注解的问题,我试过只加一次注解,然后再更新坐标,但是注解没有动。