Wednesday 22 February 2012

Finding distance between two locations using google service in android

To find the disatnce we need to call the google service by passing present , destination location lan,lat's
we need to use the service like this

http://maps.google.com/maps/api/directions/json?origin=ourLat,ourLan&destination=destLat,destLan&sensor=true&units=metric

we need to replace ourLat,ourLan, destLat,destLan values in that service.While calling this service it is giving Json object as result we can parse the Json object and get the reslut.
Sample code:

 HttpClient disthttpclient = new DefaultHttpClient();
                
                      HttpPost distpost=new HttpPost("http://maps.google.com/maps/api/directions/json?origin="+lat2+","+lng2+"&destination="+lat1+","+lng1+"&sensor=true&units=metric");
                     
                       try{                           
                       ResponseHandler<String> responseHandler=new BasicResponseHandler();
                       String responseBody=disthttpclient.execute(distpost, responseHandler);
                       JSONObject d_Obj = new JSONObject(responseBody.toString());
                       if(d_Obj.getString("status").equals("OK")){
                           JSONArray dist_data=new JSONArray(d_Obj.getString("routes"));
                           JSONObject dist_Obj1 = dist_data.getJSONObject(0);
                           JSONArray dist_data1=new JSONArray(dist_Obj1.getString("legs"));
                           JSONObject dist_Obj2 = dist_data1.getJSONObject(0);
                           String res_str = dist_Obj2.getString("distance");
                           JSONObject dist_res = new JSONObject(res_str);                          
                           double resval=Double.parseDouble(dist_res.getString("value").toString());
                          double dist =(resval*0.000621);
                           dist = Math.round(dist*10);
                           dist = dist/10;                                
                      }
}catch(Exception e){
                        e.printStackTrace();
                        }
 finally we will get the result distance value as double and assigned to dist variable;
Here some the distance will be available for the location which is having the road way only. If the distance available the service object "status" as "OK". we need to check this and if the route distance is not available we can write some other logic to calculate the distance like below

Other way to find the distance between to locations using Location class in android

float results[] = new float[3];
                            Location.distanceBetween(lat2, lng2, lat1,lng1, results);
                            double dist_mtrs = results[0];
                            dist=(dist_mtrs*0.000621);
                            dist = Math.round(dist*10);
                            dist = dist/10;



3 comments:

  1. Hello Raghu Mudem, This post was really helpfull,
    I am stuck with a problem which is almost similar to this , i need to display the route b/w two points (src and destination), i am able to display it, but if the route is much bigger that i am not zoom in or zoom out to see both source and destination. basically i am not able to zoom in or zoom out the webview to see the entire map in webview, can u post a any sample code snippet, which shows map in webview with zoom in and zoom out controls. Thanks in advance.

    ReplyDelete
    Replies
    1. see this link.

      http://www.tutorialforandroid.com/2009/02/webview-with-zoomcontrols-in-android.html

      Delete
  2. its returning 0.0 for the variable dist

    ReplyDelete