Route progress
Once turn-by-turn navigation begins, your project will need to follow the user's progress along the route to deliver contextual information in the correct way at the correct time. The Navigation SDK's RouteProgress
object contains this information and this page teaches you about using it.
Levels of route progress
The Mapbox Navigation SDK's model for tracking route progress has three pieces with different levels of granularity: the route, the leg, and the step.
Route: The blue line is a route. A route stretches between the origin and the final destination.
Leg: The larger circles with a pink stroke represent waypoints, or stops, along the route. A leg is the part of the route between two waypoints.
Step: The smaller circles with a green stroke represent maneuvers. A step is the part of the leg between two maneuvers.
The Navigation SDK uses three classes to communicate information on the user's progress at these three different levels: RouteProgress
, RouteLegProgress
, and RouteStepProgress
.
RouteProgress
The RouteProgress
class contains all the progress information at any time during a navigation session. A new RouteProgress
object is generated whenever there's a new valid Location
update or if no new Location
update is received in the past second.
RouteProgress
information includes distance measurements, the percentage of the route that's been completed, the current step index, the remaining number of route legs, and much more. For further information on data encapsulated in RouteProgress
, you can view the API reference docs here.
RouteLegProgress
The RouteLegProgress
class is specific to the current leg the user is on. For further information on data encapsulated in RouteLegProgress
, you can view the API reference docs here.
RouteStepProgress
The RouteStepProgress
class is a progress object specific to the current step the user is on. For further information on data encapsulated in RouteStepProgress
, you can view the API reference docs here.
Route progress and UI components
Several of the Navigation SDK's default UI components use route progress data to present guidance information to your users. Here's how each component uses route progress data:
- Maneuvers: Invoke the Maneuver API when route progress changes to receive updated banner instruction data any time the route progress changes. Read more in the Maneuver instructions guide.
- Trip progress: Invoke the Trip Progress API when route progress changes to keep the estimated arrival time, distance remaining, and time remaining up to date as the user progresses along the route. Read more in the Trip progress guide.
- Route line: Invoke the Route Line API to render a route line on a map.
- Route arrow: Invoke the Route Arrow API when route progress changes to determine when to show an arrow on the route line.
- Buildings: Invoke the Buildings API when route progress changes to highlight the building upon arrival at a waypoint or final destination.
- Notifications: Invoke the
MapboxNavigation#updateNotification
when route progress changes to receive updated maneuver instructions when the app is running in background. Read more in the Notifications guide.
Beyond using route progress to power the UI components provided by the SDK, you can customize how your application behaves as the user progresses along the route by listening to progress change, determining the route progress state, and implementing custom code.
Listen to progress change
Tracking a user's progress along a route is key to providing helpful and prompt navigation instructions. Implement the Navigation SDK's RouteProgressObserver
interface to receive a RouteProgress
object every time the user's location changes.
The RouteProgressObserver
can typically be used to refresh most of your application's user interface when a change occurs. For example, if you're displaying the user's current progress until the user needs to do the next maneuver. Every time this interface's onRouteProgressChanged()
method fires, you can update your view with the new information inside the RouteProgress
object.
private val routeProgressObserver = object : RouteProgressObserver {
override fun onRouteProgressChanged(routeProgress: RouteProgress) {
}
}
If you've created your own RouteProgressObserver
object, you'll need to:
- Register the
RouteProgressObserver
with your already-instantiatedMapboxNavigation
object.
mapboxNavigation.registerRouteProgressObserver(routeProgressObserver)
- Don’t forget to unregister the observer with
mapboxNavigation.unregisterRouteProgressObserver(routeProgressObserver)
. This line isn't needed if you're already runningMapboxNavigation
'sonDestroy()
method, which automatically unregisters the observer for you.
mapboxNavigation.unregisterRouteProgressObserver(routeProgressObserver)