카메라 조정

카메라를 사용하면 지도의 사용자 시점을 변경할 수 있습니다. 카메라 모드를 사용하여 내비게이션 중에 지도의 동작을 제어할 수 있습니다. 카메라 모드를 설정하려면 지도 뷰의 cameraMode 속성을 설정하여 다음 카메라 모드 상수 중 하나를 지정합니다.

  • 따라가기: 내비게이션의 기본 카메라 모드입니다. 시야각을 45도로 변경하고 카메라를 현재 위치 뒤에 배치하여 이동 방향을 향하게 합니다. 내비게이션 중에 카메라는 이동 방향을 향하도록 자동으로 조정됩니다. 지도의 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를 누르면 카메라가 추적 모드로 돌아갑니다. 타이머를 사용하여 팔로우 모드를 종료한 후 자동으로 팔로우 모드로 돌아가는 간격을 설정하면 팔로우 모드로 돌아가는 과정을 자동화할 수 있습니다.

예

다음 코드 예에서는 탐색 모드에서 사용자가 지도를 이동하고 있는지 확인합니다. 그렇다면 타이머를 설정하여 카메라 모드를 추적 모드로 전환하고 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