카메라를 사용하면 사용자가 지도를 바라보는 시점
을 변경할 수 있습니다. 카메라 모드를 사용하여 내비게이션 중에
지도의 동작을 제어할 수 있습니다. 카메라 모드를 설정하려면 다음 카메라 모드 상수 중 하나를 지정하여 지도 뷰의 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