  # Obtain direction from course heading

Discussion in 'Code Samples & Tips' started by Steve, Jun 9, 2007.

1. The following function returns a string direction such as "north," "east southeast," etc from a heading between 0 and 360 degrees. You can use this with the GPS library to determine your direction.

Sub getDirection(COG)
Dim direction
if COG >= 0 And COG <= 11.25 Then direction = "north"
if COG > 348.75 and COG <= 360 Then direction = "north"
if COG > 11.25 and COG <= 33.75 Then direction = "north northeast"
if COG > 33.75 And COG <= 56.25 Then direction = "northeast"
if COG > 56.25 And COG <= 78.75 Then direction = "east northeast"
if COG > 78.75 And COG <= 101.25 Then direction = "east"
if COG > 101.25 And COG <= 123.75 Then direction = "east southeast"
if COG > 123.75 And COG <= 146.25 Then direction = "southeast"
if COG > 146.25 And COG <= 168.75 Then direction = "south southeast"
if COG > 168.75 And COG <= 191.25 Then direction = "south"
if COG > 191.25 And COG <= 213.75 Then direction = "south southwest"
if COG > 213.75 And COG <= 236.25 Then direction = "southwest"
if COG > 236.25 And COG <= 258.75 Then direction = "west southwest"
if COG > 258.75 And COG <= 281.25 Then direction = "west"
if COG > 281.25 And COG <= 303.75 Then direction = "west northwest"
if COG > 303.75 And COG <= 326.25 Then direction = "northwest"
if COG > 326.25 And COG <= 348.75 Then direction = "north northwest"
return direction
End Sub

2. Would you please explain a bit more? How we could get the COG value? Please Advice.
Best regards.

3. Look at the GPS documentation, especially BEARING

4. Yes, "COG" = "Course Over Ground" which is often called "Bearing", though that is not a very good word for it.

Also that code from 2007 is not very efficient, since every If statement is executed although only one can evaluate to "True". It would probably execute faster if made a bit longer with some "Else"s, so only the statements down to the true one were executed.

Or even better, do some arithmetic to get an integer that represents the required sector and then index the descriptive string from a string array. (That would be the classic way to do it, so I leave it as an exercise for the reader!)

Mike.