14

On my iPad app I am using iOS map to display few points using annotation. I want to display custom callout too when an annotation is been selected. I am using UIPopoverController to show callout. However this works only when first time tap on annotation. If I want to see the callout on same annotation, I have to select different annotation first and then tap on previous annotation.

Basically this delegate method is not firing second time.

-(void)mapView:(MKMapView *)mapView 
    didSelectAnnotationView:(MKAnnotationView *)view

I am using storyboard and delegate is set on there. This is how I set the annotations.

[self.mapView addAnnotations:self.placemarksArray] 

Can anyone please let me know the reason for above issue? Thanks

Chinthaka
  • 966
  • 1
  • 13
  • 42
  • Have you tried calling deselectAnnotation at the top of the didSelectAnnotationView method? –  May 20 '14 at 12:53
  • Wow I haven't. I have tested and it's work. Could you please add this as an answer, then I can accept the answer. Thanks for you help. – Chinthaka May 20 '14 at 12:58

3 Answers3

30

The documentation for the didSelectAnnotationView delegate method says this in the Discussion section:

You can use this method to track changes in the selection state of annotation views.

(I added the bold and italics on the word "changes".)

That means the delegate method only fires when the annotation view's state changes from "not selected" to "selected".


To avoid having to tap on another annotation or the map (which changes the state of the selected annotation to "not selected") and to detect a "select" on the same annotation again, you can force the de-select at the top of the didSelectAnnotationView method:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    [mapView deselectAnnotation:view.annotation animated:YES];

    //existing code to handle tap on annotation...
}
4

Swift 3 and 4:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

    //THIS LINE IS THE SOLUTION
    mapView.deselectAnnotation(view.annotation, animated: true)

}
mschmidt
  • 2,740
  • 4
  • 17
  • 31
Danielvgftv
  • 547
  • 7
  • 6
  • This is badly formatted. Include a discussion. Consider writing the solution in the same language as the question (Objective C). – Gingi Jun 06 '18 at 21:01
2

Swift 3 version:

mapView.deselectAnnotation(view.annotation, animated: true)
marcomoreira92
  • 369
  • 3
  • 9