Java 如何获取从当前位置到下一步的距离、方向和持续时间?
我正在使用Mapbox Android SDKJava 如何获取从当前位置到下一步的距离、方向和持续时间?,java,android,mapbox,Java,Android,Mapbox,我正在使用Mapbox Android SDK compile('com.mapbox.mapboxsdk:mapboxandroid sdk:3.0。0@aar) 我曾经在上一次课时问过类似的问题,但仍然有一些问题。我不知道如何实现当我得到当前路线。我的代码如下: private Waypoint lastCorrectWayPoint; private boolean checkOffRoute(Waypoint target) { boolean isOffRoute = fals
compile('com.mapbox.mapboxsdk:mapboxandroid sdk:3.0。0@aar)
我曾经在上一次课时问过类似的问题,但仍然有一些问题。我不知道如何实现当我得到当前路线。我的代码如下:
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()
及
分别。我希望这至少有助于在创建应用程序时引导您朝着正确的方向前进。有什么方便的方法可以让我知道我当前的路线吗?若我可以得到我当前的Routestep,我可以通过在步骤列表中添加Routestep索引来找到我的下一个Routestep。在获得下一个RouteStep后,我可以使用computeDistance()方法计算当前和RouteStep之间的距离。我认为这个解决方案对开发人员来说是一个奇怪的问题。为什么currentRoute不能返回这些值或向开发人员提供方法?我不知道如何实现这一点。在他们的文件中找不到。
response.body().getRoutes().get(0).getSteps().get(0).getDistance()