Created
June 4, 2016 14:03
-
-
Save nathanbarrett/bbdc62ba826005d6ac394c169dce9848 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /* | |
| * Gets the distance in meters from two coordinate objects | |
| * | |
| * @params coord1 (object|required), coord2 (object|required) | |
| * | |
| * @returns int meters | |
| */ | |
| function distanceBetweenCoords(coord1, coord2){ | |
| var R = 6371000; // earth's radius in meters | |
| var x1 = coord2.latitude - coord1.latitude; | |
| var dLat = x1 * Math.PI / 180; | |
| var x2 = coord2.longitude - coord1.longitude; | |
| var dLon = x2 * Math.PI / 180; | |
| var lat1 = coord1.latitude * Math.PI / 180; | |
| var lat2 = coord2.latitude * Math.PI / 180; | |
| var a = Math.sin(dLat/2) * Math.sin(dLat/2) + | |
| Math.cos(lat1) * Math.cos(lat2) * | |
| Math.sin(dLon/2) * Math.sin(dLon/2); | |
| var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); | |
| return R * c; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment