问题
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?
Example: Stripped json segment from Direction API
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:
Total distance: 389.5 km Total duration: 6 hours 33 mins
回答1:
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.
回答2:
This adds duration to Kushal's answer:
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");
}
回答3:
This method will calculate distance of all legs array :
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);
}
回答4:
Estimate travel time and distance based on a recommended route.
https://developers.google.com/maps/documentation/distance-matrix/
api https://maps.googleapis.com/maps/api/distancematrix/json?origins=48.9107372,24.6907184&destinations=48.9333618,24.7070269&mode=driving&language=en-EN
回答5:
Distance Calculation method. Hope this ll help someone
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;
}
回答6:
Here's my code.Pass the JSONObject obtained from direction request url.
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;
}
来源:https://stackoverflow.com/questions/30326091/directions-api-total-distance-duration-with-waypoints