Search code examples
gmsmapview

Google Maps iOS SDK Touch Events


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


Solution

  • 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)