Skip to content

Instantly share code, notes, and snippets.

@PauloLuan
Created April 23, 2014 17:28
Show Gist options
  • Save PauloLuan/11225131 to your computer and use it in GitHub Desktop.
Save PauloLuan/11225131 to your computer and use it in GitHub Desktop.
/**
* Calculate the distance between two geopoints (Latitude and Longitude)
* in WGS84
*
* @param latitudeOne
* @param longituteOne
* @param latituteTwo
* @param longituteTwo
*
* @return distance the distance in kilometers
*/
public static double calculateDistance(
double latitudeOne,
double longituteOne,
double latituteTwo,
double longituteTwo) {
double AVERAGE_RADIUS_OF_EARTH = 6371;
double latDistance = Math.toRadians(latitudeOne - latituteTwo);
double lngDistance = Math.toRadians(longituteOne - longituteTwo);
double a = (Math.sin(latDistance / 2) * Math.sin(latDistance / 2)) + (Math.cos(Math.toRadians(latitudeOne))) * (Math.cos(Math.toRadians(latituteTwo))) * (Math.sin(lngDistance / 2)) * (Math.sin(lngDistance / 2));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double distance = AVERAGE_RADIUS_OF_EARTH * c;
return distance;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment