When navigation is enabled, the Navigation SDK for Android displays a speed limit control in the lower left corner of the map that shows the current speed limit. If a user 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 user 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 user 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 user's speed information available in accordance with the thresholds you have set. For example, you could make speed information available to rideshare operators to help them encourage their users to adhere to the speed limit and improve safety.
Customize 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.
float minorSpeedAlertThresholdPercentage = 5; float
majorSpeedAlertThresholdPercentage = 10; double severityUpgradeDurationSeconds =
5;
// Configure SpeedAlertOptions SpeedAlertOptions.Builder
speedAlertOptionsBuilder = new SpeedAlertOptions.Builder();
speedAlertOptionsBuilder.setSpeedAlertThresholdPercentage(
SpeedAlertSeverity.MINOR, minorSpeedAlertThresholdPercentage);
speedAlertOptionsBuilder.setSpeedAlertThresholdPercentage(
SpeedAlertSeverity.MAJOR, majorSpeedAlertThresholdPercentage);
speedAlertOptionsBuilder.setSeverityUpgradeDurationSeconds(severityUpgradeDurationSeconds);
// Set SpeedAlertOptions to Navigator.
navigator.setSpeedAlertOptions(speedAlertOptionsBuilder.build());
Customize how the speedometer displays speed alerts
To make speed alerts more attention-getting, 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
NavigationView
class:
Element | Color |
---|---|
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:
SpeedometerUiOptions speedometerUiOptions = new SpeedometerUiOptions.Builder()
.setBackgroundColorDayMode(MINOR, some_color)
.setBackgroundColorNightMode(MINOR, some_color) .setTextColorDayMode(MINOR,
some_color) .setTextColorNightMode(MINOR, some_color)
.setBackgroundColorDayMode(MAJOR, some_color)
.setBackgroundColorNightMode(MAJOR, some_color) .setTextColorDayMode(MAJOR,
some_color) .setTextColorNightMode(MAJOR, some_color) .build();
// Set SpeedometerUiOptions to NavigationView.
navigationView.setSpeedometerUiOptions(speedometerUiOptionsBuilder.build());
navigationView.setSpeedometerEnabled(true);
// Set SpeedometerUiOptions to SupportNavigationFragment.
supportNavigationFragment.setSpeedometerUiOptions(speedometerUiOptionsBuilder.build());
supportNavigationFragment.setSpeedometerEnabled(true);
Receive speed information from users
If your application requires sharing information about user speed, you can also use the Navigation SDK to make the user's speed information available. For example, this can be useful for rideshare applications in which an operator may want to monitor excessive speeding by users to improve safety. This can also be done without needing to render a navigation view to the user in your app.
For example, the following example shares speed information when the speed is a specified percentage over the speed limit:
// Existing flow for creating Navigator. NavigationApi.getNavigator();
// Set the SpeedAlertOptions for the MAJOR and MINOR alerts. (Note that the //
severityUpgradeDurationSeconds field is by design not used in this flow.)
SpeedAlertOptions speedAlertOptions = ...;
navigator.setSpeedAlertOptions(speedAlertOptions);
// Implement SpeedingListener. SpeedingListener speedingListener = new
SpeedingListener() { @Override public void onSpeedingUpdated(float
percentageAboveLimit, SpeedAlertSeverity speedAlertSeverity) { ... } };
// Set speedingListener to Navigator.
navigator.setSpeedingListener(speedingListener);