开发者

How to calculate distance using coordinates from LocationManager

开发者 https://www.devze.com 2023-02-01 04:44 出处:网络
I am using LocationManager to get the values of Latitude and Longitude of a user. These values are updated regularly to a database.

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 w开发者_Go百科ant to find out the distance between two users basing on the stored Latitude and Longitude values. I want to display a message when distance between two users is less than (say 100 meters). Can anyone please guide me with a tutorial or some sample code of how to achieve it.


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


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

public static 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;
    }

    private static double ToRadians(double degrees) {
        double radians = degrees * 3.1415926535897932385 / 180;
        return radians;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消