カメラを使用すると、地図に対するユーザーの視点を変更できます。カメラモードを使用して、ナビゲーション中の地図の動作を制御できます。カメラモードを設定するには、地図ビューの cameraMode
プロパティを設定し、次のいずれかのカメラモード定数を指定します。
フォロー中 - ナビゲーションのデフォルトのカメラモード。ビュー角度を 45 度に変更し、移動方向で現在の位置より後ろにカメラを配置します。ナビゲーション中、カメラは進行方向に顔に合わせて自動的に調整されます。地図の [中心を再設定] ボタンを押すと、このモードに切り替わります。このモードを選択した場合、[中央に戻る] ボタンは表示されません。
概要 - ルート全体の概要を表示します。地図表示にルートが収まるように必要に応じてズームします。このビューを選択すると、[Re-center] ボタンが表示されます。
無料 - ユーザーが操作で地図ビューを変更できます。 カメラはこのビューで固定されています。ナビゲーション中にユーザーがパンやズームを行うと、地図は自動的にこのビューに切り替わります。このビューを選択すると、[Re-center] ボタンが表示されます。
カメラモードを変更するには、地図ビューの 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