Mapbox iOS SDK 3.1如何使注释标注粘性

时间:2016-04-01 11:24:18

标签: ios mapbox mapbox-gl

我刚开始在iOS上开发。如果问题似乎微不足道,那就很抱歉:

我通过API从SwiftyJSON加载了几个标记/注释,并在Mapbox MapView上显示这些标记/注释。单击注释时,将触发标注。 - 这很好,很容易就像地狱一样。 问题:我想将mapView放在标记/注释和标注上,或者移动相机在中心显示它们,放大。每当我使用这样的东西时:

    let camera = MGLMapCamera(lookingAtCenterCoordinate: annotation.coordinate, fromDistance: 1000, pitch: 0, heading: 0)
    mapView.setCamera(camera, withDuration: 2, animationTimingFunction: CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut))

    mapView.setCenterCoordinate(annotation.coordinate, zoomLevel: 15, animated: true)

标注保持在同一位置。我想要实现的是,它在注释/标记之上显示为粘性。

这是我对此部分的完整代码(不包括viewdidload中的注释的构建:

func mapView(mapView: MGLMapView, imageForAnnotation annotation: MGLAnnotation) -> MGLAnnotationImage? {
    var annotationImage = mapView.dequeueReusableAnnotationImageWithIdentifier("marker_live")

    if annotationImage == nil {
        // Leaning Tower of Pisa by Stefan Spieler from the Noun Project
        var image = UIImage(named: "marker_live")!
        image = image.imageWithAlignmentRectInsets(UIEdgeInsetsMake(0, 0, image.size.height/2, 0))

        annotationImage = MGLAnnotationImage(image: image, reuseIdentifier: "marker_live")
    }

    return annotationImage
}


func mapView(mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
    print("tap on annotation")
    mapView.selectAnnotation(annotation, animated: true)

    return true
}

func mapView(mapView: MGLMapView, calloutViewForAnnotation annotation: MGLAnnotation) -> UIView? {

    return CustomCalloutView(representedObject: annotation)

}

func mapView(mapView: MGLMapView, tapOnCalloutForAnnotation annotation: MGLAnnotation) {
    print("Tapped the callout for: \(annotation.title!)")
    self.performSegueWithIdentifier("PlaylistFromMap", sender: annotation.title!)
    mapView.deselectAnnotation(annotation, animated: true)
}

我还考虑过使用完成处理程序,但不明白它是如何工作的。 非常感谢!

1 个答案:

答案 0 :(得分:1)

标注视图的位置目前无法在显示后更新,但this is a feature我们希望在v3.3.0中添加。

要在setCamera:setCoordinate:动画后打开标注,请使用包含完成处理程序的那些方法的变体,然后使用selectAnnotation: