事件

请选择平台: Android iOS JavaScript

利用 Maps SDK for iOS,您可以监听地图上发生的事件,例如镜头更改事件或标记点按事件。

简介

如需监听事件,您必须实现 GMSMapViewDelegate 协议。通常,您需要在显示地图的视图控制器上实现此协议。具体流程请参阅以下示例:

Swift

import GoogleMaps

class Events: UIViewController, GMSMapViewDelegate {
  // ...
}
      

Objective-C

@import GoogleMaps;

@interface Events : UIViewController <GMSMapViewDelegate>

@end
      

创建 GMSMapView 后,您可以将其委托设置为您的视图控制器。GMSMapViewDelegate 仅提供可选方法。若要监听任何特定事件,您必须实现相关方法。

Swift

override func loadView() {
  super.loadView()
  let camera = GMSCameraPosition.camera(
    withLatitude: 1.285,
    longitude: 103.848,
    zoom: 12
  )
  let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
  mapView.delegate = self
  self.view = mapView
}

// MARK: GMSMapViewDelegate

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
  print("You tapped at \(coordinate.latitude), \(coordinate.longitude)")
}
      

Objective-C

- (void)loadView {
  [super loadView];
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:1.285
                                                          longitude:103.848
                                                               zoom:12];
  GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  mapView.delegate = self;
  self.view = mapView;
}

#pragma mark - GMSMapViewDelegate

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
  NSLog(@"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
}
      

镜头位置

使用 GMSMapViewDelegate,您可以监听用于渲染地图的镜头位置的变化。有三种不同的事件。

  • mapView:willMove: 表示镜头位置即将更改。如果 gesture 参数设置为 YES,这是由于用户在 GMSMapView 上执行自然手势,例如平移或倾斜。否则,NO 表示这是程序化更改的一部分,例如通过 animateToCameraPosition: 等方法或直接更新地图的图层。如果用户点按了“我的位置”或罗盘按钮(可生成改变镜头的动画),该参数也可能为 NO

    在调用 mapView:idleAtCameraPosition: 之前,系统可能会多次调用该方法,不过通常只有在动画和手势同时发生时才会发生这种情况。例如,手势会取消任何当前动画,并再次调用 mapView:willMove:

  • 在手势或动画期间,系统会反复调用 mapView:didChangeCameraPosition:,始终在调用 mapView:willMove: 之后调用。它会向它传递中间相机位置。

  • 最后,一旦 GMSMapView 上的相机位置变为空闲状态,系统会调用 mapView:idleAtCameraPosition:,并指定相关的相机位置。此时,所有动画和手势均已停止。

    应用可以使用此事件来触发刷新标记或显示在 GMSMapView 上的其他内容,而不是例如在每次摄像头发生变化时重新加载内容。

例如,应用可以清除移动中的 GMSMapView,然后对相机停止的位置进行反向地理编码。

Swift

let geocoder = GMSGeocoder()

func mapView(_ mapView: GMSMapView, willMove gesture: Bool) {
  mapView.clear()
}

func mapView(_ mapView: GMSMapView, idleAt cameraPosition: GMSCameraPosition) {
    geocoder.reverseGeocodeCoordinate(cameraPosition.target) { (response, error) in
      guard error == nil else {
        return
      }

      if let result = response?.firstResult() {
        let marker = GMSMarker()
        marker.position = cameraPosition.target
        marker.title = result.lines?[0]
        marker.snippet = result.lines?[1]
        marker.map = mapView
      }
    }
  }
      

Objective-C

GMSGeocoder *geocoder;

- (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture {
  [mapView clear];
}

- (void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)cameraPosition {
  id handler = ^(GMSReverseGeocodeResponse *response, NSError *error) {
    if (error != nil) {
      return;
    }
    GMSReverseGeocodeResult *result = response.firstResult;
    GMSMarker *marker = [GMSMarker markerWithPosition:cameraPosition.target];
    marker.title = result.lines[0];
    marker.snippet = result.lines[1];
    marker.map = mapView;
  };
  [geocoder reverseGeocodeCoordinate:cameraPosition.target completionHandler:handler];
}
      

商家地图注点及其他地图注点上的事件

默认情况下,地图注点 (POI) 将与对应的图标一起显示在基本地图上。景点包括公园、学校、政府大楼,以及商店、餐馆和酒店等商家景点。

您可以响应地图注点上的点击事件。请参阅有关商家和其他地图注点的指南。

其他事件

如需了解 GMSMapViewDelegate 的方法的完整列表,请参阅参考指南