简体   繁体   English

出现MKMapView上的PointAnnotation标注,然后立即消失

[英]PointAnnotation callout on MKMapView appears and then immidiately disappears

I am creating a simple point annotation with a callout inside the UITapGestureRecognizer delegate. 我正在UITapGestureRecognizer委托中使用callout创建一个简单的点注释。

The first time I tap on the map, the pin appears with the callout but the callout immediately disappears after that. 我第一次点击地图时,引脚会出现带有标注,但标注会立即消失。

The second time I tap on the same pin, the callout appears and stays there, not sure why it disappears at the first time. 第二次点击相同的引脚时,标注出现并停留在那里,不知道为什么它会在第一次消失。

@IBAction func handleMapTouch(recognizer: UITapGestureRecognizer){
    let view = recognizer.view
    let touchPoint=recognizer.locationInView(view)
    var touchCord=CLLocationCoordinate2D()

    touchCord = mapView.convertPoint(touchPoint, toCoordinateFromView:
     mapView)

        mapView.removeAnnotations(mapView.annotations)
        pointAnnotation.coordinate=touchCord
        pointAnnotation.title="ABC"
        pointAnnotation.subtitle="DEF"

        mapView.addAnnotation(pointAnnotation)
        mapView.selectAnnotation(pointAnnotation, animated: true)


}

Just in case someone else has the same problem, although Keith's answer works, in my case it disrupts other gestures associated to the map, like pinch and zoom. 为了防止其他人遇到同样的问题,虽然Keith的答案有效,但就我而言,它会破坏与地图相关的其他手势,例如捏合和缩放。

For me, delaying some milliseconds the action of showing the callout worked better. 对我来说,延迟几毫秒显示标注的动作效果更好。

In Swift 3: 在Swift 3中:

let deadlineTime = DispatchTime.now() + .milliseconds(500)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
  mapView.addAnnotation(pointAnnotation)
  mapView.selectAnnotation(pointAnnotation, animated: true)
}

I have the same problem. 我也有同样的问题。 I don't know how to solve it, either, but I found a workaround. 我也不知道如何解决它,但我找到了解决方法。 Maybe it can help you too. 也许它也可以帮助你。

I used LongPressGesture to replace TapGesture 我使用LongPressGesture来取代TapGesture

In Viewdidload: 在Viewdidload中:

let longPress = UILongPressGestureRecognizer(target: self, action: "addAnnotation:")
longPress.minimumPressDuration = 0.1
self.mapView.addGestureRecognizer(longPress)

In function addAnnotation: 在函数addAnnotation中:

if(gestureRecognizer.state == .Ended){
    self.mapView.removeGestureRecognizer(gestureRecognizer)

    //remove all annotation on the map
    self.mapView.removeAnnotations(self.mapView.annotations)

    //convert point user tapped to coorinate
    let touchPoint: CGPoint! = gestureRecognizer.locationInView(self.mapView)
    let touchMapCoordinate: CLLocationCoordinate2D = self.mapView.convertPoint(touchPoint, toCoordinateFromView: self.mapView)
    showCustomAnnotation(touchMapCoordinate)
}
self.mapView.addGestureRecognizer(gestureRecognizer)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM