0

Possible Duplicate:
How do I calculate distance between two latitude longitude points?

How do you calculate the distance between two points that are in latitude and longitude values in JAVA?

Thanks

Community
  • 1
  • 1
User05
  • 121
  • 4
  • 10

1 Answers1

2

Take a look at this: http://www.zipcodeworld.com/samples/distance.java.html

Because the above link is broken look at this: Calculate distance between two latitude-longitude points? (Haversine formula).

A summary of this post:

public final static double AVERAGE_RADIUS_OF_EARTH = 6371;
public int calculateDistance(double userLat, double userLng, double venueLat, double venueLng) {

    double latDistance = Math.toRadians(userLat - venueLat);
    double lngDistance = Math.toRadians(userLng - venueLng);

    double a = (Math.sin(latDistance / 2) * Math.sin(latDistance / 2)) +
                    (Math.cos(Math.toRadians(userLat))) *
                    (Math.cos(Math.toRadians(venueLat))) *
                    (Math.sin(lngDistance / 2)) *
                    (Math.sin(lngDistance / 2));

    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

    return (int) (Math.round(AVERAGE_RADIUS_OF_EARTH * c));

}
Community
  • 1
  • 1
Manuel
  • 10,153
  • 5
  • 41
  • 60