アクティビティ

プラットフォームを選択: 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: の呼び出し後)渡されます。 中間のカメラ位置を指定します。

  • 最後に、カメラの位置が決定されると mapView:idleAtCameraPosition: が呼び出されます。 GMSMapView ではアイドル状態になり、関連するカメラ位置が指定されます。 この時点で、すべてのアニメーションや操作は終了しています。

    アプリケーションはこのイベントを使用して、マーカーなどの更新をトリガーできます。 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 のメソッドの完全なリストについては、以下をご覧ください。 リファレンス ガイドをご覧ください。