Last active
November 25, 2016 13:46
-
-
Save zurche/c9518f80ce2405c04bbcdaee0ff208b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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