攝影機可讓您變更使用者查看地圖的視角。您可以使用攝影機模式,控制導航期間的地圖檢視行為。如要設定相機模式,請呼叫下列其中一種與相機相關的方法:
追蹤我的位置 (
GoogleMap.followMyLocation):導航的預設攝影機模式。這個模式會將攝影機設為裝置或車輛。導航時,攝影機會自動朝向行進方向。固定在特定位置 (
GoogleMap.animateCamera和GoogleMap.moveCamera):將攝影機固定在特定位置。使用這個模式時,您可以設定攝影機位置,以及其他攝影機屬性,例如方位、傾斜度和縮放。選取這個檢視畫面並初始化導覽器後,系統會顯示「Re-center」(重新置中) 按鈕。顯示路線總覽 (
NavigationView.showRouteOverview或NavigationFragment.showRouteOverview):顯示剩餘路線的總覽 (最多 45 分鐘的行車時間),並視需要平移及縮放,在地圖檢視畫面中顯示路線。選取這個檢視畫面時,系統會顯示「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 NavigationFragment 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 元件,請參閱「自訂導覽 UI」。