I would like to know how to recalculate the route in the Android mapbox, since leaving the route continues the same initially mapped. For dash the route, start the navigation and even left it both the navigation and the route are not recalculated. I thank everyone.
public void getRoute(List<Point> waypoints){
waypointSize = waypoints.size();
Log.d("tag", "WaypointSSize: "+waypoints.size());
// Transforma localizacao atual em Point para o MapBox
Point origin = Point.fromLngLat(currentLocation.getLongitude(),currentLocation.getLatitude());
Log.d("tag", "Origin: "+origin);
for (Point destination : waypoints){
// Criando a rota -> Token tem que ser AccessToken
NavigationRoute.Builder builder = NavigationRoute.builder(this)
.accessToken("sk.eyJ1IjoiYWhlbnRyZWdhIiwiYSI6ImNqc2NkZ2ptZjAwYmEzenA2Zmo0MDBoNjQifQ.0r13_lXZMCNdaWrkYddRLg")
.origin(origin)
.destination(destination);
builder.build().getRoute(new Callback<DirectionsResponse>() {
@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
Log.d("tag", ""+response.code());
// Verificando se a problema como o AccessToken
if (response.body() == null){
Log.d("tag","Nenhuma rota encontrada, check access token");
return;
// Verificando se nao foi encontrada nenhuma rota
}else if(response.body().routes().size() < 1){
Log.d("tag","Nenhuma rota encontrada");
return;
}
// Adicionando a rota a variavel do MapBox
currentRoute = response.body().routes().get(0);
if (distancia == 0){
distancia = currentRoute.distance();
route = currentRoute;
}else if (distancia > currentRoute.distance()){
distancia = currentRoute.distance();
route = currentRoute;
}
cont++;
if (cont == waypointSize){
// Verificando se ja existe uma rota no mapa
if (navigationMapRoute != null) {
navigationMapRoute.removeRoute();
} else {
navigationMapRoute = new NavigationMapRoute(null, mapView, map);
}
// Adicionando a rota no mapa
navigationMapRoute.addRoute(route);
navigation.startNavigation(route);
// LatLng para marcar no mapa o destino
LatLng latLng = new LatLng(destination.latitude(),destination.longitude());
map.addMarker(new MarkerOptions().setPosition(latLng));
}
Log.d("tag", ""+currentRoute.distance());
Log.d("tag", ""+currentRoute.duration());
}
@Override
public void onFailure(Call<DirectionsResponse> call, Throwable t) {
Log.d("Rota",t.getMessage());
}
});
}
Log.d("Rota", "WaypointSSize: "+waypoints.size());
}
The Navigation SDK includes an OffRouteListener
that is not enabled by default.
The documentation goes into detail about how to detect when a user is off the initial route and trigger a request for a new route: https://docs.mapbox.com/android/navigation/overview/off-route/
⚠️ Disclaimer: I currently work for Mapbox ⚠️