# GPS Distance Formula

So i’ve been trying to make an arduino based distance measurement device using coordinates from GPS module.
i’ve done a little bit of research and found Haversine formula and several tutorials using tinyGPS++ library function called “distance_between” by Maarten Lamers to do it.

I was wondering if the distance formula used in tinyGPS++library is using Haversine formula or a part of it.
if not which would be more accurate in calculating distance between 2 coordinate ?
thanks

I found the haversine formula in this site Calculate distance and bearing between two Latitude/Longitude points using haversine formula in JavaScript
Here’s the code from the library

``````float TinyGPS::distance_between (float lat1, float long1, float lat2, float long2)
{
// returns distance in meters between two positions, both specified
// as signed decimal-degrees latitude and longitude. Uses great-circle
// distance computation for hypothetical sphere of radius 6372795 meters.
// Because Earth is no exact sphere, rounding errors may be up to 0.5%.
// Courtesy of Maarten Lamers
float sdlong = sin(delta);
float cdlong = cos(delta);
float slat1 = sin(lat1);
float clat1 = cos(lat1);
float slat2 = sin(lat2);
float clat2 = cos(lat2);
delta = (clat1 * slat2) - (slat1 * clat2 * cdlong);
delta = sq(delta);
delta += sq(clat2 * sdlong);
delta = sqrt(delta);
float denom = (slat1 * slat2) + (clat1 * clat2 * cdlong);
delta = atan2(delta, denom);
return delta * 6372795;
}
``````

A minor point, but the code you posted is not from the TinyGPS++ library, rather it is from the TinyGPS library

Something you could figure out for yourself.

Go to url=https://github.com/mikalhart/TinyGPSPlus/blob/master/src/TinyGPS%2B%2B.cpp]TinyGPS++ library[/url] find the distance thingy… Oh look!

I just went ahead and posted the code so you don’t have to go looking for it.

``````double TinyGPSPlus::distanceBetween(double lat1, double long1, double lat2, double long2)
{
// returns distance in meters between two positions, both specified
// as signed decimal-degrees latitude and longitude. Uses great-circle
// distance computation for hypothetical sphere of radius 6372795 meters.
// Because Earth is no exact sphere, rounding errors may be up to 0.5%.
// Courtesy of Maarten Lamers
double sdlong = sin(delta);
double cdlong = cos(delta);
double slat1 = sin(lat1);
double clat1 = cos(lat1);
double slat2 = sin(lat2);
double clat2 = cos(lat2);
delta = (clat1 * slat2) - (slat1 * clat2 * cdlong);
delta = sq(delta);
delta += sq(clat2 * sdlong);
delta = sqrt(delta);
double denom = (slat1 * slat2) + (clat1 * clat2 * cdlong);
delta = atan2(delta, denom);
return delta * 6372795;
}

double TinyGPSPlus::courseTo(double lat1, double long1, double lat2, double long2)
{
// returns course in degrees (North=0, West=270) from position 1 to position 2,
// both specified as signed decimal-degrees latitude and longitude.
// Because Earth is no exact sphere, calculated course may be off by a tiny fraction.
// Courtesy of Maarten Lamers
double a1 = sin(dlon) * cos(lat2);
double a2 = sin(lat1) * cos(lat2) * cos(dlon);
a2 = cos(lat1) * sin(lat2) - a2;
a2 = atan2(a1, a2);
if (a2 < 0.0)
{
a2 += TWO_PI;
}
return degrees(a2);
}
``````

There you go, so does it?

In what sense do you mean more 'accurate' ?

What distance error are you seeing from the the TinyGPS distance function ?

well i haven't actually tried either formula, its just that i've saw a couple of old post where people compare the accuracy of the distance calculation between 2 point of coordinates using the tinygps++ library function, neogps, or using the haversine formula.
I was just wondering whether the tinygps++ library distance function is even using the same haversine formula as im not really great with the math.

Might be worth comparing a known haversine calculator, which you can find on-line, with the same co-ordinates into one of the TinyGPS functions.

This is an excellent reference and on line calculator for GPS and navigation equations: Calculate distance and bearing between two Latitude/Longitude points using haversine formula in JavaScript

The accuracy of the calculations will depend on whether variable type float or double is used. The AVR based Arduinos do no support double.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.