iOS在MapBox地图上移动注释

时间:2015-12-29 17:36:31

标签: swift mapbox

是否可以在不删除和添加新注释的情况下移动注释?

我想坚持使用MapBox,因为未来支持离线地图。

提前谢谢!!

3 个答案:

答案 0 :(得分:5)

从Mapbox iOS SDK v3.2.0开始,无法更新已添加到地图的注释的坐标。 Here is the relevant ticket on Github.

答案 1 :(得分:0)

您可以为移动标记进行自定义开发。我已经完成了它并按预期工作。

如果你想沿折线移动标记,我首先要保持对MGLPointAnnotation的引用,并在定时器或用户随之移动时更新其坐标属性。

答案 2 :(得分:0)

您可以通过保留对注释实例的引用然后更新该实例的坐标来完成此操作。

如果我假设您要将地图(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!)

关于反复添加注释的最后一部分,我尝试只添加一次注释,然后只更新其坐标,但注释没有移动。