Philip Sheard
Philip Sheard

Reputation: 5825

Selecting a MapView Annotation Twice

My iPhone app has a mapview with a large number of locations that the user can select from. I would like him to be able to tap on one of the annotations to display its callout view, and then again to actually select it. The problem is that the didSelectAnnotationView only gets called once.

So how can I detect the selection an already selected annotation? Alternatively, how can I deselect an annotation without hiding the callout view? The user can work round this by deselecting the annotation before he selects it again, but this is not intuitive, and I want to avoid him having to do this.

Upvotes: 5

Views: 2410

Answers (3)

Karsten
Karsten

Reputation: 109

For those who are having this problem in '16, here is the Swift version:

mapView.deselectAnnotation(view.annotation!, animated: false)

Upvotes: 4

Mohit Manhas
Mohit Manhas

Reputation: 3541

I just found the solution with the code below:

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

}

This way the selected annotation gets deselected and you can select it once again.

Upvotes: 8

Brian Tunning
Brian Tunning

Reputation: 153

The annotation can't move from the selected state to the selected state again.

You can instead use a custom mkannotationview, where you override setSelected, and install a UITapGestureRdcognizer on your view, and remove it when the view is deselected.

wire up the recognized to do what you want for the tap-when-selected state.

Upvotes: 0

Related Questions