Ios 长按手势后在地图上查找地图批注

Ios 长按手势后在地图上查找地图批注,ios,swift,annotations,mapbox,Ios,Swift,Annotations,Mapbox,我是iOS开发和mapbox的新手,很抱歉这个问题听起来很愚蠢,但我在任何地方都找不到答案。 我有一张地图和注释在上面。我想在用户触摸并按住注释长按手势时显示一些其他信息。我得到了长按手势的工作,但已经找到了找到被触摸的注释的方法,或者至少找到了它的索引。到目前为止,我是这样做的: class eventsMapController: UIViewController, MGLMapViewDelegate { override func viewDidLoad() { super.vi

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

class eventsMapController: UIViewController, MGLMapViewDelegate {
override func viewDidLoad() {
    super.viewDidLoad()
    mapView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

    // Set the map's bounds to Oslo 59.920269,10.71167
    //let bounds = MGLCoordinateBounds(sw: CLLocationCoordinate2D(latitude: 59.925861, longitude: 10.712185),
                                //     ne: CLLocationCoordinate2D(latitude: 59.889798, longitude: 10.794754))        
    view.addSubview(mapView)

    // Set the map view‘s delegate property
    mapView.delegate = self

    let myGesture = UILongPressGestureRecognizer(target: self, action: #selector(eventsMapController.testLongGesture))
    myGesture.minimumPressDuration = 0.8
    mapView.addGestureRecognizer(myGesture) 
}
func testLongGesture(long: UILongPressGestureRecognizer){
    if long.state == .Began{ 
        print("begin", long)
    }
}
}
我正在添加这样的注释

let pointAnotation = MGLPointAnnotation()
        pointAnotation.coordinate = CLLocationCoordinate2DMake(event.lat, event.lng)
        pointAnotation.title = name
        pointAnotation.subtitle = headline
self.mapView.addAnnotation(pointAnotation)

请帮帮我,我花了好几个小时试图弄明白。

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

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

let longPressPoint : CGPoint = long.locationInView(mapView)
let longPressCoordinate = mapView.convertPoint(longPressPoint, toCoordinateFromView:mapView)
找出与长按位置对应的地图坐标。比如:

let longPressPoint : CGPoint = long.locationInView(mapView)
let longPressCoordinate = mapView.convertPoint(longPressPoint, toCoordinateFromView:mapView)
这将为您提供与长按位置相对应的CLLocationCoordinate2D

浏览注释,将每个注释的坐标与长按坐标进行比较,以找到一个接近长按坐标的注释。请记住,长按可能不在任何注释上,因此不要自动选择最近的注释


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

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

let longPressPoint : CGPoint = long.locationInView(mapView)
let longPressCoordinate = mapView.convertPoint(longPressPoint, toCoordinateFromView:mapView)
找出与长按位置对应的地图坐标。比如:

let longPressPoint : CGPoint = long.locationInView(mapView)
let longPressCoordinate = mapView.convertPoint(longPressPoint, toCoordinateFromView:mapView)
这将为您提供与长按位置相对应的CLLocationCoordinate2D

浏览注释,将每个注释的坐标与长按坐标进行比较,以找到一个接近长按坐标的注释。请记住,长按可能不在任何注释上,因此不要自动选择最近的注释