設定速度計快訊

啟用導航功能並將行駛模式設為「駕駛」後,iOS 版 Navigation SDK 會在地圖的左下角顯示速限控制項,顯示目前的速限。如果駕駛人超速,控制項會展開,在速限顯示器旁顯示計速器,並在車速達到特定門檻時觸發警報。

根據預設,當駕駛人超速 5 英里/小時 (或 10 公里/小時) 時,Navigation SDK 會觸發輕微超速警報,並將車速表文字顏色變更為紅色。當駕駛人超速 10 英里/小時 (或 20 公里/小時) 時,這項功能會觸發重大超速警報,並將車速表背景顏色變更為紅色。

您可以自訂觸發快訊的門檻,以及車速表顯示的文字和背景顏色。您也可以使用 Navigation SDK 提供駕駛人的車速資訊。舉例來說,您可以向共乘服務營運商提供車速資訊,協助他們鼓勵司機遵守速限,提升行車安全。

自訂速度快訊門檻

您可以自訂速度快訊門檻,以目前車速的速度限制百分比做為次要和主要速度快訊的門檻。您也可以指定地圖在超過閾值後,要過多久才會顯示快訊。

以下程式碼範例會將小型超速警報的門檻設為超速上限的 5%,將大型超速警報的門檻設為超速上限的 10%。這項設定會指定地圖在超過警示門檻後五秒,顯示警示。

Swift

let minorSpeedAlertThresholdPercentage: CGFloat = 0.05 let
majorSpeedAlertThresholdPercentage: CGFloat = 0.1 let
severityUpgradeDurationSeconds: TimeInterval = 5

// Configure SpeedAlertOptions let mutableSpeedAlertOptions:
GMSNavigationMutableSpeedAlertOptions = GMSNavigationMutableSpeedAlertOptions()
 mutableSpeedAlertOptions.setSpeedAlertThresholdPercentage(minorSpeedAlertThresholdPercentage,
for: .minor)
mutableSpeedAlertOptions.setSpeedAlertThresholdPercentage(majorSpeedAlertThresholdPercentage,
for: .major) mutableSpeedAlertOptions.severityUpgradeDurationSeconds =
severityUpgradeDurationSeconds

// Set SpeedAlertOptions to Navigator. mapView.navigator?.speedAlertOptions =
mutableSpeedAlertOptions; mapView.navigator?.add(self); // Only needed if
listening to the delegate events.

Objective-C

static const CGFloat minorSpeedAlertThresholdPercentage = 0.05; static const
CGFloat majorSpeedAlertThresholdPercentage = 0.1; static const NSTimeInterval
severityUpgradeDurationSeconds = 5;

// Configure SpeedAlertOptions GMSNavigationMutableSpeedAlertOptions
*mutableSpeedAlertOptions = [[GMSNavigationMutableSpeedAlertOptions alloc]
init]; [mutableSpeedAlertOptions setSpeedAlertThresholdPercentage:
minorSpeedAlertThresholdPercentage
forSpeedAlertSeverity:GMSNavigationSpeedAlertSeverityMinor];
[mutableSpeedAlertOptions
setSpeedAlertThresholdPercentage:majorSpeedAlertThresholdPercentage
forSpeedAlertSeverity:GMSNavigationSpeedAlertSeverityMajor];
[mutableSpeedAlertOptions
setSeverityUpgradeDurationSeconds:severityUpgradeDurationSeconds];

// Set SpeedAlertOptions to Navigator. mapView.navigator.speedAlertOptions =
mutableSpeedAlertOptions; [mapView.navigator addListener:self]; // Only needed
if listening to the delegate events.

自訂計速器顯示速度快訊的方式

您可以為每個警示層級自訂車速表顯示的顏色。

下表顯示 GMSNavigationSpeedometerUIOptions 類別中速度快訊的預設顏色:

元素顏色
MinorSpeedAlertBackgroundColorDayMode 0xffffff(白色)
MinorSpeedAlertBackgroundColorNightMode 0x000000
MinorSpeedAlertTextColorDayMode 0xd93025
MinorSpeedAlertTextColorNightMode 0xd93025
MajorSpeedAlertBackgroundColorDayMode 0xd93025
MajorSpeedAlertBackgroundColorNightMode 0xd93025
MajorSpeedAlertTextColorDayMode 0xffffff(白色)
MajorSpeedAlertTextColorNightMode 0xffffff(白色)

您可以指定車速表的文字和背景顏色,用於顯示輕微和嚴重速度警示:

Swift

let mutableSpeedometerUIOptions: GMSNavigationMutableSpeedometerUIOptions =
GMSNavigationMutableSpeedometerUIOptions()
mutableSpeedometerUIOptions.setTextColor(minorSpeedAlertTextColor, for: .minor,
lightingMode: .normal)
mutableSpeedometerUIOptions.setTextColor(majorSpeedAlertTextColor, for: .major,
lightingMode: .normal)
mutableSpeedometerUIOptions.setBackgroundColor(minorSpeedAlertNightModeBackgroundColor,
for: .minor, lightingMode: .lowLight)
mutableSpeedometerUIOptions.setBackgroundColor(majorSpeedAlertDayModeBackgroundColor,
for: .major, lightingMode: .normal)

mapView.settings.speedometerUIOptions = mutableSpeedometerUIOptions

Objective-C

GMSNavigationMutableSpeedometerUIOptions *mutableSpeedometerUIOptions =
[[GMSNavigationMutableSpeedometerUIOptions alloc] init];
[mutableSpeedometerUIOptions setTextColor: minorSpeedAlertTextColor
forSpeedAlertSeverity: GMSNavigationSpeedAlertSeverityMinor lightingMode:
GMSNavigationLightingModeNormal]; [mutableSpeedometerUIOptions setTextColor:
majorSpeedAlertTextColor forSpeedAlertSeverity:
GMSNavigationSpeedAlertSeverityMajor lightingMode:
GMSNavigationLightingModeNormal]; [mutableSpeedometerUIOptions
setBackgroundColor: minorSpeedAlertNightModeBackgroundColor
forSpeedAlertSeverity: GMSNavigationSpeedAlertSeverityMinor lightingMode:
GMSNavigationLightingModeLowLight]; [mutableSpeedometerUIOptions
setBackgroundColor: majorSpeedAlertDayModeBackgroundColor forSpeedAlertSeverity:
GMSNavigationSpeedAlertSeverityMajor
lightingMode:GMSNavigationLightingModeNormal];

mapView.settings.speedometerUIOptions = mutableSpeedometerUIOptions;

接收駕駛人的車速資訊

如果應用程式需要分享駕駛人速度的資訊,您也可以使用 Navigation SDK 提供駕駛人的速度資訊。這項功能對共乘服務應用程式來說相當實用,因為營運商可能會想監控駕駛人的超速行為,以提升安全性。

例如,當車速超過速限的特定百分比時,以下範例會分享車速資訊:

Swift

// Listener method for sharing speed information when the speed exceeds the
speed limit by a specified percentage. #pragma mark GMSNavigatorListener func
navigator(_ navigator : GMSNavigator, didUpdateSpeedingPercentage
percentageAboveLimit : Float) { ... }

Objective-C

// Listener method listening to speeding feed. #pragma mark
GMSNavigatorListener - (void)navigator:(GMSNavigator *)navigator
didUpdateSpeedingPercentage:(float)percentageAboveLimit { ... }