Monday, January 5, 2015

Android: Distance between two Geo Locations

In order to find the distance between two geo locations, the below code can be used

This does not give the traffic route distance, instead gives the geographical distance. 
But this is good enough to know whether the user is somewhere near the vicinity of the tagged location etc. 

public double getDistance (double lat1, double lon1, double lat2, double lon2)
    {
        double distance = 0;
        Location locationA = new Location("A");
        locationA.setLatitude(lat1);
        locationA.setLongitude(lon1);
        Location locationB = new Location("B");
        locationB.setLatitude(lat2);
        locationB.setLongitude(lon2);
        distance = locationA.distanceTo(locationB);
        return distance;
    }




References:
http://stackoverflow.com/questions/18310126/get-the-distance-between-two-locations-in-android

No comments:

Post a Comment