iPhone: Detecting Tap in MKMapView

ChrisJF picture ChrisJF · Aug 14, 2009 · Viewed 25.8k times · Source

How do I detect a single tap on an instance of MKMapView? Do I have to subclass MKMapView and then override the touchesEnded method?

Thanks,

-Chris

Answer

pseudopeach picture pseudopeach · Dec 31, 2010

If you're just looking to get notified of tap gestures without affecting any of the other touch behavior of the map, you'll want to use a UITapGestureRecognizer. It's super simple, just put in some code like this.

UITapGestureRecognizer* tapRec = [[UITapGestureRecognizer alloc] 
   initWithTarget:self action:@selector(didTapMap:)];
[theMKMapView addGestureRecognizer:tapRec];
[tapRec release];

That will call the didTapMap whenever theMKMapView receives a tap gesture and all the pinching, and dragging gestures will still work as they did before.