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

Posted

技术标签:

【中文标题】Mapbox iOS SDK 3.1 如何使注释标注具有粘性【英文标题】:Mapbox iOS SDK 3.1 How to make Annotation Callout sticky 【发布时间】:2016-04-01 11:24:18 【问题描述】:

我刚开始在 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)

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

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

到目前为止了解...但是我怎样才能检测到注释上的触摸?到目前为止,有两个代表 -mapView:didSelectAnnotation: 和 -mapView:annotationCanShowCallout: 我用来检测触摸。使用第一个,我可以触发相机移动,并在完成处理程序之后(再次)选择注释。第二个我可以显示标注。如何在不首先实际选择注释的情况下触发相机和选择? 你不能——至少,Mapbox iOS SDK 提供的任何方法都不能。 我的问题是,我使用selectAnnotation 来触发相机......所以我需要一种方法来单独触发相机或标注。有什么我可以寻找的提示吗?

以上是关于Mapbox iOS SDK 3.1 如何使注释标注具有粘性的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Mapbox iOS 中的特定坐标上放置自定义注释的底部

MapBox iOS SDK - 第一步

使用 .netrc 文件安装 Mapbox iOS SDK

mapbox 轮流导航 iOS

Mapbox长按手势后在地图上查找注释

将 mapbox 字标添加到传单