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