Skip to content

Instantly share code, notes, and snippets.

@yradunchev
Created November 22, 2012 08:58
Show Gist options
  • Select an option

  • Save yradunchev/4130083 to your computer and use it in GitHub Desktop.

Select an option

Save yradunchev/4130083 to your computer and use it in GitHub Desktop.
Calculating distance between two GPS coordinates. Result in meters.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define ER (6371007.2)
#define RAD(degrees) (degrees * (M_PI / 180))
main(int argc, char **argv)
{
double dLa1, dLo1, dLa2, dLo2;
double dLa, dLo, dA, dM;
dLa1 = RAD(atof(argv[1]));
dLo1 = RAD(atof(argv[2]));
dLa2 = RAD(atof(argv[3]));
dLo2 = RAD(atof(argv[4]));
dLa = dLa2 - dLa1;
dLo = dLo2 - dLo1;
dA = sin(dLa/2) * sin(dLa/2) + cos(dLa1) * cos(dLa2) * sin(dLo/2) * sin(dLo/2);
dM = (ER * (2 * atan2(sqrt(dA), sqrt(1-dA))));
printf("%.5lf\n", dM);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment