如何在 Mapbox Android 中退出时重新计算路线?

How to recalculate the route when exiting it in Mapbox Android?

我想知道如何重新计算 Android 地图框中的路线,因为离开的路线与最初绘制的路线相同。对于破折号路线,开始导航,甚至离开导航和路线都不会重新计算。谢谢大家。

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());


}

Navigation SDK 包含 OffRouteListener,默认情况下 启用。

文档详细介绍了如何检测用户何时离开初始路线并触发对新路线的请求:https://docs.mapbox.com/android/navigation/overview/off-route/


⚠️ 免责声明:我目前在 Mapbox 工作 ⚠️