「攝影機」可變更使用者地圖的視角。您可以使用相機模式控管地圖在導航期間的行為。如要設定相機模式,請設定地圖檢視的 cameraMode
屬性,並指定下列其中一個相機模式常數:
追蹤 — 導航的預設相機模式。將視角變更為 45 度,並將相機置於目前位置後方 (朝行方向)。導航期間,攝影機會自動配合行進方向調整。按下地圖的「重新置中」按鈕也會切換為這個模式。選取這個模式時,系統不會顯示「Re-center」按鈕。
總覽 - 顯示完整路線總覽,視需要縮放,讓路線符合地圖檢視。選取這個檢視畫面時,系統會顯示「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;
自動將地圖置中
使用者以導航模式移動地圖時,地圖檢視畫面的相機模式會從以下模式變更為免費模式。使用者明確按下「重新置中」時,攝影機會返回以下模式。您可以使用計時器設定離開以下模式之間的間隔,然後再自動返回,藉此自動返回下列模式。
範例
以下程式碼範例會檢查使用者在導航模式中移動地圖時是否會移動。如果符合,系統會設定計時器,將相機模式切換為下列模式,並將地圖中心於五秒後置中。
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