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:
が再度呼び出されます。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
のメソッドの完全なリストについては、リファレンス ガイドをご覧ください。