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

use*_*559 2 java android mapbox

我正在使用 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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

checkOffRoute()将在onLocationChanged(). 我认为 MapBox SDK 应该将这些数据提供给开发人员,而不是开发人员自己实现。或者如果我错过了 SDK 中的一些重要信息?有什么建议吗?

cam*_*ace 7

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

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

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();
Run Code Online (Sandbox Code Playgroud)

收到回复后,您可以执行以下操作

response.body().getRoutes().get(0).getSteps().get(0).getDirection()
Run Code Online (Sandbox Code Playgroud)

这将为您提供机动后大致的主要行进方向。通常是以下之一:“N”、“NE”、“E”、“SE”、“S”、“SW”、“W”或“NW”。此特定线路为您提供列表中的第一条路线(通常也是最短和最佳选择路线)和第一步。要更改这些步骤,您只需将第二个的整数值更改为.get(int)您需要的任何步骤。

持续时间和距离
与上述相同,但.getDirection()您使用:

 response.body().getRoutes().get(0).getSteps().get(0).getDuration()
Run Code Online (Sandbox Code Playgroud)

response.body().getRoutes().get(0).getSteps().get(0).getDistance()
Run Code Online (Sandbox Code Playgroud)

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