Navigation SDK for iOS 不會自動配合 iOS 系統的淺色和深色模式。
而是透過導覽功能啟用
GMSMapView,並透過 lightingMode 屬性和 GMSNavigationLightingMode 列舉支援一般和低光源色彩配置:
啟用自動切換照明模式
Navigation SDK for iOS 會根據時間和裝置位置計算建議的照明模式,並透過 GMSNavigator 的 suggestedLightingMode 屬性公開。不過,
GMSMapView
不會自動套用這項最佳化建議。
如要自動切換一般和低光源模式,請在檢視畫面控制器中完成下列步驟:
- 在啟動時設定初始照明模式:設定地圖時,請從
GMSNavigator讀取suggestedLightingMode屬性,並將該值指派給mapView.lightingMode。由於只有在建議的照明模式變更時 (例如日出或日落時),監聽器才會觸發,因此設定初始值可確保使用者在夜間開始導航時,地圖會立即以低光源模式算繪。 - 在導覽期間監聽照明模式變化:採用
GMSNavigatorListener協定,並實作navigator(_:didChangeSuggestedLightingMode:)方法,在建議模式變更時更新mapView.lightingMode。
以下範例說明如何設定初始照明模式,並在建議的照明模式變更時更新:
Swift
// Step 1: Set the initial lighting mode on launch or setup.
mapView.navigator?.add(self)
mapView.lightingMode = mapView.navigator?.suggestedLightingMode ?? .normal
// Step 2: Update the lighting mode when the sun sets or rises during navigation.
func navigator(
_ navigator: GMSNavigator,
didChangeSuggestedLightingMode lightingMode: GMSNavigationLightingMode
) {
mapView.lightingMode = lightingMode
}
Objective-C
// Step 1: Set the initial lighting mode on launch or setup.
[_mapView.navigator addListener:self];
_mapView.lightingMode = _mapView.navigator.suggestedLightingMode;
// Step 2: Update the lighting mode when the sun sets or rises during navigation.
- (void)navigator:(GMSNavigator *)navigator
didChangeSuggestedLightingMode:(GMSNavigationLightingMode)lightingMode {
_mapView.lightingMode = lightingMode;
}
如要進一步瞭解如何採用 GMSNavigatorListener,請參閱「Listen for navigation events」(監聽導覽事件) 指南中的「宣告符合必要通訊協定」。
強制使用一般或低光源模式
如果不想讓地圖自動切換模式,可以設定 mapView.lightingMode 直接強制使用一般或低光源模式,並省略 navigator(_:didChangeSuggestedLightingMode:) 監聽器更新。
以下範例說明如何強制地圖進入低光源或一般模式:
Swift
// Force low-light mode.
mapView.lightingMode = .lowLight
// Or force normal (daylight) mode.
mapView.lightingMode = .normal
Objective-C
// Force low-light mode.
_mapView.lightingMode = GMSNavigationLightingModeLowLight;
// Or force normal (daylight) mode.
_mapView.lightingMode = GMSNavigationLightingModeNormal;
自訂低光源 UI 顏色
此外,地圖處於低光源模式時,您也可以自訂導覽標題的主要和次要背景顏色。如要進一步瞭解如何自訂這些顏色,請參閱「修改導覽 UI」指南中的「低光源模式標題顏色」。