Skip to content

Instantly share code, notes, and snippets.

@yayoF
Last active August 4, 2016 14:51
Show Gist options
  • Save yayoF/a87ba8f0902a696f89e1 to your computer and use it in GitHub Desktop.
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.)
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