Tuesday 17 September 2013

Calculate distance between two points latitude and longitude in Android

Use this method with following parameters

public double Calculate_Distance(Double latB, Double longB,
            Double latA, Double longA) {
        double distance;
        Location locationA = new Location("point A");
        locationA.setLatitude(latA);
        locationA.setLongitude(longA);
        Location locationB = new Location("point B");
        locationB.setLatitude(latB);
        locationB.setLongitude(longB);
        distance = locationA.distanceTo(locationB);
        return distance;
    }

Constructs the new Location with a named provider.


Then use this below code in your activity


Double total_distance = Calculate_Distance(latB,longB, latA,
                                                longA);

Returns the approximate distance in meters between this location.

No comments:

Post a Comment