Created
October 2, 2019 13:43
-
-
Save igoventura/def23f9fdcb84f4f3fe319feec357405 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
import java.math.BigDecimal; | |
public class DistanceCalculator { | |
/** | |
* Calculates distance between to points and returns in meters. | |
* | |
* @param latitude1 | |
* @param longitude1 | |
* @param latitude2 | |
* @param longitude2 | |
* @return | |
*/ | |
public static BigDecimal distance(BigDecimal latitude1, BigDecimal longitude1, BigDecimal latitude2, BigDecimal longitude2) { | |
if (latitude1.equals(latitude2) && latitude1.equals(longitude2)) { | |
return BigDecimal.ZERO; | |
} | |
else { | |
double lat1 = latitude1.doubleValue(); | |
double lon1 = longitude1.doubleValue(); | |
double lat2 = latitude2.doubleValue(); | |
double lon2 = longitude2.doubleValue(); | |
double theta = lon1 - lon2; | |
double dist = Math.sin(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2)) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(theta)); | |
dist = Math.acos(dist); | |
dist = Math.toDegrees(dist); | |
dist = dist * 60 * 1.1515; | |
return BigDecimal.valueOf(dist); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment