Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find distance (by road) between 2 geo points in Android application without using google map direction api?

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.

like image 665
Chetak Bhimani Avatar asked Mar 24 '14 12:03

Chetak Bhimani


People also ask

Can Google Maps API calculate distance between two points?

Using the JavaScript equivalent of the Haversine formula, we can determine the length of the Polyline, the straight distance between our two markers. The function accepts two marker objects and returns the distance between them in miles.

How to draw a driving route using Google map Android API?

The first tap point in the map will be the source of the route and the second tap point in the map will be the destination of the route. On taping the second point, a driving route will be drawn in the Google Map Android API V2 using Google Directions API.

How to get the distance between two geographic locations in Android?

This example demonstrates how do I get the distance between two geographic locations in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

What is the distance matrix API for Google Maps?

This service is also available as part of the client-side Maps JavaScript API , or for server-side use with the Java Client, Python Client, Go Client and Node.js Client for Google Maps Services. The Distance Matrix API is a service that provides travel distance and time for a matrix of origins and destinations.

Does Google Maps have a formula to calculate travel distance?

When the straight line distance is not adequate, you’ll need more than a formula to determine the travel distance. Driving directions are one of the most popular features in Google Maps, so it’s unsurprising that it’s also made available via an API.


1 Answers

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));

    }
like image 148
user3458541 Avatar answered Nov 10 '22 00:11

user3458541