方向API总距离&航点的持续时间

时间:2015-05-19 12:48:00

标签: android google-maps google-maps-android-api-2 driving-directions

我正在Android应用中获取Directions API中的路标指示。它包含多个" leg"具有自己的距离和持续时间的细分。有没有办法将所有距离和持续时间相加得到总值?

示例:从Direction API中删除json段

legs": [
{
  "distance": {
    "text": "389 km",
    "value": 389438
  },
  "duration": {
   "text": "6 hours 31 mins",
   "value": 23452
  }
},
{
  "distance": {
   "text": "0.5 km",
   "value": 487
  },
  "duration": {
   "text": "2 mins",
   "value": 102
  }
}
]

从上面的回复中,有没有办法计算并显示如下输出:

总距离:389.5公里 总持续时间:6小时33分钟

6 个答案:

答案 0 :(得分:4)

这增加了Kushal回答的持续时间:

public void parseJson(String json) {

    JSONObject jsonObj = new JSONObject(json);
    JSONObject object = (JSONObject) new JSONTokener(json).nextValue();

    JSONObject legsJson = object.getJSONObject("legs");
    long totalDistance = 0;
    int totalSeconds = 0;
    for( int i = 0; i < legsJson.size(); i++ ) {
        // distance in meter
        totalDistance = totalDistance + Long.parseLong(jsonObj[i].getJSONArray("distance").getString("value"));
        totalSeconds = totalSeconds + Integer.parseInt(jsonObj[i].getJSONArray("duration").getString("value"));
    }

    // convert to kilometer
    double dist = dist / 1000.0 ;
    Log.d("distance", "Calculated distance:" + dist);

    int days = totalSeconds / 86400
    int hours = (totalSeconds - days * 86400) / 3600;
    int minutes = (totalSeconds - days * 86400 - hours * 3600) / 60;
    int seconds = totalSeconds - days * 86400 - hours * 3600 - minutes * 60;
    Log.d("duration", days + " days " + hours + " hours " + minutes + " mins" + seconds + " seconds");
}

答案 1 :(得分:2)

此方法将计算所有腿阵列的距离:

   public void parseJson(String json) {

    JSONObject jsonObj = new JSONObject(json);
    JSONObject object = (JSONObject) new JSONTokener(json).nextValue();

    JSONObject legsJson = object.getJSONObject("legs");
    long dist = 0;
    for( int i = 0; i < legsJson.size(); i++ ) {
        // dist in meter
        dist = dist + jsonObj[i].getJSONArray("distance").getString("value");
    }

    // convert to killo meter
    float frac = dist % 1000;
    dist = dist / 1000 ;
    dist = dist + frac;
    Log.w("distance","Calculated distance :"+dist);
}

答案 2 :(得分:2)

除了我对@Kushal的评论和答案之外,还有一种如何计算总时间的方法。我跳过获取JSONObject的方式,因为它已经描述并给出了解析String[]时得到的给定JSON的示例。在这个例子中,我使用给定的响应创建了所有可能的场景,因此您可以在不修改的情况下使用它:

    String[] timeItems = {"4 days 1 hour 12 mins", "5 hours 9 mins"}; // as example for visibility
    int[] total = {0, 0, 0}; // days, hours, minutes
    for(int i = 0; i < timeItems.length; i++){
        if(timeItems[i].contains("day ")){
            total[0]++;
        }else if(timeItems[i].contains("days")){
            total[0] += Integer.valueOf(timeItems[i].substring(0, timeItems[i].indexOf(" days")));
        }
        if(timeItems[i].contains("hour ")){
            total[1]++;
        }else if(timeItems[i].contains("hours")){
            if(timeItems[i].indexOf(" hours") <= 3){
                total[1] += Integer.valueOf(timeItems[i].substring(0, timeItems[i].indexOf(" hours")));
            }else{
                if(timeItems[i].contains("days")){
                    total[1] += Integer.valueOf(timeItems[i].substring(timeItems[i].lastIndexOf("days ")) + 5, timeItems[i].indexOf(" hours"));
                }else{
                    total[1] += Integer.valueOf(timeItems[i].substring(timeItems[i].lastIndexOf("day ")) + 4, timeItems[i].indexOf(" hours"));
                }
            }
        }
        if(timeItems[i].contains("min ")){
            total[2]++;
        }else if(timeItems[i].contains("mins")){
            if(timeItems[i].indexOf(" mins") <= 3){
                total[2] += Integer.valueOf(timeItems[i].substring(0, timeItems[i].indexOf(" mins")));
            }else{
                if(timeItems[i].contains("hours")){
                    total[2] += Integer.valueOf(timeItems[i].substring(timeItems[i].indexOf("hours ") + 6, timeItems[i].indexOf(" mins")));
                }else{
                    total[2] += Integer.valueOf(timeItems[i].substring(timeItems[i].indexOf("hour ") + 5, timeItems[i].indexOf(" mins")));
                }
            }
        }
    }
    Log.d("LOG", total[0] + " days " + total[1] + " hours " + total[2] + " mins.");

这比我想象的要复杂一些,也许有可能以某种方式简化或使用类似的想法,但重点是向你展示工作实例。我调试了这段代码。它给出了正确的输出:

05-19 23:00:38.687  14251-14251/whatever.com.myapplication D/LOG﹕ 4 days 6 hours 21 mins.

我希望这会有所帮助。 让我知道它是否适合你。

答案 3 :(得分:1)

答案 4 :(得分:0)

距离计算方法。希望这会帮助某人

private String distanceCalculation(String[] distanceItems) {
    //String[] totalDis = {"50 km", "4.5 km"}; // as example for visibility
    String totalDis = "";
    float distance = 0;
    String[] splitArray;
    if (distanceItems.length > 1) {
        for (String distanceItem : distanceItems) {
            splitArray = distanceItem.split(" ");
            Log.e("distance List", splitArray[0]);
            distance = distance + Float.valueOf(splitArray[0]);
        }
        DecimalFormat decimalFormat = new DecimalFormat("#.##");
        float twoDigitsF = Float.valueOf(decimalFormat.format(distance));
        totalDis = String.valueOf(twoDigitsF) + " km";
        Log.e("Total km ", totalDis);
    } else {
        totalDis = distanceItems[0];
    }
    return totalDis;
}

答案 5 :(得分:0)

这是我的代码。传递从方向请求网址获得的JSONObject。

HashMap<String,Object> getDistanceDuration(JSONObject jsonObject) {
    HashMap<String,Object> hashMap=new HashMap<>();
    double totalDistance = 0;
    long totalDuration = 0;
    try {
        JSONArray jRoutes = jsonObject.getJSONArray("routes");
        for (int i = 0; i < jRoutes.length(); i++) {
            JSONArray jLegs = ((JSONObject) jRoutes.get(i)).getJSONArray("legs");
            for (int j = 0; j < jLegs.length(); j++) {
                JSONObject legs1 = jLegs.getJSONObject(j);
                JSONObject distance = legs1.getJSONObject("distance");
                JSONObject duration = legs1.getJSONObject("duration");
                totalDistance = totalDistance+distance.getLong("value");
                totalDuration = totalDuration+duration.getLong("value");
            }
        }
        totalDistance/=1000;
        totalDuration/=60;
        totalDistance= Math.round(totalDistance* 10) / 10.0;
        hashMap.put("distance",totalDistance);
        hashMap.put("duration",totalDuration);
        Log.i("distance",totalDistance);
        Log.i("duration",totalDuration);

    } catch (JSONException e) {
        e.printStackTrace();
    }
    return hashMap;
}