Last active
August 4, 2016 14:51
-
-
Save yayoF/a87ba8f0902a696f89e1 to your computer and use it in GitHub Desktop.
Function: Distance between 2 points. Char unit is to know what metric to return. K returns kilometers. If the distance is less than 1km you have to return in meters. Returns strings (e.g.: 9 kms o 360 mts.)
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
private double distance(double startLat, double startLon, double endLat, double endLon, char unit){ | |
double theta = startLon - endLon; | |
double dist = Math.sin(deg2rad(startLat)) * Math.sin(deg2rad(endLat)) + Math.cos(deg2rad(startLat)) * Math.cos(deg2rad(endLat)) * Math.cos(deg2rad(theta)); | |
dist = Math.acos(dist); | |
dist = rad2deg(dist); | |
dist = dist * 60 * 1.1515; | |
if (unit == 'K') { | |
dist = dist * 1.609344; | |
} else if (unit == 'N') { | |
dist = dist * 0.8684; | |
} | |
return (dist); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment