# Algorithm to calculate speed from two GPS latitude and longitude points and time difference

I found this good description of how to calculate an approximate speed over ground given two latitude and longitude coordinates and a time difference:

It involves first plotting your two GPS points on a spherical model of the earth, calculating the angle between them using a dot product, calculating distance using this angle and the earth’s radius and finally dividing by the elapsed time to approximate the speed.

Thanks to bpiguy for the answer!

Now I just have to implement it….

Update:

I finally got around to writing some code to play with this distance calculation:

This function takes the latitude and longitude in signed decimal format and returns the distance in metres, I have left in the ‘r’s for clarity but if efficiency is what you’re after then they can be removed as the original post suggests…

Now once you have the distance between the points you can estimate the average speed by dividing this distance by the time between the two position measurements like this:

This code assumes that p1 and p2 represent the first and second measured GPS positions and that the time-stamp recorded at each is enumerated in milliseconds, it calculates both metres per second and kilometres per hour. It is important to note that this is only an estimate of the average speed between the two points and its accuracy will depend on various factors including the distance and time elapsed between the two GPS measurements.

Tags:
7 replies
1. Varun says:

Any idea how to get speed?

2. bogi says:

thank’s…..

3. Ravin says:

What is the type “auto”? in your declaration:
auto time_s = (p2.timestamp – p1.timestamp) / 1000.0;

• Kevin Godden says:

Hi, The ‘auto’ allows the c++ (11) compiler to infer the type of the expression from what’s on the right-hand side of the equals, so in this case, what’s on the right hand side are doubles time_s will end up being a double too. This is all done at compile time and it really just cuts down on some typing! In c# the equivalent is the var keyword. In the example you quoted I really shouldn’t have used it as I used the ‘double’ type explicitly everywhere else and so the example ends up looking a little bit inconsistent!

http://en.cppreference.com/w/cpp/language/auto

So in summary its is very handy but can be overused!

• Ravin says:

Interesting, thanks for sharing!!

4. Micha? says:

hello

in what system u need to input coordinates

• KevinG says:

Hi, the input coordinates are in decimal degrees, see: https://en.wikipedia.org/wiki/Decimal_degrees