Created
July 4, 2011 17:48
-
-
Save gyuque/1063693 to your computer and use it in GitHub Desktop.
calc distance from (lng1,lat1) to (lng2,lat2)
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
function distanceLL(x1, y1, x2, y2) | |
{ | |
var DEG2RAD = Math.PI / 180.0; | |
x1 *= DEG2RAD; | |
y1 *= DEG2RAD; | |
x2 *= DEG2RAD; | |
y2 *= DEG2RAD; | |
var dx = Math.abs(x2-x1); | |
var dy = Math.abs(y2-y1); | |
var phi = dy*0.5 + y1; | |
var S = Math.sin(phi); | |
var M = 6335439.0 / (Math.sqrt( Math.pow( (1- 0.006674*S*S) , 3) )); | |
var N = 6378137.0 / Math.sqrt( 1- 0.006674*S*S ); | |
return Math.sqrt( Math.pow(M*dy, 2) + Math.pow(N*Math.cos(phi)*dx ,2) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment