Created
October 26, 2012 08:24
-
-
Save viktorbezdek/3957601 to your computer and use it in GitHub Desktop.
Calculate distance between 2 GPS coordinates
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
/** | |
* Distance between two points | |
* @param coords1 {lat: xx, lng: yy} | |
* @param coords2 {lat: xx, lng: yy} | |
* @returnd distance in meters | |
*/ | |
function _getCoordsDistance(coords1, coords2) | |
{ | |
// earth | |
var R = 6371, // km | |
lat1 = parseFloat(coords1.lat), | |
lat2 = parseFloat(coords2.lat), | |
lon1 = parseFloat(coords1.lng), | |
lon2 = parseFloat(coords2.lng); | |
// deg2rad | |
lat1 = (lat1 / 180) * Math.PI; | |
lat2 = (lat2 / 180) * Math.PI; | |
lon1 = (lon1 / 180) * Math.PI; | |
lon2 = (lon2 / 180) * Math.PI; | |
// Equirectangular approximation | |
// lower accuracy, higher performance | |
var x = (lon2-lon1) * Math.cos((lat1+lat2)/2); | |
var y = (lat2-lat1); | |
var d = Math.sqrt(x*x + y*y) * R; | |
return Math.round(d * 1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment