# Thread: GPS: Keeping track of distance

1. ## GPS: Keeping track of distance

Is there any library or api function that can return a total distance stamp. Just like a timestamp but with the total distance instead so that i can take a 'distance stamp' at the start and then use this value to calculate the distance traveled since the application started.

Example:
Code:
`distance_since_start = current_distance - start_distance`
I want to make an application and part of the functionality is to keep track of the distance since the user pressed start. Is there any sample code for doing this or any good libraries?

2. ## Re: GPS: Keeping track of distance

Originally Posted by pure_x01
Is there any library or api function that can return a total distance stamp. Just like a timestamp but with the total distance instead so that i can take a 'distance stamp' at the start and then use this value to calculate the distance traveled since the application started.
You probably have to calculate it by your self. Fortunately it is quite easy. Just positioning.position() and then calculate the distance between current and previous point e.g. by using Calculate.distance(lat1, lon1, lat2, lon2) function found from here:

Example:

Code:
```>>> import Calculate
>>> Calculate.distance(60.2000,25.0000,60.2001,25.0000)
11.111696663076945```
Add the result to the total_distance variable or something.

NOTE: the result is accurate enough if the distance is less than a few thousand meters (I guess).

3. ## Re: GPS: Keeping track of distance

Originally Posted by pure_x01
Is there any library or api function that can return a total distance stamp. Just like a timestamp but with the total distance instead so that i can take a 'distance stamp' at the start and then use this value to calculate the distance traveled since the application started.
Code:
`distance_since_start = current_distance - start_distance`
Sorry,

One of the days, when I'm still in work mode: what do you mean with "distance travelled"? Would it be just like you say distance between starting point A and current position B - as a direct line? Or would you mean just like you say bit later the distance travelled when moving from A to B - including all the turns and wanderings meter by meter?

Think carefully what you ask, because you might get it

Btw calculating a direct line gets complicated when distance is long enough. You have to consider the fact that earth is a globe and distance is actually an arc... If it gets too complicated, you could check this site for possible help

Cheers,

--jouni

4. ## Re: GPS: Keeping track of distance

Originally Posted by JOM
Sorry,

One of the days, when I'm still in work mode: what do you mean with "distance travelled"? Would it be just like you say distance between starting point A and current position B - as a direct line? Or would you mean just like you say bit later the distance travelled when moving from A to B - including all the turns and wanderings meter by meter?

...
What i am after is the total distance traveled since a certain point in time including turns and in the smallest possible unit (probably meters). The starting point for the counter could be from when the phone starts or when the program was started.

Example:
Code:
```
if __name__ == "__main__":
start_dist = gps.global_distance()
last_dist = start_dist
while True:
time.sleep(1)
glob_dist = gps.global_distance()
current_dist = glob_dist - start_dist
print 'you have traveled %d meters since the app started' \
% (current_dist)

# calculate the speed, since the update is per second
# the distance since the last update is the speed
dist_since_update = glob_dist - last_dist
last_dist = glob_dist
print '%d meters per second' % (dist_since_update)```
Im not sure if the code above make sense since it is very early
and i am on vacation and could not sleep, but it is the global_distance
that is the interesting function. :-)

The code above would work with any return value the first time the global_distance was called. So if the global distance counters starts counting on the first invocation or any point in time before the first invocation does not matter as long as the subsequent calls returns a value that is the same or has increased since the first call.

It would be a nice to have method for apps that just tracks distance or calculates current speed.

Thanx

Regards
Per

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•