-
-
Save salcode/6db160f5f2b98529d1f2518afce0016d to your computer and use it in GitHub Desktop.
JavaScript function to calculate the approximate distance between two coordinates (latitude, longitude) on Earth.
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
/** | |
* Calculate approximate distance between two coordinates on Earth. | |
* | |
* @see https://salferrarello.com/javascript-distance-between-locations | |
* | |
* Example: | |
* | |
* LatLngDist( 40.3215666, -75.9828464, 33.5417647, -112.169801 ) | |
* | |
* Result: | |
* 2036 miles (or 3277 km) | |
* @see https://stackoverflow.com/questions/21279559/geolocation-closest-locationlat-long-from-my-position. | |
*/ | |
function LatLngDist( lat1, lng1, lat2, lng2 ) { | |
// Convert Degress to Radians | |
function Deg2Rad(deg) { | |
return deg * Math.PI / 180; | |
} | |
function PythagorasEquirectangular(lat1, lon1, lat2, lon2) { | |
lat1 = Deg2Rad(lat1); | |
lat2 = Deg2Rad(lat2); | |
lon1 = Deg2Rad(lon1); | |
lon2 = Deg2Rad(lon2); | |
// var R = 6371; // km | |
var R = 3959; // miles | |
var x = (lon2 - lon1) * Math.cos((lat1 + lat2) / 2); | |
var y = (lat2 - lat1); | |
var d = Math.sqrt(x * x + y * y) * R; | |
return d; | |
} | |
return PythagorasEquirectangular( lat1, lng1, lat2, lng2 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment