Skip to content Skip to sidebar Skip to footer

How To Calculate Distance Using Coordinates From Locationmanager

I am using LocationManager to get the values of Latitude and Longitude of a user. These values are updated regularly to a database. Now, i want to find out the distance between two

Solution 1:

You can have a look at the Location class. You can set the longitude and latitude and there is a distanceTo method that you can use

Solution 2:

Ou pode utilizar este metodo que eu desenvolvi... Or you can use this method I developed...

publicstatic Double distance(latitudeA, latitudeB, longitudeA, longitudeB){
        double radius = 3958.75;
        double dlat = ToRadians(Double.parseDouble(String.valueOf(latitudeB))
                - Double.parseDouble(String.valueOf(latitudeA)));
        double dlon = ToRadians(Double.parseDouble(String.valueOf(longitudeB))
                - Double.parseDouble(String.valueOf(longitudeA)));
        double a = Math.sin(dlat / 2)
                * Math.sin(dlat / 2)
                + Math.cos(ToRadians(Double.parseDouble(String.valueOf(latitudeA))))
                * Math.cos(ToRadians(Double.parseDouble(String.valueOf(latitudeB)))) * Math.sin(dlon / 2)
                * Math.sin(dlon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        Double d = radius * c;
        double meterConversion = 1609.00;
        return d * meterConversion;
    }

    privatestaticdoubleToRadians(double degrees){
        double radians = degrees * 3.1415926535897932385 / 180;
        return radians;
    }

Post a Comment for "How To Calculate Distance Using Coordinates From Locationmanager"