3

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

如果我触摸地图(而不是标记),我想得到通知,但我不知道如何。我在viewDidLoad里面做的是这个:

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

和外面的viewDidLoad:

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

但是这种方法不起作用,并且不会在控制台窗口上打印任何内容..

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

4

4 回答 4

17

我认为您需要的已经是地图代表的一部分

 /**
 * 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) 
于 2015-03-19T12:07:03.817 回答
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
}
于 2018-01-25T04:16:31.950 回答
0

斯威夫特 3

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
    NSLog(@"Touched map");
}
于 2018-01-19T07:04:36.813 回答
-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) ;
  }
}
于 2015-12-01T11:13:53.453 回答