Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created September 7, 2022 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/9bd01d0cf1549a645fb5f2de75601ceb to your computer and use it in GitHub Desktop.
Save parzibyte/9bd01d0cf1549a645fb5f2de75601ceb to your computer and use it in GitHub Desktop.
const calcularDistanciaEntreDosCoordenadas = (lat1, lon1, lat2, lon2) => {
// Convertir todas las coordenadas a radianes
lat1 = gradosARadianes(lat1);
lon1 = gradosARadianes(lon1);
lat2 = gradosARadianes(lat2);
lon2 = gradosARadianes(lon2);
// Aplicar fórmula
const RADIO_TIERRA_EN_KILOMETROS = 6371;
let diferenciaEntreLongitudes = (lon2 - lon1);
let diferenciaEntreLatitudes = (lat2 - lat1);
let a = Math.pow(Math.sin(diferenciaEntreLatitudes / 2.0), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(diferenciaEntreLongitudes / 2.0), 2);
let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return RADIO_TIERRA_EN_KILOMETROS * c;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment