我尝试构建一个应用程序,向我显示一个带有航点的路线,并在textview中显示距离和旅行时间。
我的问题如下,我有一个代码片段,用于计算total distance
和totalSeconds
,在我的应用之前,只显示两点之间的时间和距离,现在使用此代码,它从航路点计算距离和时间
这是我从所有航路点获得totalDistance和totalSeconds的代码片段
public class DirectionFinder {
private static final String DIRECTION_URL_API = "https://maps.googleapis.com/maps/api/directions/json?";
private static final String GOOGLE_API_KEY = "AIzaSyDLCO4H7Bsw3OgcjYHEOnl8j5WkWLjABow";
private DirectionFinderListener listener;
private String origin;
private String destination;
private String waypoints;
public DirectionFinder(DirectionFinderListener listener, String origin, String destination, String waypoints) {
this.listener = listener;
this.origin = origin;
this.destination = destination;
this.waypoints = waypoints;
}
public void execute() throws UnsupportedEncodingException {
listener.onDirectionFinderStart();
new DownloadRawData().execute(createUrl());
}
private String createUrl() throws UnsupportedEncodingException {
String urlOrigin = URLEncoder.encode(origin, "utf-8");
String urlDestination = URLEncoder.encode(destination, "utf-8");
String urlWaypoints = URLEncoder.encode(waypoints, "utf-8");
return DIRECTION_URL_API + "origin=" + urlOrigin + "&destination=" + urlDestination + "&waypoints=optimize:true" + urlWaypoints + "&key=" + GOOGLE_API_KEY;
}
private class DownloadRawData extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String link = params[0];
try {
URL url = new URL(link);
InputStream is = url.openConnection().getInputStream();
StringBuffer buffer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String res) {
try {
parseJSon(res);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
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");
long totalDistance = 0;
int totalSeconds = 0;
JSONObject jsonDistance = null;
JSONObject jsonDuration = null;
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");
for (int j = 0; j < jsonLegs.length(); j++) {
jsonDistance = ((JSONObject) jsonLegs.get(j)).getJSONObject("distance");
totalDistance = totalDistance + Long.parseLong(jsonDistance.getString("value"));
/** Getting duration from the json data */
jsonDuration = ((JSONObject) jsonLegs.get(j)).getJSONObject("duration");
totalSeconds = totalSeconds + Integer.parseInt(jsonDuration.getString("value"));
}
route.distance = new Distance(jsonDistance.getString("text"), jsonDistance.getInt("value"));
route.duration = new Duration(jsonDuration.getString("text"), jsonDuration.getInt("value"));
route.points = decodePolyLine(overview_polylineJson.getString("points"));
routes.add(route);
}
double dist = totalDistance / 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");
listener.onDirectionFinderSuccess(routes);
}
问题是,我需要从片段之前获取totalDistance和totalSeconds,我如何获取这些数据,我不知道我是怎么做的,下面是我之前获取数据的代码
route.distance = new Distance(jsonDistance.getString("text"), jsonDistance.getInt("value"));
route.duration = new Duration(jsonDuration.getString("text"), jsonDuration.getInt("value"));