启用导航功能并将行驶模式设为“驾车”后,Navigation SDK for iOS 会在地图的左下角显示一个限速控件,用于显示当前限速。如果驾驶员超速,该控件会展开,在限速显示屏旁边显示速度计,并在速度达到特定阈值时触发提醒。
默认情况下,当驾驶员超速 5 英里/小时(或 10 公里/小时)时,Navigation SDK 会触发轻微超速提醒,并将速度表文本的颜色更改为红色。当驾驶员超速超过 10 英里/小时(或 20 公里/小时)时,系统会触发重大超速提醒,并将速度表背景颜色更改为红色。
您可以自定义触发提醒的阈值,以及速度表显示的文本和背景颜色。您还可以使用 Navigation SDK 提供驾驶员的速度信息。例如,您可以向共享车辆运营商提供速度信息,帮助他们鼓励司机遵守限速并提高安全性。
自定义速度提醒的阈值
您可以将轻微和严重速度提醒的速度提醒阈值自定义为当前速度上限的百分比。您还可以指定地图在超出阈值后显示提醒的时长。
以下代码示例将轻微超速提醒的阈值设置为超出限速 5%,将严重超速提醒的阈值设置为超出限速 10%。它指定地图会在超出提醒阈值 5 秒后显示提醒。
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 { ... }