方向API总距离&航点持续时间 [英] Directions API total distance & duration with waypoints

查看:89
本文介绍了方向API总距离&航点持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Android应用程序中的Directions API获取包括航路点在内的路线.它包含多个具有自己的距离和持续时间的腿"段.有没有一种方法可以汇总所有距离和持续时间以获得总值?

I am getting directions including waypoints from Directions API in my Android app. It contains multiple "leg" segments which has its own distance and duration. Is there a way to sum all distances and durations get the total values?

示例:从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
  }
}
]

从上面的响应中,有没有一种方法可以计算和显示如下输出:

from the above response, is there a way to calculate and show the output like following:

总距离:389.5公里 总时长:6小时33分钟

Total distance: 389.5 km Total duration: 6 hours 33 mins

推荐答案

除了我对@Kushal的评论和答复以外,这里还提供了一种计算总时间的方法.我跳过了获取JSONObject的方法,因为它已经进行了描述,并给出了在解析JSON时获得的给定String[]的示例.在此示例中,我给出了所有可能的情况并给出了响应,因此您无需修改​​即可使用它:

As addition to my comment and answer of @Kushal here is a way how to calculate total time. I skip the way to get JSONObject, because it described already and give an example with given String[] which you get while parsing JSON. In this example I made ALL of possible scenarios with given response, so you may use it without modifying:

    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.");

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

This got to be a little bit more complex than I thought, maybe it is possible to simplify somehow or use similar ideas, but the main point is to show you working example. I debugged this code. It gives correct output:

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

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

I hope this is helpful. Let me know if it either fits you or not.

这篇关于方向API总距离&amp;航点持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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