I had a similar problem, in that MKMapView
's selection model didn't quite match my applications requirements. To work around this, what we need is a way to recognize taps on annotation views even if they are already selected. We can accomplish this by adding a UITapGestureRecognizer
to each annotation view in the delegate method mapView:viewForAnnotation:
Depending on your interaction model, you might need to completely drive your UI using UITapGestureRecognizer
, rather than using mapView:didSelectAnnotationView:
and mapView:didDeselectAnnotationView:
For example, I'm using a map view as a user picker. As such, selecting a user should move on to the next view controller, even if that user is already selected. Selection only provides visual feedback to the user.
- (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
MKPinAnnotationView *annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"pinAnnotationIdentifier"];
if (annotationView == nil)
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:kSPUserAnnotationViewMapViewReuseIdentifier];
// need to be able to select annotations that are already selected;
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(annotationViewTapped:)];
[annotationView addGestureRecognizer:tapRecognizer];
}
return annotationView;
}
- (void)annotationViewTapped:(id)sender
{
UITapGestureRecognizer *tapRecognizer = (UITapGestureRecognizer*) sender;
MKPinAnnotationView *userView = (MKPinAnnotationView*) tapRecognizer.view;
[self userSelected:(MyUserModel*) userView.annotation];
}
}