Call PointAnnotation on MKMapView appears and then disappears

I am creating a simple point annotation with UITapGestureRecognizer within the UITapGestureRecognizer delegate.

The first time I click on the card, the output appears with the offset, but after that the shutdown immediately disappears.

The second time, when I click the same output, an inscription appears and remains there, not knowing why it disappears for 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) } 
+5
source share
2 answers

I have the same problem. I do not know how to solve this, but I found a workaround. Perhaps this will help you too.

I used LongPressGesture to replace TapGesture

In Viewdidload:

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

In the addAnnotation function:

 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) 
+1
source

Just in case, someone has a different problem, although Keith's answer works, in my case, he breaks other gestures related to the card, such as pinching and scaling.

For me, putting off a few milliseconds, the action of showing the exponential code has improved.

In Swift 3:

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

All Articles