With iOS 3.2 or greater, it's probably better and simpler to use a
UIGestureRecognizer with the map view instead of trying to subclass it and intercepting touches manually.First, add the gesture recognizer to the map view:UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc]Next, implement
initWithTarget:self action:@selector(tapGestureHandler:)];
tgr.delegate = self; //also addto @interface
[mapView addGestureRecognizer:tgr];
[tgr release];shouldRecognizeSimultaneouslyWithGestureRecognizerand returnYESso your tap gesture recognizer can work at the same time as the map's
(otherwise taps on pins won't get handled automatically by the map):- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizerFinally, implement the gesture handler:
shouldRecognizeSimultaneouslyWithGestureRecognizer
:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}- (void)tapGestureHandler:(UITapGestureRecognizer *)tgr
{
CGPoint touchPoint = [tgr locationInView:mapView];
CLLocationCoordinate2D touchMapCoordinate
= [mapView convertPoint:touchPoint toCoordinateFromView:mapView];
NSLog(@"tapGestureHandler: touchMapCoordinate = %f,%f",
touchMapCoordinate.latitude, touchMapCoordinate.longitude);
}
