3

I'm trying to add an UIGestureRecognizer to one the whole google map view.

I want to get notified if i touch the map ( not the marker ), but i don't know how. what i did is this inside viewDidLoad:

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

and outside viewDidLoad:

- (void)didTapMap:(UITapGestureRecognizer *)recognizer {
    NSLog(@"Touched map");
}

but this method don't work and don't print anything on the console window..

please help me and show me how to do it please

erenon
  • 18,838
  • 2
  • 61
  • 93

4 Answers4

17

I think what you need is already part of the map's delegate

 /**
 * Called after a tap gesture at a particular coordinate, but only if a marker
 * was not tapped.  This is called before deselecting any currently selected
 * marker (the implicit action for tapping on the map).
 */
- (void)mapView:(GMSMapView *)mapView
    didTapAtCoordinate:(CLLocationCoordinate2D)coordinate;

there are other methods in that delegate, take a look a them too.

Use the following function for Swift:

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) 
Vimal Venugopalan
  • 4,091
  • 3
  • 16
  • 25
Gonzalo E
  • 196
  • 1
  • 6
  • Can someone convert this to Swift 3? Does the latest version of the google maps sdk and Swift still even look like this? – Neo42 Aug 11 '17 at 13:59
1
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
    // In My Case Im adding one overlay view on marker tap.
    // infoWindow is a subclass of UIview in my code

    infoWindow.removeFromSuperview()
    infoWindow = loadNiB()
    infoWindow.center = mapView.projection.point(for: marker.position)
    infoWindow.center.y = infoWindow.center.y - sizeForOffset(view: infoWindow)
    self.view.addSubview(infoWindow)

    return false
}
Rajasekhar Pasupuleti
  • 1,598
  • 15
  • 22
  • 2
    While this code-only answer may answer the question, please add an explanation of why it does so. This will help future users evaluate the answer for their situation. – Tom Brunberg Jan 25 '18 at 11:58
0

for Swift 3

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
    NSLog(@"Touched map");
}
Anil
  • 272
  • 3
  • 14
-2
- (void) mapView:(GMSMapView *)mapView didTapAtCoordinate (CLLocationCoordinate2D)coordinate{
NSLog(@"User did tap at coordinate %f, %f", coordinate.latitude, coordinate.longitude) ;
NSLog(@"Map view center %f %f and zoom is %f",  mapView.camera.target.latitude, mapView.camera.target.longitude, mapView.camera.zoom) ;
  }
}
Inder_iOS
  • 1,636
  • 1
  • 12
  • 20