Arrival detection
In turn-by-turn applications using the Mapbox Navigation SDK, the SDK detects when a user arrives at a waypoint or a final destination and allows you create an arrival experience. There are few ways you can implement the experience with Navigation SDK or customize it based on your requirements.
Listen for arrival events
The ArrivalObserver
interface is a callback, which can be used to receive the following events:
onWaypointArrival
is triggered when RouteProgress.currentState is equal to RouteProgressState.COMPLETE. The event is triggered once per route leg, when there are multiple legs to navigate. If we are on the last leg of the routeonFinalDestinationArrival
is triggered instead.onNextRouteLegStart
is triggered once the driver has arrived at a stop and has started navigating the next leg.onFinalDestinationArrival
is triggered when RouteProgress.currentState is equal to RouteProgressState.COMPLETE. The event is triggered once when there are no more route legs to navigate.
Create an instance of ArrivalObserver
Instantiate ArrivalObserver
in your Activity
or Fragment
.
val arrivalObserver = object : ArrivalObserver() {
override fun onWaypointArrival(routeProgress: RouteProgress) {
// do something when the user arrives at a waypoint
}
override fun onNextRouteLegStart(routeLegProgress: RouteLegProgress) {
// do something when the user starts a new leg
}
override fun onFinalDestinationArrival(routeProgress: RouteProgress) {
// do something when the user reaches the final destination
}
}
Start and stop receiving arrival events
Register and unregister the ArrivalObserver
interface with your already-instantiated MapboxNavigation
object at appropriate places.
override fun onStart() {
super.onStart()
mapboxNavigation.registerArrivalObserver(arrivalObserver)
}
override fun onStop() {
super.onStop()
mapboxNavigation.unregisterArrivalObserver(arrivalObserver)
}
Customize arrival events
The Mapbox Navigation SDK allows you to customize when to trigger the onNextRouteLegStart
event when navigating waypoints with multiple legs. You can customize the experience by implementing ArrivalController
.
The Mapbox Navigation SDK includes a default implementation of ArrivalController
known as AutoArrivalController
and will automatically decide when to trigger onNextRouteLegStart
event.
To use your custom controller, create a new ArrivalController
.
val myArrivalController = object : ArrivalController() {
override fun navigateNextRouteLeg(routeLegProgress: RouteLegProgress): Boolean {
}
}
You can then pass your controller to your already-instantiated MapboxNavigation
object.
mapboxNavigation.setArrivalController(myArrivalController)
When your controller navigateNextRouteLeg
returns true
, the navigator will start navigating to the next stop by triggering arrivalObserver.onNextRouteLegStart. When your controller navigateNextRouteLeg
returns false
the navigator will not start navigating to the next stop.
If in any situation your controller navigateNextRouteLeg
returns false always, you can force trigger navigateNextRouteLeg
to continue navigation.
mapboxNavigation.navigateNextRouteLeg()