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 が含まれます。
スポットでのクリック イベントに対しては応答できます。お店やサービスなどのスポットに関するガイドをご覧ください。
その他のイベント
GMSMapViewDelegate
のメソッドの完全なリストについては、リファレンス ガイドをご覧ください。