如何获取从当前位置到下一步的距离、方向、持续时间?

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

我正在使用 Mapbox Android SDK

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

我之前在问过类似的问题,但还是有问题。拿到currentRoute不知道怎么实现。我的代码如下:

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() 将在 onLocationChanged() 内调用。我认为 MapBox SDK 应该将这些数据提供给开发者,而不是开发者自己实现。或者如果我错过了 SDK 中的一些重要信息?有什么建议吗?

希望您的应用进展顺利。我看到你试图获得下一步的方向、距离和持续时间。我会尽可能简短地回答这个问题。

方向
首先,当您请求路线时,您需要包含几行:

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

分别。我希望这至少有助于指导您在创建应用程序时朝着正确的方向发展。