Created
March 17, 2013 09:11
-
-
Save rafaellyra/5180762 to your computer and use it in GitHub Desktop.
Calcula a distancia em raio entre duas coordenadas de latitude/longitude.
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 CalcRadiusDistance(lat1, lon1, lat2, lon2) { | |
var RADIUSMILES = 3961, | |
RADIUSKILOMETERS = 6373, | |
latR1 = this.deg2rad(lat1), | |
lonR1 = this.deg2rad(lon1), | |
latR2 = this.deg2rad(lat2), | |
lonR2 = this.deg2rad(lon2), | |
latDifference = latR2 - latR1, | |
lonDifference = lonR2 - lonR1, | |
a = Math.pow(Math.sin(latDifference / 2), 2) + Math.cos(latR1) * Math.cos(latR2) * Math.pow(Math.sin(lonDifference / 2), 2), | |
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)), | |
dm = c * RADIUSMILES, | |
dk = c * RADIUSKILOMETERS; | |
this.mi = this.round(dm); | |
this.km = this.round(dk); | |
} | |
CalcRadiusDistance.prototype.deg2rad = function (deg) { | |
var rad = deg * Math.PI / 180; | |
return rad; | |
}; | |
CalcRadiusDistance.prototype.round = function (x) { | |
return Math.round(x * 10) / 10; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment