I successfully start Mapbox navigation UI using this line of code:
NavigationLauncher.startNavigation(MainActivity.this, options);
But I want to start a navigation session using a MapboxNavigation
object so that I can implement listeners EG: the OffRouteListener
.
To do so I use the following code, but no navigation UI shows up.
I'm not even sure that MapboxNavigation#startNavigation
is supposed to start any visual component.
MapboxNavigation mapboxNavigation = new MapboxNavigation(MainActivity.this, accessToken, mapboxNavigationOptions);
mapboxNavigation.addOffRouteListener(location -> {
// do things
});
mapboxNavigation.startNavigation(directionsRoute);
This example in the Mapbox Android documentation shows how to start a visual navigation component using MapboxNavigation#startNavigation
, in conjunction with an OffRouteListener
. The relevant section of the source code (also linked here) is:
@OnClick(R.id.startRouteButton)
public void onStartRouteClick() {
boolean isValidNavigation = navigation != null;
boolean isValidRoute = route != null && route.distance() > TWENTY_FIVE_METERS;
if (isValidNavigation && isValidRoute) {
// Hide the start button
startRouteButton.setVisibility(View.INVISIBLE);
// Attach all of our navigation listeners.
navigation.addNavigationEventListener(this);
navigation.addProgressChangeListener(this);
navigation.addMilestoneEventListener(this);
navigation.addOffRouteListener(this);
((ReplayRouteLocationEngine) locationEngine).assign(route);
navigation.setLocationEngine(locationEngine);
mapboxMap.getLocationComponent().setLocationComponentEnabled(true);
navigation.startNavigation(route);
mapboxMap.removeOnMapClickListener(this);
}
}
The navigation
variable is initialized here as an instance of MapboxNavigation
.
So, adding the lines
((ReplayRouteLocationEngine) locationEngine).assign(route);
mapboxNavigation.setLocationEngine(locationEngine);
mapboxMap.getLocationComponent().setLocationComponentEnabled(true);
Before calling mapboxNavigation.startNavigation(directionsRoute);
in your implementation should do the trick. You should also ensure that the directionsRoute
object is a correctly formulated instance of the DirectionsRoute
class.