【问题标题】:Mapbox find annotation on the map after long press gestureMapbox长按手势后在地图上查找注释
【发布时间】:2016-06-10 13:02:37
【问题描述】:

我是 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)

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

【问题讨论】:

    标签: ios swift annotations mapbox


    【解决方案1】:

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

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

    1. 找出长按位置对应的地图坐标。比如:

      let longPressPoint : CGPoint = long.locationInView(mapView)
      let longPressCoordinate = mapView.convertPoint(longPressPoint, toCoordinateFromView:mapView)
      

      这会给你一个与长按位置相对应的CLLocationCoordinate2D

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多