9

I am developing an Android application. I need to find the distance between two geo coordinates.

I used Location.distanceBetween() and Location.distanceTo() functions. These functions give the straight distance, but the actual distance is different when we travel by road.

tomrozb
  • 25,773
  • 31
  • 101
  • 122
Chetak Bhimani
  • 459
  • 4
  • 19

2 Answers2

8

Use google api as

public float getDistance(double lat1, double lon1, double lat2, double lon2) {
        String result_in_kms = "";
        String url = "http://maps.google.com/maps/api/directions/xml?origin=" + lat1 + "," + lon1 + "&destination=" + lat2 + "," + lon2 + "&sensor=false&units=metric";
        String tag[] = {"text"};
        HttpResponse response = null;
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(url);
            response = httpClient.execute(httpPost, localContext);
            InputStream is = response.getEntity().getContent();
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = builder.parse(is);
            if (doc != null) {
                NodeList nl;
                ArrayList args = new ArrayList();
                for (String s : tag) {
                    nl = doc.getElementsByTagName(s);
                    if (nl.getLength() > 0) {
                        Node node = nl.item(nl.getLength() - 1);
                        args.add(node.getTextContent());
                    } else {
                        args.add(" - ");
                    }
                }
                result_in_kms =String.valueOf( args.get(0));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Float f=Float.valueOf(result_in_kms);
        return f*1000;
    }

Or you can use following function

public final static double AVERAGE_RADIUS_OF_EARTH = 6371;
    public float calculateDistance(double userLat, double userLng, double venueLat, double venueLng) {

        double latDistance = Math.toRadians(userLat - venueLat);
        double lngDistance = Math.toRadians(userLng - venueLng);

        double a = (Math.sin(latDistance / 2) * Math.sin(latDistance / 2)) +
                        (Math.cos(Math.toRadians(userLat))) *
                        (Math.cos(Math.toRadians(venueLat))) *
                        (Math.sin(lngDistance / 2)) *
                        (Math.sin(lngDistance / 2));

        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        return (float) (Math.round(AVERAGE_RADIUS_OF_EARTH * c));

    }
user3458541
  • 104
  • 4
  • Good but in `getDistance()` you have to search for the tag `value` and not `text` - otherwise `args.get(0)` returns distance + units (e.g **42 km**) and `String.valueOf()` throws an exception. Change `String tag[] = {"text"};` to `String tag[] = {"value"};` to get only numeric distance. – Alaa M. Oct 25 '16 at 18:54
0

You can use the Bing Maps routing service to calculate the driving distance. Since you are creating a mobile app the free terms of use of Bing Maps is double what is offered by Google. Take a look at the service here

You can also find an example of how to use this service in an Android app here

Blu
  • 4,036
  • 6
  • 38
  • 65
rbrundritt
  • 16,570
  • 2
  • 21
  • 46