How can I get the distance, direction, duration fr

2019-06-12 03:56发布

I'm using Mapbox Android SDK

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

I asked the similar question before at here, but still have some problem. I don't know how to implement when I get currentRoute. my code is as below:

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() would be called within onLocationChanged(). I think MapBox SDK should provide these data to developer instead of developer implement it by himself. or if I miss something important information in SDK? Any suggestion?

1条回答
聊天终结者
2楼-- · 2019-06-12 04:18

hope your app is coming along well. I see your trying to get direction, distance, and duration to next step. I'll try and answer this as best I can while keeping it short.

Direction
First when you request the route you need to include a couple lines:

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

Once you receive the response you can do something along the lines of

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

which will give you the approximate cardinal direction of travel following the maneuver. Typically one of the following: 'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', or 'NW'. This specific line gives you the first route in the list (also typically the shortest and best choice route) and the first step. To change through the steps you simply change the integer value of the second .get(int) to whatever step you need.

Duration and Distance
Same as above but instead of .getDirection() you use:

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

and

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

respectively. I hope this at least helps to guide you in the right direction while creating your app.

查看更多
登录 后发表回答