How do you calculate the "Course Over Ground" from location sensor infos?

Hi guys. I'm trying to get COG (Course Over Ground) in my application while moving. I know there is a formula for this using the last position and the current position, but I am having difficulty implementing it in my project. Could any of my colleagues help me?

Welcome Alexandre.

A typical GPS receiver (for land, air, or water use) does not have any way to know what direction it (or the vehicle) is pointing, We can calculate the direction of movement by comparing successive positions to give the course over ground

You found an algorithm you are having difficulty with. If you care to share the algorithm with the community, someone might be able to help.

This might help


this COG

1 Like

You can get these easily using the LocationChanged event in the LocationSensor.if that is all you need to do for your COG algorithm. Be aware, the LS doesn't always know where it is precisely. :cry:

You can also calculate the distance between the positions using Math blocks.

Dear @Alexandre_Alves,
if what you want is simply to calculate the distance between two fixed positions you can use the following blocks:

where lat1/2 and lon1/2 are the coordinates of the two points.
The distance is in km, converted in meters for my use, by the last multiplication blocks.
(Please don't care if the procedure is called VincentyDist because it isn't actually the Vincenty formula, but this is the name I gave to the procedure in an old app of mine and I didn't change it).
Hoping it could help.

Hi Steve,

Thank you very much, I will study the paper you suggested.

In the meantime, what I'm trying to calculate is the direction I'm heading in with my vehicle. I am calculating it using the Haversine formula, with the procedures below, where previousLat/Long were recorded 5 seconds before current Lat/Long.

But I'm not getting a stable reading, even modifying the previousLat/Long update time.

Do you have any suggestions for stabilizing the reading?

Thanks Uskiara,

What I'm looking for is my direction of travel. I think the formula you presented is to determine the distance between two points.

Anyway, thank you for your contribution, as I will like to add this function to my application.

I think this extension might calculate a bearing. It also provides what you would consider more useful information. You could try the extension. I have used it and it works fairly well and believe bearing was pretty good.

if not that one, then this one

I need a better explanation of what you mean by a stable reading.

I think the issue is you are sampling too frequently or the Accuracy of your coordinates is sometimes greater than 10 m. Some people throw out any readings they get where Accuracy is greater than say 10 m in the hopes it provides increased precision.

Somewhere I wrote an algorithm to calculate a bearing using the LocationSensor. I'll look for it if you would find that useful; but not this evening.


Thank you so much Steve, for your contribution.

I'll try this extensions...

I observe a very large oscillation, up to 12°, even when driving in a straight line for a long distance.

I'll check this too...

I would really like to have some alternative to an extension if you can help me with that. But take your time, I still have some time to work on this project.

Thanks in advance,

Dear @Alexandre_Alves,
don't be scared to use the extension that @SteveJG has suggested you.
It works so good that I can fetch new data every second in my app that I use to perform rally races.
Obviously the accuracy of the GPS data depends on how many satellites are visibile at any time, but the speed at which the extension works is high enough to be used on a car.
To avoid glitches in your algorithm you can, as @SteveJG has already suggested, apply a filter to new incoming data, discarding those that are affected by an accuracy worse that 10 meters (for example). Just take care that in this case, when the accuracy is bad, and you have discarded a data, you cannot rely also on the speed relevant to that sample because it would be affected by the same uncertainty.

Here is an example

There you also can find the link to @SteveJG 's example pointing to Mekka


Thank you so much @uskiara ,

I tried one of these extensions in the past and didn't get a good result. But perhaps I should give another try with yours and @SteveJG guidelines.

Thank you @Taifun,

In fact, I can't use the LocationSensor because the device is not always pointed in the direction of the vehicle's movement. It can be on a console, for example, or on a support aimed at the pilot's view.

That's why I'll have to insist on a solution with LocationSensor.

Anyway, thank you very much for your contribution. There are many other examples on that list that will help me a lot. :wink:

Here is the algorithm I used to calculate bearing using the LocationSensor (not Orientation Sensor ) previous and current coordinates


Sorry, I don't remember if the first code image works or not but the second one does :wink:

using Formula to Find Bearing or Heading angle between two points: Latitude Longitude -

Capture a previous lat/lon from the LocationSensor.LocationChanged (where you were a little while ago :wink: ) and use the previous coordinates with your current lat/lon in the Procedure BearingAbsolute2 .

This may work for you.

Good luck.

1 Like

Hi guys,

Thank you very much for your help, it was really very constructive.

For some reason the KIO4 extension didn't work on my device. That's why I chose to maintain the algorithmic approach with the Haversine formula.

I added some input from @SteveJG into my calculations and after some long testing session I finally understood what was wrong. I will share the solution here for others to access.

The LocationSensor update time and distance were too small for the speed at which I am operating. So I created a settings section in my app so that the user can adjust these parameters, according to the speed at which their vehicle will operate.

With a little patience, you can get stable data from a slow walk to a car race. :wink:

Here is my test application, for others to study if they encounter the same problem. With a short click on the plus and minus buttons you change the LocationSensor time_interval. With a long click you change the distance_interval.

Thanks again @SteveJG, @Taifun and @uskiara.

COG_1.aia (22.5 KB)


Dear @Alexandre_Alves, nice to read that and thanks for having shared your results,
It's pretty weird that the NMEA extension does not work for you. Anyway, also for me the LocationSensor blocks featured by AI2 were working fine (but I needed the precise GPS timestamp also, and to this purpose I use also the KIO4 extension).

In detail, the settings that I used for the LocationSensor were:
DistanceInterval = 0 means that the GPS does not wait to reach a delta to raise a new pair of coordinates, while 1000 as TimeInterval means that every second the GPS shall make availabe new data as well.
I reckon that these settings seem to be contradictory with respect to the expected behaviour of the GPS (normally not so fast), but they work fine for me.

To validate the quality of the coordinates I checked these values:
As you can see the accuracy limit that I experimented to be a good one for my use is 20 (meters), while be careful that also when accuracy is = 0 the fixed point is not reliable.

With these settings I can reach a good updating rate also for this application:

:rofl: :rofl: :rofl:

1 Like

Wow... This is a very good aplication for an app :joy: Is that a Lancia? What year?

My racing pod is a little slower than yours :wink:

So I was facing problems with the stability of the numbers (in 1 second at 3-4kn, the difference between positions is no greater than 2 meters) this was creating a difference of up to 18° every second. With a Time Interval of around 5200ms, my reading is a bit more stable, but not enough for a perfectionist.

To further increase this stability and reduce update time, recently I added an averaging calculation. Now I have an incredibly stable reading and a very interesting update time, even on days with low wind :wink:

Once again I am leaving a copy of the optimized code here for others to consult.

COG_AVG.aia (8.8 KB)

Thank you again @uskiara and have great trips with your beautiful Lancia.

1 Like

Dear @Alexandre_Alves,
what a beautiful way to use an app !
In my "first life" ('til I was 40) I worked also in the design of electronic devices for ships and, at that time the GPS was still at the beginning of its life, so ships were still using gyrocompasses or magnetic compasses, and the GPS was not precise enough, neither it was capable to "give the north" to maintain a course. At that time it was necessary to use a "differential" GPS to have such information.
But, as I said, it was a life ago....
Yes, the car is a Lancia Fulvia 1300cc Rally version, year 1967 (pretty old, but younger than me :rofl:).
Ciao, ciao!

1 Like

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