How can I get the distance, direction, duration from my current location to the next step?

I am using Mapbox Android SDK

compile ('com.mapbox.mapboxsdk:mapbox-android-sdk:3.0.0@aar').

I asked a similar question earlier here , but there are still some problems. I do not know how to implement when I get currentRoute. my code is as follows:

private Waypoint lastCorrectWayPoint;
private boolean checkOffRoute(Waypoint target) {
    boolean isOffRoute = false;
    if(currentRoute != null){
        if (currentRoute.isOffRoute(target)) {
            showMessage("You are off-route, recalculating...");
            isOffRoute = true;
            lastCorrectWayPoint = null;
            //would recalculating route.
        } else {
            lastCorrectWayPoint = target;
            String direction = "Turn right"; //The message what should I prompt to user
            double distance = 0.0;//The distance which from target to next step.
            int duration = 0;//The time which from target to next step.
            String desc = "Turn right to xx street.";
            //Implement logic to get them here.
            showMessage("direction:" + direction + ", distance:" + distance + ", duration:" + duration + ", desc:" + desc);
        }
    }

checkOffRoute()will be called inside onLocationChanged(). I think the MapBox SDK should provide this data to the developer, not the developer, to implement it on his own. or if I miss something important information in the SDK? Any suggestion?

0
source share
1 answer

, . , , . , .

Direction
, , :

MapboxDirections client = new MapboxDirections.Builder()
                .setAccessToken(getString(R.string.accessToken))
                .setOrigin(origin)
                .setDestination(destination)
                .setProfile(DirectionsCriteria.PROFILE_DRIVING)
                .setAlternatives(true) // Gives you more then one route if alternative routes available
                .setSteps(true) // Gives you the steps for each direction
                .setInstructions(true) // Gives human readable instructions
                .build();

, -

response.body().getRoutes().get(0).getSteps().get(0).getDirection()

. : "N", "NE", "E", "SE", "S", "SW", "W" "NW". ( ) . , .get(int) , .


, , .getDirection() :

 response.body().getRoutes().get(0).getSteps().get(0).getDuration()

response.body().getRoutes().get(0).getSteps().get(0).getDistance()

. , .

+3

Source: https://habr.com/ru/post/1623329/


All Articles