Google Maps iOS SDK触摸事件

时间:2015-03-14 10:27:48

标签: gmsmapview

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

如果我触摸地图(不是标记),我想收到通知,但我不知道如何。我做的是这个内部viewDidLoad:

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

和viewDidLoad外部:

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

但这种方法无法正常工作,也无法在控制台窗口上打印任何内容。

请帮帮我,告诉我该怎么做

4 个答案:

答案 0 :(得分:16)

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

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

答案 1 :(得分:0)

for Swift 3

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

答案 2 :(得分:0)

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