Configure speedometer alerts

When navigation is enabled and the travel mode is set to driving, the Navigation SDK for iOS displays a speed limit control in the lower left corner of the map that shows the current speed limit. If a driver exceeds the speed limit, the control expands to display a speedometer next to the speed limit display and triggers alerts when the speed reaches a certain threshold.

By default, the Navigation SDK triggers a minor speed alert when the driver exceeds the speed limit by 5 mph (or 10 kph), and changes the color of the speedometer text to red. It triggers a major speed alert when the driver exceeds the speed limit by 10 mph (or 20 kph), and changes the speedometer background color to red.

You can customize both the threshold for triggering the alerts and the text and background colors the speedometer displays. You can also use the Navigation SDK to make the driver's speed information available. For example, you could make speed information available to rideshare operators to help them encourage their drivers to adhere to the speed limit and improve safety.

Customizing thresholds for speed alerts

You can customize the speed alert threshold for both minor and major speed alerts as a percentage over the speed limit of the current speed. You can also specify how long the threshold is exceeded before the map displays the alert.

The following code example sets the threshold for a minor speed alert to five percent over the speed limit, and the threshold for a major speed alert to 10 percent over the speed limit. It specifies that the map displays an alert after an alert threshold has been exceeded for five seconds.

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.

Customizing how the speedometer displays speed alerts

You can customize the colors of the speedometer display for each alert level.

The following table shows the default colors for speed alerts in the GMSNavigationSpeedometerUIOptions class:

ElementColor
MinorSpeedAlertBackgroundColorDayMode 0xffffff(white)
MinorSpeedAlertBackgroundColorNightMode 0x000000
MinorSpeedAlertTextColorDayMode 0xd93025
MinorSpeedAlertTextColorNightMode 0xd93025
MajorSpeedAlertBackgroundColorDayMode 0xd93025
MajorSpeedAlertBackgroundColorNightMode 0xd93025
MajorSpeedAlertTextColorDayMode 0xffffff(white)
MajorSpeedAlertTextColorNightMode 0xffffff(white)

You can specify the text and background color of the speedometer for both minor and major speed alerts:

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;

Receiving speed information from drivers

If your application requires sharing information about driver speed, you can also use the Navigation SDK to make the driver's speed information available. This can be useful for rideshare applications in which an operator may want to monitor excessive speeding by drivers to improve safety.

For example, the following example shares speed information when the speed is a specified percentage over the speed limit:

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 { ... }