GPS distance measurement

How to calculate the distance between two places using GPS coordinates?

+5
source share
2 answers

You should use the haversine formula: Haversin formula:

R = radius of the earth (average radius = 6,371 km)

Δlat = lat2-lat1

Δlong = long2- long1

a = sin² (Δlat / 2) + cos (lat1) .cos (lat2) .sin² (Δlong / 2)

c = 2.atan2 (√a, √ (1-a))

d = Rc

Where d is the distance (your decision) and all angles should be in radians

Look for the haversine library and in C:

#include <math.h>
#include "haversine.h"

#define d2r (M_PI / 180.0)

//calculate haversine distance for linear distance
double haversine_km(double lat1, double long1, double lat2, double long2)
{
    double dlong = (long2 - long1) * d2r;
    double dlat = (lat2 - lat1) * d2r;
    double a = pow(sin(dlat/2.0), 2) + cos(lat1*d2r) * cos(lat2*d2r) * pow(sin(dlong/2.0), 2);
    double c = 2 * atan2(sqrt(a), sqrt(1-a));
    double d = 6367 * c;

    return d;
}

double haversine_mi(double lat1, double long1, double lat2, double long2)
{
    double dlong = (long2 - long1) * d2r;
    double dlat = (lat2 - lat1) * d2r;
    double a = pow(sin(dlat/2.0), 2) + cos(lat1*d2r) * cos(lat2*d2r) * pow(sin(dlong/2.0), 2);
    double c = 2 * atan2(sqrt(a), sqrt(1-a));
    double d = 3956 * c; 

    return d;
}
+9
source

http://en.wikipedia.org/wiki/ Great-circle_distance

(Pythagorean theorem will not be sufficient due to terrestrial sphericity)

+3
source

All Articles