PointAnnotation callout on MKMapView appears and then immidiately disappears
Asked Answered
S

2

7

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

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)


}
Shoran answered 21/8, 2015 at 10:23 Comment(3)
Once try by removing this line - mapView.selectAnnotation(pointAnnotation, animated: true)Chaos
that is how I coded it first, but without that line the callout does not show at all on the first click (even for a short time).Shoran
This problem is still unresolved, can anyone help please?Shoran
C
2

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.

For me, delaying some milliseconds the action of showing the callout worked better.

In Swift 3:

let deadlineTime = DispatchTime.now() + .milliseconds(500)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
  mapView.addAnnotation(pointAnnotation)
  mapView.selectAnnotation(pointAnnotation, animated: true)
}
Conjunction answered 12/1, 2017 at 14:49 Comment(0)
J
1

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

In Viewdidload:

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

In function 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)
Jedjedd answered 11/3, 2016 at 6:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.