我尝试构建一个应用程序,用于计算距起点和目的地+航点的距离和行程时间,但只有它显示我的第一条腿,我需要添加所有腿
这是json:
{
"distance" : {
"text" : "1.3 km",
"value" : 1313
},
"duration" : {
"text" : "5 min",
"value" : 278
}, {
"distance" : {
"text" : "1.9 km",
"value" : 1854
},
"duration" : {
"text" : "7 min",
"value" : 405
},
我需要添加 1.3km + 1.9km ,这样的结果,就像让我回到 3.2km 一样的值,就像总距离一样,与时间相同行程。
下面显示距离和旅行时间的directionfinder.java的片段代码
private void parseJSon(String data) throws JSONException {
if (data == null)
return;
List<Route> routes = new ArrayList<Route>();
JSONObject jsonData = new JSONObject(data);
JSONArray jsonRoutes = jsonData.getJSONArray("routes");
for (int i = 0; i < jsonRoutes.length(); i++) {
JSONObject jsonRoute = jsonRoutes.getJSONObject(i);
Route route = new Route();
JSONObject overview_polylineJson = jsonRoute.getJSONObject("overview_polyline");
JSONArray jsonLegs = jsonRoute.getJSONArray("legs");
JSONObject jsonLeg = jsonLegs.getJSONObject(0);
JSONObject jsonDistance = jsonLeg.getJSONObject("distance");
JSONObject jsonDuration = jsonLeg.getJSONObject("duration");
JSONObject jsonEndLocation = jsonLeg.getJSONObject("end_location");
JSONObject jsonStartLocation = jsonLeg.getJSONObject("start_location");
route.distance = new Distance(jsonDistance.getString("text"), jsonDistance.getInt("value"));
route.duration = new Duration(jsonDuration.getString("text"), jsonDuration.getInt("value"));
route.endAddress = jsonLeg.getString("end_address");
route.startAddress = jsonLeg.getString("start_address");
route.startLocation = new LatLng(jsonStartLocation.getDouble("lat"), jsonStartLocation.getDouble("lng"));
route.endLocation = new LatLng(jsonEndLocation.getDouble("lat"), jsonEndLocation.getDouble("lng"));
route.points = decodePolyLine(overview_polylineJson.getString("points"));
routes.add(route);
}
listener.onDirectionFinderSuccess(routes);
}
答案 0 :(得分:0)
假设这是你的JSON(我试图从你的提取代码中推断它)。如果它不是您拥有的JSON,请更正它,我会相应地修改我的答案。
public RelayCommand<IList> SetFlagCommand { get; private set; }
...
SetFlagCommand = new RelayCommand<IList>(SetFlag, CanSetFlag);
...
mButtons.Add(new Button
{
...
Command = SetFlagCommand,
CommandParameter = new Binding("SelectedItems") { ElementName = "lstAllChoices" },
});
...
private void SetFlag(IList list)...
private bool CanSetFlag(IList list)...
您需要遍历腿部以添加它们并计算路线的总值。为了清晰起见,我删除了所有与距离和持续时间无关的内容。
{
"routes" : [ {
"overview_polyline": {},
"legs" : [ {
"distance" : { "text" : "1.3 km", "value" : 1313 },
"duration" : { "text" : "5 min", "value" : 278 }
},{
"distance" : { "text" : "1.9 km", "value" : 1854 },
"duration" : { "text" : "7 min", "value" : 405 }
} ]
},{
"overview_polyline": {},
"legs" : [ {
"distance" : { "text" : "1.4 km", "value" : 1420 },
"duration" : { "text" : "6 min", "value" : 305 }
},{
"distance" : { "text" : "2.1 km", "value" : 2100 },
"duration" : { "text" : "9 min", "value" : 540 }
} ]
} ]
}
输出:
private void parseJSon(String data) throws JSONException {
if (data == null)
return;
List<Route> routes = new ArrayList<>();
JSONObject jsonData = new JSONObject(data);
JSONArray jsonRoutes = jsonData.getJSONArray("routes");
for (int i = 0; i < jsonRoutes.length(); i++) {
JSONObject jsonRoute = jsonRoutes.getJSONObject(i);
Route route = new Route();
JSONObject overview_polylineJson = jsonRoute.getJSONObject("overview_polyline");
JSONArray jsonLegs = jsonRoute.getJSONArray("legs");
double routeDistanceText = 0d;
int routeDistanceValue = 0;
int routeDurationText = 0;
int routeDurationValue = 0;
for (int legIndex = 0; legIndex < jsonLegs.length(); legIndex++) {
JSONObject jsonLeg = jsonLegs.getJSONObject(legIndex);
JSONObject jsonDistance = jsonLeg.getJSONObject("distance");
JSONObject jsonDuration = jsonLeg.getJSONObject("duration");
// extract leg distance text as a double
String legDistanceTextString = jsonDistance.getString("text");
double legDistanceText = Double.valueOf(legDistanceTextString.substring(0, legDistanceTextString.indexOf(" ")));
// extract leg distance value as an integer
int legDistanceValue = jsonDistance.getInt("value");
// extract leg duration text as an integer
String legDurationTextString = jsonDuration.getString("text");
int legDurationText = Integer.valueOf(legDurationTextString.substring(0, legDurationTextString.indexOf(" ")));
// extract leg duration value as an integer
int legDurationValue = jsonDuration.getInt("value");
// add leg values to route values
routeDistanceText += legDistanceText;
routeDistanceValue += legDistanceValue;
routeDurationText += legDurationText;
routeDurationValue += legDurationValue;
}
// add back km and min to distance and duration texts
String routeDistanceTextString = routeDistanceText + " km";
String routeDurationTextString = routeDurationText + " min";
Log.i("TAG", "route: "+i);
Log.i("TAG", "routeDistanceText: "+routeDistanceTextString);
Log.i("TAG", "routeDistanceValue: "+routeDistanceValue);
Log.i("TAG", "routeDurationText: "+routeDurationTextString);
Log.i("TAG", "routeDurationValue: "+routeDurationValue);
route.distance = new Distance(routeDistanceTextString, routeDistanceValue);
route.duration = new Duration(routeDurationTextString, routeDurationValue);
routes.add(route);
}
}