調整攝影機

攝影機可讓您變更使用者的地圖視角。您可以使用相機模式來控制導航期間地圖的行為。如要設定相機模式,請設定地圖檢視畫面的 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;

自動將地圖置中

使用者在導航模式中移動地圖時,地圖檢視的相機模式會從以下模式變更為免費模式。當使用者明確按下「Re-center」時,攝影機會恢復為以下模式。您可使用計時器來設定離開以下模式,然後自動返回該模式的間隔,即可自動返回以下模式。

範例

以下程式碼範例會檢查使用者在導航模式中是否移動地圖。如果是的話,它會設定計時器,將相機模式切換為下列模式,並在五秒後將地圖置中。

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