my app allows the user to record his way. I use the following code to show the coordinates received on a map:
PolylineOptions rectOptions = new PolylineOptions();
for (int i = 0; i < _lstLat.length; i++) {
rectOptions.add(new LatLng(_lstLat[i], _lstLong[i]));
}
if (_lstLat.length > 0) {
LatLng latlng = new LatLng(_lstLat[0], _lstLong[0]);
CameraUpdate center = CameraUpdateFactory.newLatLng(latlng);
CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
mMap.moveCamera(center);
mMap.animateCamera(zoom);
Polyline polyline = mMap.addPolyline(rectOptions);
}
Now I want to the distance the user moved. How can I get that? I found the following code that seems to work.
public static String GetDistance(GeoPoint src, GeoPoint dest) throws ProtocolException, IOException, JSONException {
StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.googleapis.com/maps/api/directions/json?");
urlString.append("origin=");// from
urlString.append(Double.toString((double) src.getLatitudeE6() / 1E6));
urlString.append(",");
urlString.append(Double.toString((double) src.getLongitudeE6() / 1E6));
urlString.append("&destination=");// to
urlString.append(Double.toString((double) dest.getLatitudeE6() / 1E6));
urlString.append(",");
urlString.append(Double.toString((double) dest.getLongitudeE6() / 1E6));
urlString.append("&mode=walking&sensor=true");
Log.d("xxx", "URL=" + urlString.toString());
// get the JSON And parse it to get the directions data.
HttpURLConnection urlConnection = null;
URL url = null;
try {
url = new URL(urlString.toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
InputStream inStream = urlConnection.getInputStream();
BufferedReader bReader = new BufferedReader(new InputStreamReader(inStream));
String temp, response = "";
while ((temp = bReader.readLine()) != null) {
// Parse data
response += temp;
}
// Close the reader, stream & connection
bReader.close();
inStream.close();
urlConnection.disconnect();
// Sortout JSONresponse
JSONObject object = (JSONObject) new JSONTokener(response).nextValue();
JSONArray array = object.getJSONArray("routes");
// Log.d("JSON","array: "+array.toString());
// Routes is a combination of objects and arrays
JSONObject routes = array.getJSONObject(0);
// Log.d("JSON","routes: "+routes.toString());
String summary = routes.getString("summary");
// Log.d("JSON","summary: "+summary);
JSONArray legs = routes.getJSONArray("legs");
// Log.d("JSON","legs: "+legs.toString());
JSONObject steps = legs.getJSONObject(0);
// Log.d("JSON","steps: "+steps.toString());
JSONObject distance = steps.getJSONObject("distance");
// Log.d("JSON","distance: "+distance.toString());
String sDistance = distance.getString("text");
int iDistance = distance.getInt("value");
return sDistance;
}
I want to knwo if this is the best way to accomplish this. Isn't there a way to get this directly from map?
Cheers, Steafn