Điều chỉnh camera

Máy ảnh cho phép bạn thay đổi điểm nhìn trên bản đồ của người dùng. Bạn có thể sử dụng các chế độ máy ảnh để kiểm soát hành vi của bản đồ trong khi điều hướng. Để đặt chế độ máy ảnh, hãy đặt thuộc tính cameraMode của chế độ xem bản đồ, chỉ định một trong các hằng số chế độ máy ảnh sau:

  • Đang theo dõi – Chế độ máy ảnh mặc định để điều hướng. Thay đổi góc xem thành 45 độ và đặt máy ảnh phía sau vị trí hiện tại hướng theo hướng di chuyển. Trong khi di chuyển, máy ảnh sẽ tự động điều chỉnh theo khuôn mặt theo hướng di chuyển. Việc nhấn nút Căn giữa lại của bản đồ cũng sẽ chuyển sang chế độ này. Nút Căn giữa lại sẽ không xuất hiện khi chọn chế độ này.

  • Overview (Tổng quan) – Cho thấy thông tin tổng quan về toàn bộ tuyến đường, thu phóng nếu cần để tuyến đường đó phù hợp với chế độ xem bản đồ. Khi khung hiển thị này được chọn, nút Re-center (Căn giữa lại) sẽ xuất hiện.

  • Miễn phí — Cho phép người dùng thay đổi chế độ xem bản đồ bằng cử chỉ. Máy ảnh vẫn đứng yên ở chế độ xem này. Bản đồ sẽ tự động vào chế độ xem này nếu người dùng xoay hoặc thu phóng trong khi điều hướng. Khi khung hiển thị này được chọn, nút Re-center (Căn giữa lại) sẽ xuất hiện.

Để thay đổi chế độ máy ảnh, hãy đặt thuộc tính cameraMode của chế độ xem bản đồ, như minh hoạ dưới đây:

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;

Tự động căn giữa bản đồ

Khi người dùng di chuyển bản đồ ở Chế độ chỉ đường, chế độ máy ảnh cho chế độ xem bản đồ sẽ thay đổi từ chế độ sau đây sang chế độ tự do. Máy ảnh sẽ quay về chế độ sau khi người dùng nhấn Căn giữa lại một cách rõ ràng. Bạn có thể tự động hoá việc quay lại chế độ theo dõi bằng cách sử dụng bộ tính giờ để đặt một khoảng thời gian giữa thời điểm rời khỏi chế độ theo dõi rồi tự động quay lại chế độ đó.

Ví dụ:

Mã ví dụ sau đây sẽ kiểm tra để xác định xem người dùng có đang di chuyển bản đồ khi ở Chế độ chỉ đường hay không. Nếu đúng, ứng dụng sẽ đặt bộ tính giờ để chuyển chế độ máy ảnh sang chế độ theo dõi, căn giữa bản đồ sau 5 giây.

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