Sunshine Ghanem
Sunshine Ghanem

Reputation: 31

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

Upvotes: 3

Views: 7237

Answers (4)

Rajasekhar Pasupuleti
Rajasekhar Pasupuleti

Reputation: 1658

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
}

Upvotes: 1

Anil
Anil

Reputation: 272

for Swift 3

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
    NSLog(@"Touched map");
}

Upvotes: 0

Gonzalo E
Gonzalo E

Reputation: 196

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) 

Upvotes: 17

Inder_iOS
Inder_iOS

Reputation: 1656

- (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) ;
  }
}

Upvotes: -2

Related Questions