如何获得从当前位置到下一步的距离,方向,持续时间? [英] How can I get the distance, direction, duration from my current location to next step?

查看:127
本文介绍了如何获得从当前位置到下一步的距离,方向,持续时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mapbox Android SDK

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

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?

推荐答案

希望您的应用程序进展顺利.我看到您正在尝试确定下一步的方向,距离和持续时间.在保持简短的同时,我会尽力回答.

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

,将为您提供该操作后的基本行进方向.通常为以下之一:'N','NE','E','SE','S','SW','W'或'NW'.此特定行为您提供列表中的第一条路线(通常也是最短和最佳选择的路线)和第一步.要更改这些步骤,只需将第二个.get(int)的整数值更改为所需的任何步骤即可.

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.

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

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

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

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.

这篇关于如何获得从当前位置到下一步的距离,方向,持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆