Skip to content

Instantly share code, notes, and snippets.

@zurche
Last active November 25, 2016 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zurche/c9518f80ce2405c04bbcdaee0ff208b6 to your computer and use it in GitHub Desktop.
Save zurche/c9518f80ce2405c04bbcdaee0ff208b6 to your computer and use it in GitHub Desktop.
/**
* This helper is used to calculate the distance between two Latitude/Longitude points in kilometers.
*/
public class LatLonDistanceCalculator {
private static final int EARTH_RADIUS_IN_KILOMETERS = 6371;
public static float calculateDistance(Point pointA,
Point pointB) {
double dLat = Math.toRadians(pointB.getLatitude()
- pointA.getLatitude());
double dLng = Math.toRadians(pointB.getLongitude()
- pointA.getLongitude());
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(Math.toRadians(pointA.getLatitude())) *
Math.cos(Math.toRadians(pointB.getLatitude())) *
Math.sin(dLng / 2) * Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return (float) (EARTH_RADIUS_IN_KILOMETERS * c);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment