mapView: didSelectAnnotationView: not functioning properly.
Asked Answered
C

3

6

I'm building an IOS app that uses the built in map view. Im successfully placing custom annotations, etc. However, I'm having a problem with the delegate function that is called when an annotation is pressed (mapView:didSelectAnnotationView).

The first time I press an annotation, the function is called properly. However, if I proceed to click the same annotation again, the function does not fire. If I click on a different annotation at this point, the function WILL fire but then if I click on THAT annotation again, the function does not fire. Basically, I can never click on the same annotation twice in a row. The delegate function will only be called the first time. Has anyone encountered this problem? Is there somewhere in particular I should look for the bug?

Coughlin answered 29/10, 2014 at 0:8 Comment(1)
I have the same issue, but there is one difference, ie. I am getting issue when the custom annotationview comes on the border of the mapview.Finnic
P
5

Well, when you think about it, you have already selected that annotation view. It doesn't make sense for the delegate to tell you that the pin is selected if it already is.

A simple fix could be to set the annotation to deselected in the delegate call. This should allow you to get the call again.

[annotation setSelected:NO animated:NO];

Vists here for the method you need to call. https://developer.apple.com/library/ios/documentation/mapkit/reference/MKAnnotationView_Class/index.html#//apple_ref/occ/instm/MKAnnotationView/setSelected:animated:

Plumbic answered 29/10, 2014 at 0:20 Comment(1)
What didn't work for you? Are you using Objective-C still? This answer is very old, so it could have been made obsolete in the intervening time.Plumbic
C
5

Friend suggested an idea and it turned out to be correct. When didSelectAnnotationView fires, it actually tags the annotation as selected somehow. Then when you click it again, the delegate function doesn't fire because it is 'already selected'. You have to manually deselect the annotation by calling the following function once you're done doing what you want.

[mapView deselectAnnotation:view.annotation animated:false];
Coughlin answered 29/10, 2014 at 0:18 Comment(0)
P
5

Well, when you think about it, you have already selected that annotation view. It doesn't make sense for the delegate to tell you that the pin is selected if it already is.

A simple fix could be to set the annotation to deselected in the delegate call. This should allow you to get the call again.

[annotation setSelected:NO animated:NO];

Vists here for the method you need to call. https://developer.apple.com/library/ios/documentation/mapkit/reference/MKAnnotationView_Class/index.html#//apple_ref/occ/instm/MKAnnotationView/setSelected:animated:

Plumbic answered 29/10, 2014 at 0:20 Comment(1)
What didn't work for you? Are you using Objective-C still? This answer is very old, so it could have been made obsolete in the intervening time.Plumbic
V
1
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)aView
    {
        indexPathTag=aView.tag;
        [mapView deselectAnnotation:aView.annotation animated:YES];

    }
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)aView
    {
    }

I hope this will work for you :) I have faced the same problem, this code worked for me.

Velar answered 19/11, 2014 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.