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

nis*_*pta 7 annotations mkmapview ios callout swift

我正在UITapGestureRecognizer委托中创建一个带有标注的简单点注释.

我第一次点击地图时,引脚会出现带有标注,但标注会立即消失.

第二次点击相同的引脚时,标注出现并停留在那里,不知道为什么它会在第一次消失.

@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)


}
Run Code Online (Sandbox Code Playgroud)

小智 1

我也有同样的问题。我也不知道如何解决,但我找到了解决方法。也许它也可以帮助你。

我曾经LongPressGesture替换过TapGesture

在视图中加载:

let longPress = UILongPressGestureRecognizer(target: self, action: "addAnnotation:")
longPress.minimumPressDuration = 0.1
self.mapView.addGestureRecognizer(longPress)
Run Code Online (Sandbox Code Playgroud)

在函数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)
Run Code Online (Sandbox Code Playgroud)