Selecting a MapView Annotation Twice
Asked Answered
E

3

5

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.

Exaggerated answered 26/3, 2012 at 14:41 Comment(6)
If you are using a subclass of MKAnnotationView can you not detect the second press on that view in your own code? (rather than on the 'pin')Aphrodisia
I am not currently subclassing MKAnnotationView, though I guess I could. But I am surprised that there does not seem to be a simple solution to this.Exaggerated
I've just had a fiddle around and I see what you're getting at more clearly now. I click on the annotation and it vanishes though, so no chance of a second click without reselecting the relevant pin anyway.Aphrodisia
Could you not use the typical approach of adding a button as the rightCalloutAccessoryView and handle the button press in calloutAccessoryControlTapped?Shotwell
That would be the obvious solution, nice username by the way.Aphrodisia
Yes, that is what I went for in the end. Thanks, AK.Exaggerated
A
8

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.

Ammonal answered 14/5, 2015 at 9:21 Comment(0)
S
4

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

mapView.deselectAnnotation(view.annotation!, animated: false)
Sue answered 12/5, 2016 at 11:25 Comment(0)
S
0

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.

Stonwin answered 5/4, 2012 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.