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

mir*_*asx 1 annotations ios mapbox swift

我是 iOS 开发和 Mapbox 的新手,如果问题听起来很愚蠢,我很抱歉,但我在任何地方都找不到答案。\n我有一张地图和上面的注释。我想在用户触摸并按住注释(长按手势)时显示一些其他信息。我有长按手势来工作,但找到了找到被触摸的注释的方法,或者至少是它的索引。到目前为止我是这样的:

\n\n
class eventsMapController: UIViewController, MGLMapViewDelegate {\noverride func viewDidLoad() {\n    super.viewDidLoad()\n    mapView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]\n\n    // Set the map\'s bounds to Oslo 59.920269,10.71167\n    //let bounds = MGLCoordinateBounds(sw: CLLocationCoordinate2D(latitude: 59.925861, longitude: 10.712185),\n                                //     ne: CLLocationCoordinate2D(latitude: 59.889798, longitude: 10.794754))        \n    view.addSubview(mapView)\n\n    // Set the map view\xe2\x80\x98s delegate property\n    mapView.delegate = self\n\n    let myGesture = UILongPressGestureRecognizer(target: self, action: #selector(eventsMapController.testLongGesture))\n    myGesture.minimumPressDuration = 0.8\n    mapView.addGestureRecognizer(myGesture) \n}\nfunc testLongGesture(long: UILongPressGestureRecognizer){\n    if long.state == .Began{ \n        print("begin", long)\n    }\n}\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我正在添加这样的注释

\n\n
let pointAnotation = MGLPointAnnotation()\n        pointAnotation.coordinate = CLLocationCoordinate2DMake(event.lat, event.lng)\n        pointAnotation.title = name\n        pointAnotation.subtitle = headline\nself.mapView.addAnnotation(pointAnotation)\n
Run Code Online (Sandbox Code Playgroud)\n\n

请帮助我,我花了很多时间试图弄清楚。

\n

Tom*_*ton 5

我想我首先要尝试的是实现委托方法mapView(_:didSelectAnnotation:)。我不确定长按时是否会调用它,但如果是这样,它会告诉您要使用哪个注释,从而使事情变得相对简单。

如果没有,您可能需要执行以下操作:

  1. 找出长按位置对应的地图坐标。就像是:

    let longPressPoint : CGPoint = long.locationInView(mapView)
    let longPressCoordinate = mapView.convertPoint(longPressPoint, toCoordinateFromView:mapView)
    
    Run Code Online (Sandbox Code Playgroud)

    这为您提供了CLLocationCoordinate2D与长按位置相对应的位置。

  2. 运行注释,将每个注释的坐标与长按坐标进行比较,以找到最接近长按的坐标。请记住,长按可能不会针对任何注释,因此不要自动选择最接近的注释。