How to calculate distance using coordinates from LocationManager

谁都会走 提交于 2020-01-17 04:49:07

问题


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 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.


回答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




回答2:


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


来源:https://stackoverflow.com/questions/4544972/how-to-calculate-distance-using-coordinates-from-locationmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!