I am trying to find distance between two locations. I have longitudes and latitudes and I can calculate Euclidean distance. But I want to find road distance. I mean, , I wan
Kenny's improved answer that is working for me, I have modified few things according to me. As LatLng is introduced instead of geopoint. I am posting working code which is modified version of Kenny's answer. All credits goes to kenny not me!
Important point :
Pass the address in string format. for eg : toronto
copy this function :
private void GetDistance(String src, String dest) {
String s = src.replaceAll("\\s","+");
String d = dest.replaceAll("\\s","+");
StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.googleapis.com/maps/api/directions/json?");
urlString.append("origin=");
urlString.append((s));
urlString.append("&destination=");
urlString.append((d));
urlString.append("&mode=driving&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());
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("xxx", "array: " + array.toString());
//Routes is a combination of objects and arrays
JSONObject routes = array.getJSONObject(0);
Log.d("xxx", "routes: " + routes.toString());
//String summary = routes.getString("summary");
//Log.d("JSON","summary: "+summary);
JSONArray legs = routes.getJSONArray("legs");
Log.d("xxx", "legs: " + legs.toString());
JSONObject steps = legs.getJSONObject(0);
Log.d("JSON", "steps: " + steps.toString());
JSONObject distance = steps.getJSONObject("distance");
Log.d("xxx", "distance: " + distance.toString());
String sDistance = distance.getString("text");
int iDistance = distance.getInt("value");
Log.d("SEE THE DIST : ", "" + sDistance + iDistance);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
As this function performs networking operation we must use AsyncTask as follows:
private class Task extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
GetDistance(from, to);
return null;
}
}
LOG CAT:
01-05 17:01:08.657 3736-3820/in.test D/xxx: URL=http://maps.googleapis.com/maps/api/directions/json?origin=MP+Chowk+Thane+West+Thane,+Maharashtra+400080+&destination=Ghatkopar+West,+Mumbai,+Maharashtra,+India&mode=driving&sensor=true
01-05 17:01:08.687 3736-3736/in.test W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
01-05 17:01:08.687 3736-3736/in.test W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
01-05 17:01:08.797 3736-3778/in.test V/RenderScript: Application requested CPU execution
01-05 17:01:08.807 3736-3778/in.test V/RenderScript: 0xb97768c0 Launching thread(s), CPUs 4
01-05 17:01:09.097 3736-3820/in.test D/xxx: array: [{"bounds":{"northeast":{"lat":19.1885561,"lng":72.9686183},"southwest":{"lat":19.0842303,"lng":72.9075409}},"copyrights":"Map data ©2016 Google","legs":[{"distance":{"text":"15.8 km","value":15818},"duration":{"text":"26 mins","value":1562},"end_address":"Ghatkopar West, Mumbai, Maharashtra, India","end_location":{"lat":19.0908134,"lng":72.9076817},"start_address":"MP Chowk, Thane West, Thane, Maharashtra 400080, India","start_location":{"lat":19.1855772,"lng":72.9556247},"steps":[{"distance":{"text":"27 m","value":27},"duration":{"text":"1 min","value":6},"end_location":{"lat":19.185599,"lng":72.9558468},"html_instructions":"Head <b>northeast<\/b> on <b>MP Chowk<\/b> toward <b>SG Barve Rd<\/b>","polyline":{"points":"{dbtBsch|L?AA?AA?AA??AAAAA?A?A?AAA?A?A?A?A?A?A@A?A@A?A@A?A@A"},"start_location":{"lat":19.1855772,"lng":72.9556247},"travel_mode":"DRIVING"},{"distance":{"text":"0.9 km","value":870},"duration":{"text":"3 mins","value":165},"end_location":{"lat":19.1883648,"lng":72.963413},"html_instructions":"Exit the roundabout onto <b>Lal Bahadur Shastri Marg<\/b><div style=\"font-size:0.9em\">Pass by Wagle Estate Post Office (on the left)<\/div>","polyline":{"points":"_ebtBaeh|LWoAOcAEe@A_AA{AMcBEQC]COOqAk@sDcAaE_@_Ac@}@[o@o@aAk@w@k@w@e@{@s@{AWm@"},"start_location":{"lat":19.185599,"lng":72.9558468},"travel_mode":"DRIVING"},{"distance":{"text":"2.4 km","value":2448},"duration":{"text":"5 mins","value":316},"end_location":{"lat":19.16823,"lng":72.96618509999999},"html_instructions":"Turn <b>right<\/b> to merge onto <b>NH 3<\/b><div style=\"font-size:0.9em\">Partial toll road<\/div>","polyline":{"points":"gvbtBiti|LQYIOCEGKXEZGp@QvM}CnAWLCNCL?X?jFsAp@QlGaBt@Qh@Of@Mz@WXIRI`@MNGRKrB_AxCqAbCaAz@]^Mb@Mr@Ob@IbDg@v@O|B]XEvAW~@OLAPCN?Z?r@@tG^r@Df@F`@HzDt@lAZl@RrAr@bAn@vC`BtD~B"},"start_location":{"lat":19.1883648,"lng":72.963413},"travel_mode":"DRIVING"},{"distance":{"text":"5.6 km","value":5599},"duration":{"text":"5 mins","value":320},"end_location":{"lat":19.1249579,"lng":72.9390226},"html_instructions":"Keep <b>right<\/b> to stay on <b>NH 3<\/b><div style=\"font-size:0.9em\">Partial toll road<\/div>","maneuver":"keep-right","polyline":{"points":"mx~sBuej|LnEnCnElCXNXNtFhDtFhDnc@jW|AbAtL|GZPTN@@@?B@DD@?DB`@X@?rN|I|BtAb@T~X~NzNtHd_@vRhHtDNJh@ZrCzAh@XxAt@lAn@zo@l\\p@ZdCjA~Bz@fGjB"},"start_location":{"lat":19.16823,"lng":72.96618509999999},"travel_mode":"DRIVING"},{"distance":{"text":"1.5 km","value":1518},"duration":{"text":"2 mins","value":95},"end_location":{"lat":19.1122651,"lng":72.93371189999999},"html_instructions":"Keep <b>left<\/b> to stay on <b>NH 3<\/b>","maneuver":"keep-left","polyline":{"points":"_jvsB{{d|LtCt@v@VxAj@jKjDj@Pt`@zLrKfD~HhC"},"start_location":{"lat":19.1249579,"lng":72.9390226},"travel_mode":"DRIVING"},{"distance":{"text":"2.4 km","value":2371},"duration":{"text":"2 mins","value":139},"end_location":{"lat":19.0923682,"lng":72.9255933},"html_instructions":"Keep <b>right<\/b> to stay on <b>NH 3<\/b>","maneuver":"keep-right","polyline":{"points":"uzssBuzc|LrQlFd@Nd@N|XrInBn@zBp@vQpF`F|AjVpH~PfFzAh@"},"start_location":{"lat":19.1122651,"lng":72.93371189999999},"travel_mode":"DRIVING"},{"distance":{"text":"0.9 km","value":921},"duration":{"text":"1 min","value":54},"end_location":{"lat":19.0849757,"lng":72.9217895},"html_instructions":"Continue straight to stay on <b>NH 3<\/b>","maneuver":"straight","polyline":{"points":"i~osB}gb|LZJ|NlEXHTHn@R|@VvAb@tC~@b@NTHh@TB?TJNHZNTL`@T`@Vr@f@d@^`E~C"},"start_location":{"lat":19.0923682,"lng":72.9255933},"travel_mode":"DRIVING"},{"distance":{"text":"0.1 km","value":132},"duration":{"text":"1 min","value":18},"end_location":{"lat":19.0842303,"lng":72.92085449999999},"html_instructions":"Take the exit on the <b>right<\/b> toward <b>Link Rd<\/b>","maneuver":"ramp-right","polyline":{"points":"cpnsBepa|LBV@@@@rBlBDDBDDF@FFT"},"start_location":{"lat":19.0849757,"lng":72.9217895},"travel_mode":"DRIVING"},{"distance":{"text":"0.3 km","value":275},"duration":{"text":"1 min","value":47},"end_location":{"lat":19.0862782,"lng":72.9194201},"html_i
01-05 17:01:09.107 3736-3820/in.test D/xxx: routes: {"bounds":{"northeast":{"lat":19.1885561,"lng":72.9686183},"southwest":{"lat":19.0842303,"lng":72.9075409}},"copyrights":"Map data ©2016 Google","legs":[{"distance":{"text":"15.8 km","value":15818},"duration":{"text":"26 mins","value":1562},"end_address":"Ghatkopar West, Mumbai, Maharashtra, India","end_location":{"lat":19.0908134,"lng":72.9076817},"start_address":"MP Chowk, Thane West, Thane, Maharashtra 400080, India","start_location":{"lat":19.1855772,"lng":72.9556247},"steps":[{"distance":{"text":"27 m","value":27},"duration":{"text":"1 min","value":6},"end_location":{"lat":19.185599,"lng":72.9558468},"html_instructions":"Head <b>northeast<\/b> on <b>MP Chowk<\/b> toward <b>SG Barve Rd<\/b>","polyline":{"points":"{dbtBsch|L?AA?AA?AA??AAAAA?A?A?AAA?A?A?A?A?A?A@A?A@A?A@A?A@A"},"start_location":{"lat":19.1855772,"lng":72.9556247},"travel_mode":"DRIVING"},{"distance":{"text":"0.9 km","value":870},"duration":{"text":"3 mins","value":165},"end_location":{"lat":19.1883648,"lng":72.963413},"html_instructions":"Exit the roundabout onto <b>Lal Bahadur Shastri Marg<\/b><div style=\"font-size:0.9em\">Pass by Wagle Estate Post Office (on the left)<\/div>","polyline":{"points":"_ebtBaeh|LWoAOcAEe@A_AA{AMcBEQC]COOqAk@sDcAaE_@_Ac@}@[o@o@aAk@w@k@w@e@{@s@{AWm@"},"start_location":{"lat":19.185599,"lng":72.9558468},"travel_mode":"DRIVING"},{"distance":{"text":"2.4 km","value":2448},"duration":{"text":"5 mins","value":316},"end_location":{"lat":19.16823,"lng":72.96618509999999},"html_instructions":"Turn <b>right<\/b> to merge onto <b>NH 3<\/b><div style=\"font-size:0.9em\">Partial toll road<\/div>","polyline":{"points":"gvbtBiti|LQYIOCEGKXEZGp@QvM}CnAWLCNCL?X?jFsAp@QlGaBt@Qh@Of@Mz@WXIRI`@MNGRKrB_AxCqAbCaAz@]^Mb@Mr@Ob@IbDg@v@O|B]XEvAW~@OLAPCN?Z?r@@tG^r@Df@F`@HzDt@lAZl@RrAr@bAn@vC`BtD~B"},"start_location":{"lat":19.1883648,"lng":72.963413},"travel_mode":"DRIVING"},{"distance":{"text":"5.6 km","value":5599},"duration":{"text":"5 mins","value":320},"end_location":{"lat":19.1249579,"lng":72.9390226},"html_instructions":"Keep <b>right<\/b> to stay on <b>NH 3<\/b><div style=\"font-size:0.9em\">Partial toll road<\/div>","maneuver":"keep-right","polyline":{"points":"mx~sBuej|LnEnCnElCXNXNtFhDtFhDnc@jW|AbAtL|GZPTN@@@?B@DD@?DB`@X@?rN|I|BtAb@T~X~NzNtHd_@vRhHtDNJh@ZrCzAh@XxAt@lAn@zo@l\\p@ZdCjA~Bz@fGjB"},"start_location":{"lat":19.16823,"lng":72.96618509999999},"travel_mode":"DRIVING"},{"distance":{"text":"1.5 km","value":1518},"duration":{"text":"2 mins","value":95},"end_location":{"lat":19.1122651,"lng":72.93371189999999},"html_instructions":"Keep <b>left<\/b> to stay on <b>NH 3<\/b>","maneuver":"keep-left","polyline":{"points":"_jvsB{{d|LtCt@v@VxAj@jKjDj@Pt`@zLrKfD~HhC"},"start_location":{"lat":19.1249579,"lng":72.9390226},"travel_mode":"DRIVING"},{"distance":{"text":"2.4 km","value":2371},"duration":{"text":"2 mins","value":139},"end_location":{"lat":19.0923682,"lng":72.9255933},"html_instructions":"Keep <b>right<\/b> to stay on <b>NH 3<\/b>","maneuver":"keep-right","polyline":{"points":"uzssBuzc|LrQlFd@Nd@N|XrInBn@zBp@vQpF`F|AjVpH~PfFzAh@"},"start_location":{"lat":19.1122651,"lng":72.93371189999999},"travel_mode":"DRIVING"},{"distance":{"text":"0.9 km","value":921},"duration":{"text":"1 min","value":54},"end_location":{"lat":19.0849757,"lng":72.9217895},"html_instructions":"Continue straight to stay on <b>NH 3<\/b>","maneuver":"straight","polyline":{"points":"i~osB}gb|LZJ|NlEXHTHn@R|@VvAb@tC~@b@NTHh@TB?TJNHZNTL`@T`@Vr@f@d@^`E~C"},"start_location":{"lat":19.0923682,"lng":72.9255933},"travel_mode":"DRIVING"},{"distance":{"text":"0.1 km","value":132},"duration":{"text":"1 min","value":18},"end_location":{"lat":19.0842303,"lng":72.92085449999999},"html_instructions":"Take the exit on the <b>right<\/b> toward <b>Link Rd<\/b>","maneuver":"ramp-right","polyline":{"points":"cpnsBepa|LBV@@@@rBlBDDBDDF@FFT"},"start_location":{"lat":19.0849757,"lng":72.9217895},"travel_mode":"DRIVING"},{"distance":{"text":"0.3 km","value":275},"duration":{"text":"1 min","value":47},"end_location":{"lat":19.0862782,"lng":72.9194201},"html_i
01-05 17:01:09.107 3736-3820/in.test D/xxx: legs: [{"distance":{"text":"15.8 km","value":15818},"duration":{"text":"26 mins","value":1562},"end_address":"Ghatkopar West, Mumbai, Maharashtra, India","end_location":{"lat":19.0908134,"lng":72.9076817},"start_address":"MP Chowk, Thane West, Thane, Maharashtra 400080, India","start_location":{"lat":19.1855772,"lng":72.9556247},"steps":[{"distance":{"text":"27 m","value":27},"duration":{"text":"1 min","value":6},"end_location":{"lat":19.185599,"lng":72.9558468},"html_instructions":"Head <b>northeast<\/b> on <b>MP Chowk<\/b> toward <b>SG Barve Rd<\/b>","polyline":{"points":"{dbtBsch|L?AA?AA?AA??AAAAA?A?A?AAA?A?A?A?A?A?A@A?A@A?A@A?A@A"},"start_location":{"lat":19.1855772,"lng":72.9556247},"travel_mode":"DRIVING"},{"distance":{"text":"0.9 km","value":870},"duration":{"text":"3 mins","value":165},"end_location":{"lat":19.1883648,"lng":72.963413},"html_instructions":"Exit the roundabout onto <b>Lal Bahadur Shastri Marg<\/b><div style=\"font-size:0.9em\">Pass by Wagle Estate Post Office (on the left)<\/div>","polyline":{"points":"_ebtBaeh|LWoAOcAEe@A_AA{AMcBEQC]COOqAk@sDcAaE_@_Ac@}@[o@o@aAk@w@k@w@e@{@s@{AWm@"},"start_location":{"lat":19.185599,"lng":72.9558468},"travel_mode":"DRIVING"},{"distance":{"text":"2.4 km","value":2448},"duration":{"text":"5 mins","value":316},"end_location":{"lat":19.16823,"lng":72.96618509999999},"html_instructions":"Turn <b>right<\/b> to merge onto <b>NH 3<\/b><div style=\"font-size:0.9em\">Partial toll road<\/div>","polyline":{"points":"gvbtBiti|LQYIOCEGKXEZGp@QvM}CnAWLCNCL?X?jFsAp@QlGaBt@Qh@Of@Mz@WXIRI`@MNGRKrB_AxCqAbCaAz@]^Mb@Mr@Ob@IbDg@v@O|B]XEvAW~@OLAPCN?Z?r@@tG^r@Df@F`@HzDt@lAZl@RrAr@bAn@vC`BtD~B"},"start_location":{"lat":19.1883648,"lng":72.963413},"travel_mode":"DRIVING"},{"distance":{"text":"5.6 km","value":5599},"duration":{"text":"5 mins","value":320},"end_location":{"lat":19.1249579,"lng":72.9390226},"html_instructions":"Keep <b>right<\/b> to stay on <b>NH 3<\/b><div style=\"font-size:0.9em\">Partial toll road<\/div>","maneuver":"keep-right","polyline":{"points":"mx~sBuej|LnEnCnElCXNXNtFhDtFhDnc@jW|AbAtL|GZPTN@@@?B@DD@?DB`@X@?rN|I|BtAb@T~X~NzNtHd_@vRhHtDNJh@ZrCzAh@XxAt@lAn@zo@l\\p@ZdCjA~Bz@fGjB"},"start_location":{"lat":19.16823,"lng":72.96618509999999},"travel_mode":"DRIVING"},{"distance":{"text":"1.5 km","value":1518},"duration":{"text":"2 mins","value":95},"end_location":{"lat":19.1122651,"lng":72.93371189999999},"html_instructions":"Keep <b>left<\/b> to stay on <b>NH 3<\/b>","maneuver":"keep-left","polyline":{"points":"_jvsB{{d|LtCt@v@VxAj@jKjDj@Pt`@zLrKfD~HhC"},"start_location":{"lat":19.1249579,"lng":72.9390226},"travel_mode":"DRIVING"},{"distance":{"text":"2.4 km","value":2371},"duration":{"text":"2 mins","value":139},"end_location":{"lat":19.0923682,"lng":72.9255933},"html_instructions":"Keep <b>right<\/b> to stay on <b>NH 3<\/b>","maneuver":"keep-right","polyline":{"points":"uzssBuzc|LrQlFd@Nd@N|XrInBn@zBp@vQpF`F|AjVpH~PfFzAh@"},"start_location":{"lat":19.1122651,"lng":72.93371189999999},"travel_mode":"DRIVING"},{"distance":{"text":"0.9 km","value":921},"duration":{"text":"1 min","value":54},"end_location":{"lat":19.0849757,"lng":72.9217895},"html_instructions":"Continue straight to stay on <b>NH 3<\/b>","maneuver":"straight","polyline":{"points":"i~osB}gb|LZJ|NlEXHTHn@R|@VvAb@tC~@b@NTHh@TB?TJNHZNTL`@T`@Vr@f@d@^`E~C"},"start_location":{"lat":19.0923682,"lng":72.9255933},"travel_mode":"DRIVING"},{"distance":{"text":"0.1 km","value":132},"duration":{"text":"1 min","value":18},"end_location":{"lat":19.0842303,"lng":72.92085449999999},"html_instructions":"Take the exit on the <b>right<\/b> toward <b>Link Rd<\/b>","maneuver":"ramp-right","polyline":{"points":"cpnsBepa|LBV@@@@rBlBDDBDDF@FFT"},"start_location":{"lat":19.0849757,"lng":72.9217895},"travel_mode":"DRIVING"},{"distance":{"text":"0.3 km","value":275},"duration":{"text":"1 min","value":47},"end_location":{"lat":19.0862782,"lng":72.9194201},"html_instructions":"Continue onto <b>Link Rd<\/b><div style=\"font-size:0.9em\">Pass by A 3 (on the left)<\/div>","polyline":{"points":"mknsBija|LIRoDtB{BpAcB`A"
01-05 17:01:09.117 3736-3820/in.test D/JSON: steps: {"distance":{"text":"15.8 km","value":15818},"duration":{"text":"26 mins","value":1562},"end_address":"Ghatkopar West, Mumbai, Maharashtra, India","end_location":{"lat":19.0908134,"lng":72.9076817},"start_address":"MP Chowk, Thane West, Thane, Maharashtra 400080, India","start_location":{"lat":19.1855772,"lng":72.9556247},"steps":[{"distance":{"text":"27 m","value":27},"duration":{"text":"1 min","value":6},"end_location":{"lat":19.185599,"lng":72.9558468},"html_instructions":"Head <b>northeast<\/b> on <b>MP Chowk<\/b> toward <b>SG Barve Rd<\/b>","polyline":{"points":"{dbtBsch|L?AA?AA?AA??AAAAA?A?A?AAA?A?A?A?A?A?A@A?A@A?A@A?A@A"},"start_location":{"lat":19.1855772,"lng":72.9556247},"travel_mode":"DRIVING"},{"distance":{"text":"0.9 km","value":870},"duration":{"text":"3 mins","value":165},"end_location":{"lat":19.1883648,"lng":72.963413},"html_instructions":"Exit the roundabout onto <b>Lal Bahadur Shastri Marg<\/b><div style=\"font-size:0.9em\">Pass by Wagle Estate Post Office (on the left)<\/div>","polyline":{"points":"_ebtBaeh|LWoAOcAEe@A_AA{AMcBEQC]COOqAk@sDcAaE_@_Ac@}@[o@o@aAk@w@k@w@e@{@s@{AWm@"},"start_location":{"lat":19.185599,"lng":72.9558468},"travel_mode":"DRIVING"},{"distance":{"text":"2.4 km","value":2448},"duration":{"text":"5 mins","value":316},"end_location":{"lat":19.16823,"lng":72.96618509999999},"html_instructions":"Turn <b>right<\/b> to merge onto <b>NH 3<\/b><div style=\"font-size:0.9em\">Partial toll road<\/div>","polyline":{"points":"gvbtBiti|LQYIOCEGKXEZGp@QvM}CnAWLCNCL?X?jFsAp@QlGaBt@Qh@Of@Mz@WXIRI`@MNGRKrB_AxCqAbCaAz@]^Mb@Mr@Ob@IbDg@v@O|B]XEvAW~@OLAPCN?Z?r@@tG^r@Df@F`@HzDt@lAZl@RrAr@bAn@vC`BtD~B"},"start_location":{"lat":19.1883648,"lng":72.963413},"travel_mode":"DRIVING"},{"distance":{"text":"5.6 km","value":5599},"duration":{"text":"5 mins","value":320},"end_location":{"lat":19.1249579,"lng":72.9390226},"html_instructions":"Keep <b>right<\/b> to stay on <b>NH 3<\/b><div style=\"font-size:0.9em\">Partial toll road<\/div>","maneuver":"keep-right","polyline":{"points":"mx~sBuej|LnEnCnElCXNXNtFhDtFhDnc@jW|AbAtL|GZPTN@@@?B@DD@?DB`@X@?rN|I|BtAb@T~X~NzNtHd_@vRhHtDNJh@ZrCzAh@XxAt@lAn@zo@l\\p@ZdCjA~Bz@fGjB"},"start_location":{"lat":19.16823,"lng":72.96618509999999},"travel_mode":"DRIVING"},{"distance":{"text":"1.5 km","value":1518},"duration":{"text":"2 mins","value":95},"end_location":{"lat":19.1122651,"lng":72.93371189999999},"html_instructions":"Keep <b>left<\/b> to stay on <b>NH 3<\/b>","maneuver":"keep-left","polyline":{"points":"_jvsB{{d|LtCt@v@VxAj@jKjDj@Pt`@zLrKfD~HhC"},"start_location":{"lat":19.1249579,"lng":72.9390226},"travel_mode":"DRIVING"},{"distance":{"text":"2.4 km","value":2371},"duration":{"text":"2 mins","value":139},"end_location":{"lat":19.0923682,"lng":72.9255933},"html_instructions":"Keep <b>right<\/b> to stay on <b>NH 3<\/b>","maneuver":"keep-right","polyline":{"points":"uzssBuzc|LrQlFd@Nd@N|XrInBn@zBp@vQpF`F|AjVpH~PfFzAh@"},"start_location":{"lat":19.1122651,"lng":72.93371189999999},"travel_mode":"DRIVING"},{"distance":{"text":"0.9 km","value":921},"duration":{"text":"1 min","value":54},"end_location":{"lat":19.0849757,"lng":72.9217895},"html_instructions":"Continue straight to stay on <b>NH 3<\/b>","maneuver":"straight","polyline":{"points":"i~osB}gb|LZJ|NlEXHTHn@R|@VvAb@tC~@b@NTHh@TB?TJNHZNTL`@T`@Vr@f@d@^`E~C"},"start_location":{"lat":19.0923682,"lng":72.9255933},"travel_mode":"DRIVING"},{"distance":{"text":"0.1 km","value":132},"duration":{"text":"1 min","value":18},"end_location":{"lat":19.0842303,"lng":72.92085449999999},"html_instructions":"Take the exit on the <b>right<\/b> toward <b>Link Rd<\/b>","maneuver":"ramp-right","polyline":{"points":"cpnsBepa|LBV@@@@rBlBDDBDDF@FFT"},"start_location":{"lat":19.0849757,"lng":72.9217895},"travel_mode":"DRIVING"},{"distance":{"text":"0.3 km","value":275},"duration":{"text":"1 min","value":47},"end_location":{"lat":19.0862782,"lng":72.9194201},"html_instructions":"Continue onto <b>Link Rd<\/b><div style=\"font-size:0.9em\">Pass by A 3 (on the left)<\/div>","polyline":{"points":"mknsBija|LIRoDtB{BpAcB`A
01-05 17:01:09.117 3736-3820/in.test D/xxx: distance: {"text":"15.8 km","value":15818}
01-05 17:01:09.117 3736-3820/in.test D/SEE THE DIST :: 15.8 km15818
THANKS KENNY!
The easiest way would be to use the Google Directions API to get the directions, this gives you a list of all the points along the route (and the total distance).
Check out : http://code.google.com/apis/maps/documentation/directions/
If your not sure how to do this let me know and i'll post some code for you which will get the directions from google and extract the data you need.
UPDATE - CODE AS REQUESTED
private void GetDistance(GeoPoint src, GeoPoint dest) {
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;
url = new URL(urlString.toString());
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");
}
This will return the distance between the two points, you can change the function to return it as a string or as an integer, just return either sDistance or iDistance.
Hope this helps you, let me know if you need any more help.
I saw couple of people have answered it correctly but have a look to this answer. Very useful api for using Google Map direction api. Google Maps Direction Api usage
Since you're already working with google-maps, take a look at the driving directions api.
(BTW, Google Maps API v3? I'm going to assume yes)
For a pair of lat/lng points, you can make a request for driving directions
In API3 it would look something like so:
var directionsService = new google.maps.DirectionsService();
var start = from; // A Google LatLng point or an address,
// see their API for further details
var end = to; // A Google LatLng point or an address,
//see their API for further details
var request = {
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
//Check if status Ok
//Distance will be in response.routes[0].legs[0].distance.text
});
Verify that a response status was ok, and then make sure there is a routes collection. if so, there's a result and you can access the "distance" text element via:
response.routes[0].legs[0].distance.text
Hope that helps.
Use the google maps distance matrix API http://code.google.com/apis/maps/documentation/distancematrix/.