调整相机

借助摄像头,您可以更改用户的 地图视角。您可以使用相机模式来控制 地图。要设置相机模式,请将 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