活动

请选择平台: 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: 之后。它会通过 中间的相机位置。

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

    应用可以使用此事件来刷新标记或其他 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 上的完整方法列表,请参阅 参考指南