问题
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
回答1:
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)
回答2:
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
}
回答3:
for Swift 3
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
NSLog(@"Touched map");
}
回答4:
- (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) ;
}
}
来源:https://stackoverflow.com/questions/29047860/google-maps-ios-sdk-touch-events