您可以使用镜头更改用户查看地图的视角。您可以使用相机模式来控制地图视图在导航期间的行为。如需设置相机模式,请调用以下与相机关联的方法之一:
“遵循我的位置”(
GoogleMap.followMyLocation
) - 默认的相机导航模式。此模式可将摄像头设置为设备或车辆。 在导航期间,相机会自动朝向行进方向。 启用“高细节”设置 (NavigationMapStyle.HIGH_DETAIL
) 后,当缩放级别为 19 或更高时,系统会显示 2-D 建筑物轮廓。固定到位置(
GoogleMap.animateCamera
和GoogleMap.moveCamera
)- 将相机固定在特定位置。使用此模式时,您可以设置镜头位置以及其他镜头属性(例如方位、倾斜度、缩放比例等)。选择此视图且初始化导航器后,系统会显示重新居中按钮。显示路线概览(
NavigationView.showRouteOverview
或SupportNavigationFragment.showRouteOverview
)- 显示剩余路线的概览,根据需要进行平移和缩放,以使路线适合地图视图。选择该视图后,系统会显示重新居中按钮。
点击 Re-center 按钮可将镜头设为 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();
“高细节”设置
启用“高细节”设置后,当相机的缩放级别设置为 19 或更高时,系统会显示 2D 建筑物轮廓。您可以在导航期间使用 FollowMyLocationOptions
对象替换缩放级别。这样,您就可以增加到足够高的缩放级别,以便在用户接近目的地时显示 2-D 建筑物轮廓。
以下示例启用了高细节设置:
navigationView.setNavigationMapStyle(NavigationMapStyle.HIGH_DETAIL);
以下示例替换了导航期间相机的缩放级别。缩放级别设置为 15,这已经足够显示 2-D 建筑物轮廓。
googleMap.followMyLocation(
FollowMyLocationOptions.builder(CameraPerspective.TILTED)
.setZoomLevel(15.0f)
.build());
后续步骤
如需了解如何通过确定在地图上显示哪些内置界面组件,来自定义用户与地图互动的方式,请参阅自定义导航界面。