您可以透過攝影機變更使用者的地圖視角。你可以使用攝影機模式,控制導航期間的地圖檢視行為。如要設定攝影機模式,請呼叫與攝影機相關聯的下列其中一種方法:
追蹤我的位置 (
GoogleMap.followMyLocation
):導航的預設攝影機模式。這個模式會將攝影機設為裝置或車輛。 導航時,攝影機會自動朝向行進方向。固定在特定位置 (
GoogleMap.animateCamera
和GoogleMap.moveCamera
):將攝影機固定在特定位置。使用這個模式時,您可以設定攝影機位置,以及航向、傾斜、縮放等其他攝影機屬性。選取這個檢視畫面並初始化 Navigator 後,系統會顯示「重新置中」按鈕。顯示路線總覽 (
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();
下一步
請參閱「自訂導航使用者介面」,瞭解如何決定要在地圖上顯示哪些內建 UI 元件,藉此自訂使用者與地圖的互動方式。