相機可讓你變更使用者地圖的視角。您可以在導航期間使用相機模式,控制地圖檢視畫面的行為。如要設定相機模式,請呼叫以下與相機關聯的方法:
追蹤我的位置 (
GoogleMap.followMyLocation
) — 導航的預設相機模式。這個模式會將攝影機設為裝置或車輛。導航期間,相機會自動面對行駛方向。 啟用高詳細設定 (NavigationMapStyle.HIGH_DETAIL
) 時,當縮放等級為 19 以上時,會顯示 2-D 建築物輪廓。固定在位置 (
GoogleMap.animateCamera
和GoogleMap.moveCamera
) - 修正攝影機在特定位置。使用這個模式時,您可以設定攝影機位置以及其他相機屬性,例如方位、傾斜和縮放等。選取該檢視畫面且導覽器初始化後,系統會顯示「Re-center」按鈕。顯示路線總覽 (
NavigationView.showRouteOverview
或SupportNavigationFragment.showRouteOverview
):顯示其餘路線的總覽,視需要平移及縮放,讓路線符合地圖檢視。選取這個檢視區塊後,畫面上會顯示「Re-center」按鈕。
按一下「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 以上時,就會顯示 2-D 建築物輪廓。您可以在導航期間使用 FollowMyLocationOptions
物件覆寫縮放等級。這可讓您增加縮放等級,以便在使用者接近目的地時顯示 2-D 建築物輪廓。
以下範例會啟用高詳細資料設定:
navigationView.setNavigationMapStyle(NavigationMapStyle.HIGH_DETAIL);
下列範例會覆寫導航期間相機的縮放等級。縮放等級設為 15,足以顯示 2-D 建築物輪廓。
googleMap.followMyLocation(
FollowMyLocationOptions.builder(CameraPerspective.TILTED)
.setZoomLevel(15.0f)
.build());
後續步驟
請參閱「自訂導覽 UI」,瞭解如何決定要在地圖上顯示的內建 UI 元件,自訂使用者與地圖的互動方式。