相機可讓您變更使用者的地圖視角。您可以使用相機模式,控制地圖在導航期間的行為。如要設定相機模式,請呼叫下列任一與相機相關聯的方法:
追蹤我的位置 (
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
物件,在導覽期間覆寫縮放等級。如此一來,您就可以擴大縮放等級,在使用者接近目的地時顯示 2D 建築物輪廓。
以下範例會啟用高詳細資料設定:
navigationView.setNavigationMapStyle(NavigationMapStyle.HIGH_DETAIL);
以下範例會在導覽期間覆寫相機的縮放等級。縮放等級設為 15,足以顯示 2D 建築物的輪廓。
googleMap.followMyLocation(
FollowMyLocationOptions.builder(CameraPerspective.TILTED)
.setZoomLevel(15.0f)
.build());
後續步驟
請參閱「自訂導覽 UI」,瞭解如何透過決定要在地圖上顯示的內建 UI 元件,自訂使用者與地圖的互動方式。