Droid_Interceptor
Droid_Interceptor

Reputation: 653

Calculate distance between two points

I'm making an application for tracking a user as they run, cycle or walk and display it to the screen. I know that there is distanceBetween function built into android but is this accurate enough for a fittness application? I was considering using Haversine formula or other such formula for calculating distances between points the only problem I see about these formulas is that it usually is straight line or as the crow flys distances. Does anyone have an idea about this?

Upvotes: 2

Views: 1720

Answers (2)

umassthrower
umassthrower

Reputation: 1397

Tracking their route as they walk is going to involve a series of way points. If you sample at say 1 way point every 10 seconds then you can calculate the distance between the previous point and the new point using either a crow-flys technique like haversine or just make some webservice requests to an external service like google maps and their distance matrix which can give you the snapped-to-streets distance between 2 points using suggested best path.

http://code.google.com/apis/maps/documentation/distancematrix/

You just have to make sure that your sample rate isn't too high or you can go over the 2500 API calls/24-hour-period rate limiter (which is per IP I believe). But 2500 will give you 7 hours at 1 sample per 10 seconds, or you can try a lower rate like 1 per 35 seconds and risk having the route be guessed wrong, but guarantee that your device won't go over 2500 samples.

Personally, if location polling is free, I would just go with distanceBetween and poll every 1-2 seconds. That should be short enough that the accuracy of GPS becomes your biggest source of error.

Upvotes: 1

Yauraw Gadav
Yauraw Gadav

Reputation: 1746

The Haversine formula is accurate for most distances, but it suffers from rounding errors when the points are (nearly) antipodal. The following formula is accurate for all distances.

> double delta = G1 - G2;  
>     double p1 = cos(L2) * sin(delta);
>     double p2 = cos(L1) * sin(L2) - sin(L1) * cos(L2) * cos(delta);  
>     double p3 = sin(L1) * sin(L2) + cos(L1) * cos(L2) * cos(delta);
>     distance = 60 * Math.atan2(Math.sqrt(p1*p1 + p2*p2), p3);

Here's an example and the implementation.

resource : Here

Upvotes: 1

Related Questions