2016-02-25 13 views
0

我正在使用Mapbox Android SDK如何獲得從當前位置到下一步的距離,方向和持續時間?

compile ('com.mapbox.mapboxsdk:mapbox-android-sdk:[email protected]')

我問過類似的問題之前在here,但仍然有一些問題。當我獲得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中的重要信息?任何建議?

+0

有什麼方便的方法可以知道我目前的Routestep?如果我可以得到我目前的Routestep,我可以通過在步驟列表中添加Routestep索引來找到我的下一個Routestep。在我得到下一個RouteStep後,我可以使用方法computeDistance()計算我的當前和RouteStep之間的距離。我認爲這個解決方案對於開發者來說很奇怪。爲什麼currentRoute不能返回這些值或向開發人員提供方法?我不知道如何實現這一點。沒有在他們的文件中找到。 – user3034559

回答

3

希望你的應用程序順利進行。我看到你試圖讓方向,距離和持續時間進入下一步。我會盡量在保持短暫的同時儘可能地回答這個問題。

方向
首先,當你要求你需要的路線包括幾行:

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

分別。我希望這至少有助於在創建應用程序時引導您朝着正確的方向發展。

相關問題