イベント

プラットフォームを選択: Android iOS JavaScript

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: が呼び出される前に複数回呼び出されることがあります。ただし、これは通常、アニメーションと操作が同時に行われた場合にのみ行われます。たとえば、操作により現在のアニメーションがキャンセルされ、2 回目の 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 が含まれます。

スポットでのクリック イベントにも反応できます。お店やサービスなどのスポットに関するガイドをご覧ください。

その他のイベント

GMSMapViewDelegate のメソッドの全一覧については、リファレンス ガイドをご覧ください。