1

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

我通过 SwiftyJSON 从 API 加载几个标记/注释,并将它们显示在 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)
}

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

4

1 回答 1

1

The position of a callout view currently cannot be updated after it has been displayed, but this is a feature we’re looking to add in v3.3.0.

To open the callout after a setCamera: or setCoordinate: animation, use the variants of those methods that includes a completion handler and then selectAnnotation: there.

于 2016-04-01T23:39:11.760 回答