Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distance measurment by the GPS coordinates

Tags:

c

math

gps

How to calculate the distance between 2 places by using the GPS coordinates?

like image 666
Vaka Kiran kumar Avatar asked May 27 '11 06:05

Vaka Kiran kumar


2 Answers

You have to use the haversine formula: Haversine formula:

R = earth’s radius (mean radius = 6,371km)

Δlat = lat2− lat1

Δlong = long2− long1

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

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

d = R.c

Where d is distance (your solution) and all the angles must 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;
}
like image 187
dLobatog Avatar answered Oct 24 '22 13:10

dLobatog


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

(Pythagorean theorem won't be enough because of earth's sphericity)

like image 42
Igor Avatar answered Oct 24 '22 11:10

Igor