android google map finding distance

14,566

Solution 1

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.

Solution 2

Use the google maps distance matrix API http://code.google.com/apis/maps/documentation/distancematrix/.

Solution 3

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.

Share:
14,566
cemal
Author by

cemal

Updated on June 28, 2022

Comments

  • cemal
    cemal almost 2 years

    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 want to calculate the distance of the road that I am going on while going to destination from source. In this case how to calculate this?