# Thread: Location API: distanceTo function

1. Hey guys,

Has anyone used the distanceTo function? When applying distanceTo to these QGeoCoordinate objects (while I stand still) I get these results:

Code:
```-34.555153 -58.483817 -----> -34.555158 -58.483807 -----> distanceTo: 4.660631 meters.
-34.555158 -58.483807 -----> -34.555171 -58.483806 -----> distanceTo: 1.065398 meters.
-34.555171 -58.483806 -----> -34.554278 -58.484111 -----> distanceTo: 55.065398 meters (!!!)```
The distanceTo help says:

Returns the distance (in meters) from this coordinate to the coordinate specified by other. Altitude is not used in the calculation.

This calculation returns the great-circle distance between the two coordinates, with an assumption that the Earth is spherical for the purpose of this calculation.

Returns 0 if the type of this coordinate or the type of other is QGeoCoordinate::InvalidCoordinate.
To me, it looks like it just returns random values.

The distanceTo algorithm implemented in qgeocoordinate.cpp is

Code:
```    // Haversine formula
double dlat = qgeocoordinate_degToRad(other.d->lat - d->lat);
double dlon = qgeocoordinate_degToRad(other.d->lng - d->lng);
double y = sin(dlat / 2.0) * sin(dlat / 2.0)
+ cos(qgeocoordinate_degToRad(d->lat))
* cos(qgeocoordinate_degToRad(other.d->lat))
* sin(dlon / 2.0) * sin(dlon / 2.0);
double x = 2 * atan2(sqrt(y), sqrt(1 - y));
return qreal(x * qgeocoordinate_EARTH_MEAN_RADIUS * 1000);```
Do anyone has the slighest idea whats going on?

Thanks in advance!

Best regards,
Matias

2. Hi,

GPS typically has an error of up to 100m, so that looks fairly typical. If the QGeoPositionInfo objects have the horizontal accuracy attribute set you could try printing that out as well to have something to compare the distance to.

The distance formula used in QGeoCoordinate is the Haversine formula for computing great circle distances - it's a pretty good method and is a fairly standard when it comes to calculating distances between points on the globe. I get different distances with the values you provided - although that's most likely because the latitudes / longitudes you printed have been truncated when they were printed.

Cheers,

Dave

3. You're totally right! I never thought a cellphone's GPS would be so imprecise. But I guess that given that they're mainly thought for car drivers its logical.

About the equation, correct me if I'm wrong (as I'm not a mathematician), but that looks more like the Great-circle distance formula, not the Haversine:

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

I actually ended up implementing Haversine as from my own pseudo-sapiens mathematic standpoint , I thought it was more precise.

This is how the Haversine looks in my code:

Code:
```double GeoDistanceCalc::distanceTo(const QGeoCoordinate &from, const QGeoCoordinate &to)
{
double R = 6371; // 3960 for miles
double lat = deg2rad(to.latitude() - from.latitude());
double lon = deg2rad(to.longitude() - from.longitude());
double a = sin(lat / 2.f) * sin(lat / 2.f) + cos(deg2rad(from.latitude())) * cos(deg2rad(to.latitude())) * sin(lon / 2.f) * sin(lon / 2.f);
double c = 2.f * asin(std::min((double)1.f, sqrt(a)));
double d = R * c;

return d * 1000.f;
}```
I don't know, perhaps it'll be useful for somebody

4. I can't find difference between haversine and one used in qgeocoordinate.
Haversine
Code:
```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```
Formula in qgeocoordinate
Code:
```double dlat = qgeocoordinate_degToRad(other.d->lat - d->lat);
double dlon = qgeocoordinate_degToRad(other.d->lng - d->lng);
double y = sin(dlat / 2.0) * sin(dlat / 2.0)
+ cos(qgeocoordinate_degToRad(d->lat))
* cos(qgeocoordinate_degToRad(other.d->lat))
* sin(dlon / 2.0) * sin(dlon / 2.0);
double x = 2 * atan2(sqrt(y), sqrt(1 - y));
return qreal(x * qgeocoordinate_EARTH_MEAN_RADIUS * 1000);```
But if there is a difference please enlighten me because I'm working with distanceTo() function also.

5. Check this:

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

It looks more like the Vincenty's variation rather than Haversine. And now that I understand it more properly, I think QtMobility's version is more accurate, I must have been had the wrong impression because of early errors in my code.

Again, I don't have enough mathematical knowledge to properly analyze these equations, but for one instance, there is no arc tangent calculation on Haversine's.

Best!

6. In a future versions we're probably going to add a few new distance functions (to calculate coordinate a given distance away from other coordinates), and at the same time we'll move from using arctangent to arcsine in the distanceTo function to improve it's behaviour for very large distances. The wiki pages you linked to Haversine makes passing reference to arctangent (and why we're looking at moving away from it).

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.