借助 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
中的方法的完整列表,请参阅参考指南。