Google 地图 iOS SDK 触摸事件

Google Maps iOS SDK Touch Events

我正在尝试将 UIGestureRecognizer 添加到整个 google 地图视图中。

我想在触摸地图(而不是标记)时收到通知,但我不知道如何操作。我所做的是在 viewDidLoad:

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

和外部 viewDidLoad:

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

但是这个方法不起作用,也不会在控制台上打印任何东西window..

请帮助我并告诉我该怎么做

我想你需要的已经是地图委托的一部分了

 /**
 * 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;

该委托中还有其他方法,也看看它们。

对 Swift 使用以下函数:

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) 
- (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) ;
  }
}

对于Swift 3

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
    NSLog(@"Touched map");
}
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
}