调整摄像头
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
借助镜头,您可以更改用户查看地图的视角。您可以使用相机模式来控制导航期间地图的行为。如需设置相机模式,请设置地图视图的 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;
自动将地图重新居中
当用户在导航模式下移动地图时,地图视图的摄像头模式会从跟随模式更改为自由模式。当用户明确按下重新居中时,摄像头会返回到跟随模式。您可以使用计时器设置从退出跟随模式到自动返回跟随模式的时间间隔,从而自动返回跟随模式。
示例
以下代码示例用于检查在导航模式下,地图是否正在被用户移动。如果为 true,则设置一个计时器,以在 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
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eYou can change the user's viewpoint of the map by setting the \u003ccode\u003ecameraMode\u003c/code\u003e property to \u003ccode\u003efollowing\u003c/code\u003e, \u003ccode\u003eoverview\u003c/code\u003e, or \u003ccode\u003efree\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eFollowing\u003c/code\u003e mode puts the camera behind the current position, \u003ccode\u003eoverview\u003c/code\u003e mode displays the entire route, and \u003ccode\u003efree\u003c/code\u003e mode allows user interaction.\u003c/p\u003e\n"],["\u003cp\u003eThe map automatically enters \u003ccode\u003efree\u003c/code\u003e mode when the user pans or zooms, and a \u003cstrong\u003eRe-center\u003c/strong\u003e button is available in \u003ccode\u003eoverview\u003c/code\u003e and \u003ccode\u003efree\u003c/code\u003e modes to return to \u003ccode\u003efollowing\u003c/code\u003e mode.\u003c/p\u003e\n"],["\u003cp\u003eYou can automate the return to \u003ccode\u003efollowing\u003c/code\u003e mode after a set time interval using a timer to improve user experience.\u003c/p\u003e\n"]]],[],null,["# Adjust the camera\n\nThe [camera](/maps/documentation/ios-sdk/views) allows you to change the user's\nviewpoint of the map. You can use camera modes to control the behavior of the\nmap during navigation. To set the camera mode, set the `cameraMode` property of\nthe map view, specifying one of the following camera mode constants:\n\n- Following --- The default camera mode for navigation. Changes the view\n angle to 45 degrees and puts the camera behind the current position facing in\n the direction of travel. During navigation the camera automatically adjusts\n to face in the direction of travel. Pressing the map's **Re-center** button\n will also switch to this mode. The **Re-center** button is not visible when\n this mode is selected.\n\n- Overview --- Displays an overview of the entire route, zooming as needed\n to fit the route into the map view. When this view is selected the\n **Re-center** button is visible.\n\n- Free --- Lets the user change the map view with [gestures](/maps/documentation/navigation/ios-sdk/controls).\n The camera remains stationary in this view. The map will automatically enter\n this view if the user pans or zooms during navigation. When this view is\n selected the **Re-center** button is visible.\n\nTo change the camera mode, set the `cameraMode` property of the map view, as\nshown here:\n\n\u003cbr /\u003e\n\n### Swift\n\n\u003cbr /\u003e\n\n // Set the mode to \"overview\":\n mapView.cameraMode = .overview\n\n // Set the mode to \"free\":\n mapView.cameraMode = .free\n\n // Set the mode to \"following\":\n mapView.cameraMode = .following\n\n\u003cbr /\u003e\n\n### Objective-C\n\n\u003cbr /\u003e\n\n // Set the mode to \"overview\":\n mapView.cameraMode = GMSNavigationCameraModeOverview;\n\n // Set the mode to \"free\":\n mapView.cameraMode = GMSNavigationCameraModeFree;\n\n // Set the mode to \"following\":\n mapView.cameraMode = GMSNavigationCameraModeFollowing;\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nAutomatically recenter the map\n------------------------------\n\nWhen users move the map in Navigation mode, the camera mode for the map\nview changes from following mode to free mode. The camera returns to following\nmode when the user explicitly presses **Re-center**. You can automate the\nreturn to following mode by using a timer to set an interval between leaving\nfollowing mode, and then automatically returning to it.\n\n### Example\n\nThe following code example checks to determine whether the map is being moved by\nthe user while in Navigation mode. If it is, then it sets a timer to switch the\ncamera mode to following mode, centering the map after five seconds.\n\n\u003cbr /\u003e\n\n### Swift\n\n\u003cbr /\u003e\n\n class YourViewController: UIViewController {\n\n @IBOutlet weak var mapView: GMSMapView!\n var autoFollowTimer: Timer!\n\n override func viewDidLoad() {\n super.viewDidLoad()\n mapView.delegate = self\n ...\n }\n\n ...\n }\n\n /** Implements the GMSMapViewDelegate protocol. */\n extension YourViewController: GMSMapViewDelegate {\n func mapView(_ mapView: GMSMapView, willMove gesture: Bool) {\n if mapView.navigator?.isGuidanceActive == false {return}\n if !gesture {return}\n\n autoFollowTimer?.invalidate()\n autoFollowTimer = Timer(\n timeInterval: TimeInterval(5.0),\n target: self,\n selector: #selector(recenterMap),\n userInfo: nil,\n repeats: false)\n RunLoop.current.add(autoFollowTimer, forMode: .default)\n }\n\n /** Centers the map in guidance mode. */\n @objc private func recenterMap() {\n if mapView.navigator?.isGuidanceActive == true {\n mapView.cameraMode = .following\n }\n\n autoFollowTimer.invalidate()\n autoFollowTimer = nil\n }\n }\n\n\u003cbr /\u003e\n\n### Objective-C\n\n\u003cbr /\u003e\n\n @interface YourViewController : UIViewController\u003cGMSMapViewDelegate\u003e\n ...\n @end\n\n\n @implementation YourViewController {\n GMSMapView *_mapView;\n NSTimer *_autoFollowTimer;\n ...\n }\n\n ...\n\n - (void)viewDidLoad {\n [super viewDidLoad];\n ...\n _mapView.delegate = self;\n ...\n }\n\n ...\n\n /** Implements the GMSMapViewDelegate protocol. */\n - (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture {\n if (!_mapView.navigator.guidanceActive) return;\n if (!gesture) return;\n\n [_autoFollowTimer invalidate];\n _autoFollowTimer = [NSTimer scheduledTimerWithTimeInterval:5.0\n target:self\n selector:@selector(recenterMap)\n userInfo:nil\n repeats:NO];\n }\n\n /** Centers the map in guidance mode. */\n - (void)recenterMap {\n if (_mapView.navigator.guidanceActive) {\n _mapView.cameraMode = GMSNavigationCameraModeFollowing;\n }\n\n [_autoFollowTimer invalidate];\n _autoFollowTimer = nil;\n }\n\n @end\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e"]]