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 のメソッドの完全なリストについては、
リファレンス ガイドをご覧ください。