路线 API 总距离和持续时间以及航点

Muc*_*low 5 android google-maps driving-directions google-maps-android-api-2

我正在从我的 Android 应用程序中的 Directions API 获取包括航路点在内的路线。它包含多个“腿”段,这些段有自己的距离和持续时间。有没有办法将所有距离和持续时间相加得到总值?

示例:从 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
  }
}
]
Run Code Online (Sandbox Code Playgroud)

根据上面的响应,有没有一种方法可以计算并显示输出,如下所示:

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

Eli*_*s N 5

这增加了库沙尔回答的持续时间:

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");
}
Run Code Online (Sandbox Code Playgroud)


Yur*_*ets 4

作为我对 @Kushal 的评论和回答的补充,这是一种计算总时间的方法。我跳过了 get 的方法,因为它已经描述了,并给出了一个在解析时得到的JSONObject给定示例String[]JSON给定示例。在此示例中,我使用给定响应制作了所有可能的场景,因此您可以使用它而无需修改:

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

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

\n\n
05-19 23:00:38.687  14251-14251/whatever.com.myapplication D/LOG\xef\xb9\x95 4 days 6 hours 21 mins.\n
Run Code Online (Sandbox Code Playgroud)\n\n

我希望这对您有所帮助。\n请告诉我它是否适合您。

\n

  • 为什么不使用持续时间的值字段?value 是持续时间以秒为单位,因此只需将所有这些相加即可获得总秒数,然后根据需要对其进行格式化。检查我的答案以获取更多信息。 (4认同)