调整相机

您可以使用镜头更改用户的地图视角。您可以 使用相机模式来控制导航期间地图视图的行为。 要设置相机模式,请调用以下关联的 使用相机:

  • 跟踪我的位置 (GoogleMap.followMyLocation) - 默认摄像头 模式。此模式会将摄像头设为设备或车辆。 在导航期间,相机会自动朝向行进方向。 启用高细节设置 (NavigationMapStyle.HIGH_DETAIL) 后, 当缩放级别为 19 或更高时,系统会显示二维建筑物轮廓。

  • 已固定到地点(GoogleMap.animateCameraGoogleMap.moveCamera) – 将镜头固定在特定位置。使用此模式时,您可以 设置镜头位置以及方位、倾斜度等其他镜头属性 选择此视图并初始化导航器后, 系统会显示重新居中按钮。

  • 显示路线概览(NavigationView.showRouteOverviewSupportNavigationFragment.showRouteOverview) - 显示概览 根据需要平移和缩放以适应路线 进入地图视图选择此视图后,点击重新居中按钮 可见。

点击重新居中按钮可将镜头设置为 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();

高细节设置

启用高细节设置后,系统会显示 2D 建筑物轮廓 将镜头缩放级别设置为 19 或更高。您可以替换 使用 FollowMyLocationOptions 对象在导航期间实现缩放。这个 增大到足以显示 2D 建筑轮廓 当用户接近其目的地时。

下例启用了高细节设置:

  navigationView.setNavigationMapStyle(NavigationMapStyle.HIGH_DETAIL);

以下示例在导航期间替换了相机的缩放级别。 缩放级别设置为 15,此级别足以显示 2D 建筑 大纲。

  googleMap.followMyLocation(
              FollowMyLocationOptions.builder(CameraPerspective.TILTED)
                      .setZoomLevel(15.0f)
                      .build());

下一步

请参阅自定义导航界面 了解如何自定义用户与地图的互动方式 方法是确定地图上显示哪些内置界面组件。