0

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.

Rahul Kalidindi
  • 4,666
  • 14
  • 56
  • 92

2 Answers2

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

ccheneson
  • 49,072
  • 8
  • 63
  • 68
  • Thanks for the reply... I have also found a function here(http://android.voxisland.com/code_examples/How_to_calculate_the_distance_between_two_GPS_coordinates.rhtml)... I ll use both and find out which one might yield better accuracy. – Rahul Kalidindi Dec 28 '10 at 09:46
0

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