借助摄像头,您可以更改用户查看地图的视角。您可以使用摄像头模式来控制导航期间地图视图的行为。 如需设置摄像头模式,请调用与摄像头关联的以下方法之一:
关注我的位置 (
GoogleMap.followMyLocation):导航的默认摄像头模式。此模式会将摄像头设置为设备或车辆。 在导航期间,摄像头会自动面向行驶方向。固定到位置 (
GoogleMap.animateCamera和GoogleMap.moveCamera):将摄像头固定在特定位置。使用此模式时,您可以设置摄像头位置以及其他摄像头属性,例如方位角、倾斜度、缩放级别等。选择此视图并初始化导航器后,重新居中 按钮会显示出来。显示路线概览 (
NavigationView.showRouteOverview或SupportNavigationFragment.showRouteOverview):显示剩余路线的概览(最长为接下来的 45 分钟车程),并根据需要平移和缩放以将路线放入地图视图中。选择此视图后,重新居中 按钮会显示出来。
点击重新居中 按钮会将摄像头设置为 followMyLocation 模式。
关注我的位置模式
最常见的摄像头设置是将摄像头设置为设备或车辆,以显示其在行程中的当前位置。在此摄像头模式下,您可以查看路线,让汽车始终朝上显示在屏幕上,并采用倾斜视角 (CameraPerspective.TILTED);也可以让汽车始终朝北 (CameraPerspective.TOP_DOWN_NORTH_UP) 或朝向 (CameraPerspective.TOP_DOWN_HEADING_UP) 显示在屏幕顶部。
以下代码段使用了 TILTED 视角:
// Set the camera to follow the device (vehicle):
mNavFragment.getMapAsync(googleMap -> googleMap.followMyLocation(CameraPerspective.TILTED))
固定到位置模式
Pinned 模式可让您对摄像头进行最大程度的控制。在此模式下,您可以将摄像头放置在特定位置,分配方位角以确定摄像头画面方向,更改倾斜度以设置视角,并设置摄像头的缩放级别。
以下代码段展示了移动摄像头的一些常用方法。
private static final LatLng SYDNEY = new LatLng(-33.88, 151.21);
private static final LatLng MOUNTAIN_VIEW = new LatLng(37.4, -122.1);
private GoogleMap map;
... // Obtain the map from a SupportNavigationFragment or NavigationView.
// Move the camera instantly to Sydney with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomIn());
// Zoom out to zoom level 10, animating with a duration of 2 seconds.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
// Construct a CameraPosition focusing on Mountain View and animate the camera to that position.
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(MOUNTAIN_VIEW) // Sets the center of the map to Mountain View
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
显示路线概览模式
showRouteOverview 摄像头设置会显示整个行程。对于多目的地行程,此模式会显示路线中尚未行驶的部分。
// Place the camera to see the remaining route:
mNavFragment.showRouteOverview();
下一步
如需了解如何通过确定将哪些内置界面组件显示在地图上,自定义用户与地图的互动方式,请参阅自定义导航界面 。