Skip to content

Instantly share code, notes, and snippets.

@zurche
Created June 6, 2016 19:31
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/efda911027d371787ce4bd476a3e753d to your computer and use it in GitHub Desktop.
Save zurche/efda911027d371787ce4bd476a3e753d 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