使用 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 options = GMSMapViewOptions() options.camera = camera let mapView = GMSMapView(options: options) 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]; GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init]; options.camera = camera; GMSMapView *mapView = [[GMSMapView alloc] initWithOptions:options]; 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
static 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 的完整方法列表,请参阅参考指南。