Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Calcula a distancia em raio entre duas coordenadas de latitude/longitude.
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
You can’t perform that action at this time.