Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created January 2, 2021 06:40
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/42d487b27e41c34859c1b24e60e2631e to your computer and use it in GitHub Desktop.
Save parzibyte/42d487b27e41c34859c1b24e60e2631e to your computer and use it in GitHub Desktop.
/*
Recibe dos pares de coordenadas en grados (así como las da Google maps)
Regresa la distancia que hay entre esos dos puntos en Kilómetros
*/
double calcularDistanciaEntreDosCoordenadas(double lat1, double lon1, double lat2, double lon2)
{
// Convertir todas las coordenadas a radianes
lat1 = gradosARadianes(lat1);
lon1 = gradosARadianes(lon1);
lat2 = gradosARadianes(lat2);
lon2 = gradosARadianes(lon2);
// Aplicar fórmula
double RADIO_TIERRA_EN_KILOMETROS = 6371;
double diferenciaEntreLongitudes = (lon2 - lon1);
double diferenciaEntreLatitudes = (lat2 - lat1);
double a = pow(sin(diferenciaEntreLatitudes / 2.0), 2) + cos(lat1) * cos(lat2) * pow(sin(diferenciaEntreLongitudes / 2.0), 2);
double c = 2 * atan2(sqrt(a), sqrt(1 - a));
double d = RADIO_TIERRA_EN_KILOMETROS * c;
return d;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment