カメラを調整する

カメラを使用すると、ユーザーの 視点位置を変更できます。カメラモードを使用すると、ナビゲーション中の地図の動作を制御できます。カメラモードを設定するには、地図ビューの cameraMode プロパティを設定し、次のいずれかのカメラモード定数を指定します。

  • 追従 - ナビゲーションのデフォルトのカメラモードです。ビューアングルを 45 度に変更し、カメラを現在位置の後ろに配置して進行方向を向かせます。ナビゲーション中、カメラは進行方向を向くように自動的に調整されます。地図の [中央に戻す] ボタンを押すと、このモードに切り替わります。このモードが選択されている場合、[中央に戻す] ボタンは表示されません。

  • 概要 - ルート全体を概要表示します。ルートが地図ビューに収まるように必要に応じてズームします。このビューが選択されている場合、[中央に戻す] ボタンが表示されます。

  • フリー - ユーザーがジェスチャーで地図ビューを変更できます。 このビューでは、カメラは静止したままです。ナビゲーション中にユーザーがパンまたはズームすると、地図は自動的にこのビューに切り替わります。このビューが選択されている場合、[中央に戻す] ボタンが表示されます。

カメラモードを変更するには、次のように地図ビューの cameraMode プロパティを設定します。

Swift

// Set the mode to "overview":
mapView.cameraMode = .overview

// Set the mode to "free":
mapView.cameraMode = .free

// Set the mode to "following":
mapView.cameraMode = .following

Objective-C

// Set the mode to "overview":
mapView.cameraMode = GMSNavigationCameraModeOverview;

// Set the mode to "free":
mapView.cameraMode = GMSNavigationCameraModeFree;

// Set the mode to "following":
mapView.cameraMode = GMSNavigationCameraModeFollowing;

地図を自動的に中央に戻す

ユーザーがナビゲーション モードで地図を移動すると、地図ビューのカメラモードが追従モードからフリーモードに変わります。ユーザーが [中央に戻す] を明示的に押すと、カメラは追従モードに戻ります。タイマーを使用して、追従モードを終了してから自動的に戻るまでの間隔を設定することで、追従モードに戻る処理を自動化できます。

次のコード例では、ナビゲーション モード中にユーザーが地図を移動しているかどうかを確認します。移動している場合は、タイマーを設定してカメラモードを追従モードに切り替え、5 秒後に地図を中央に戻します。

Swift

class YourViewController: UIViewController {

  @IBOutlet weak var mapView: GMSMapView!
  var autoFollowTimer: Timer!

  override func viewDidLoad() {
    super.viewDidLoad()
    mapView.delegate = self
    ...
  }

  ...
}

/** Implements the GMSMapViewDelegate protocol. */
extension YourViewController: GMSMapViewDelegate {
  func mapView(_ mapView: GMSMapView, willMove gesture: Bool) {
    if mapView.navigator?.isGuidanceActive == false {return}
    if !gesture {return}

    autoFollowTimer?.invalidate()
    autoFollowTimer = Timer(
      timeInterval: TimeInterval(5.0),
      target: self,
      selector: #selector(recenterMap),
      userInfo: nil,
      repeats: false)
    RunLoop.current.add(autoFollowTimer, forMode: .default)
  }

  /** Centers the map in guidance mode. */
  @objc private func recenterMap() {
    if mapView.navigator?.isGuidanceActive == true {
       mapView.cameraMode = .following
    }

    autoFollowTimer.invalidate()
    autoFollowTimer = nil
  }
}

Objective-C

@interface YourViewController : UIViewController<GMSMapViewDelegate>
...
@end


@implementation YourViewController {
  GMSMapView *_mapView;
  NSTimer *_autoFollowTimer;
  ...
}

...

- (void)viewDidLoad {
  [super viewDidLoad];
  ...
  _mapView.delegate = self;
  ...
}

...

/** Implements the GMSMapViewDelegate protocol. */
- (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture {
  if (!_mapView.navigator.guidanceActive) return;
  if (!gesture) return;

  [_autoFollowTimer invalidate];
  _autoFollowTimer = [NSTimer scheduledTimerWithTimeInterval:5.0
                                                      target:self
                                                    selector:@selector(recenterMap)
                                                    userInfo:nil
                                                     repeats:NO];
}

/** Centers the map in guidance mode. */
- (void)recenterMap {
  if (_mapView.navigator.guidanceActive) {
    _mapView.cameraMode = GMSNavigationCameraModeFollowing;
  }

  [_autoFollowTimer invalidate];
  _autoFollowTimer = nil;
}

@end