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
を使用すると、地図のレンダリングに使用されるカメラ位置の変更をリッスンできます。リッスンできるのは、3 つのイベントです。
mapView:willMove:
は、カメラの位置が変更されようとしていることを示します。gesture
引数がYES
に設定されている場合は、ユーザーがGMSMapView
でパンやチルトなどの自然なジェスチャーを実行しているためです。それ以外の場合、NO
は、これがプログラムによる変更の一部であることを示します(animateToCameraPosition:
などのメソッドを使用した変更や、地図のレイヤを直接更新するなど)。ユーザーが [自分の位置情報] ボタンまたはコンパス ボタンをタップした場合、カメラを変更するアニメーションが生成され、NO
になることもあります。このメソッドは、
mapView:idleAtCameraPosition:
が呼び出される前に複数回呼び出されることがあります。ただし、通常は、アニメーションとジェスチャーが同時に発生した場合にのみ発生します。たとえば、ジェスチャーによって現在のアニメーションがキャンセルされ、mapView:willMove:
が 2 回呼び出されます。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)は、対応するアイコンとともにベースマップにデフォルトで表示されます。POI には、公園、学校、政府機関に加えて、店舗、レストラン、ホテルなどの商業 POI が含まれます。
スポットでのクリック イベントにも反応できます。お店やサービスなどのスポットに関するガイドをご覧ください。
その他のイベント
GMSMapViewDelegate
のメソッドの一覧については、リファレンス ガイドをご覧ください。