# How to Calculate Course & Distance in VB

Written by kevin walker
• Share
• Tweet
• Share
• Pin
• Email

The distance and course between two points on a globe can be calculated using two formulas. The problem is well defined and serves as an easy task for a programming language like Visual Basic. However, a choice must be made between a straight, unchanging course (Rhumb lines) or a more efficient curved course that takes advantage of the curve of the Earth.

Skill level:
Moderate

## Instructions

1. 1

Create a new project by clicking "File" and "New Project."

2. 2

Drag four labels out of the toolbox and into your project and arrange them in a line from top to bottom. Click each label and, in the properties box, change the text field so that they read "Starting Latitude," "Ending Latitude," "Starting Longitude," "Ending Longitude."

3. 3

Drag four text boxes out of the toolbox and place one just to the right of each label. Click each text box and, in the properties box, change the name field to reflect the following names: "startLatTextBox," "endLatTextBox," "startLongTextBox," "endLongTextBox."

4. 4

Drag a button from the toolbox and put it at the bottom of the project. Double-click it to enter the code editor.

5. 5

Paste the following within the "Button1_Click" method:

``````    Dim sLat = Double.Parse(startLatTextBox.Text)

Dim sLong = Double.Parse(startLongTextBox.Text)

Dim eLat = Double.Parse(endLatTextBox.Text)

Dim eLong = Double.Parse(endLongTextBox.Text)

Dim dLat = D2R(eLat - slat)

Dim dLong = D2R(eLong - sLong)

Dim dPhi = Math.Log(Math.Tan(eLat / 2 + Math.PI / 4) / Math.Tan(slat / 2 + Math.PI / 4))

Dim q = New Double

If (dPhi = 0) Then

q = Math.Cos(slat)

Else

q = dLat / dPhi

End If

If (Math.Abs(dLong) &gt; Math.PI) Then

IIf(dLong &gt; 0, dLong = (2 * Math.PI - dLong) * -1, dLong = 2 * Math.PI + dLong)

End If

Dim earth Radius = 6371 REM kilometres

Dim distance = Math.Sqrt(dLat ^ 2 + q ^ 2 * dLong ^ 2) * earth Radius

Dim bearing = Math.Atan2(dLong, dPhi)

bearing = bearing * 180 / Math.PI

MessageBox.Show("Distance = " &amp; distance &amp; ". Course = " &amp; bearing)
``````

The above code makes use of a "D2R" (degrees to radians) function that doesn't exist yet, so paste the following code directly below the "end function" statement for "Button1_Click."

``````Public Function D2R(ByVal Angle As Double) As Double

D2R = Angle * Math.PI / 180

End Function
``````
6. 6

Click the green arrow to start your program. You can now enter any pair of latitude and longitude coordinates (in decimal format) and find the distance between them (in kilometres) and bearing (in degrees) to reach the destination.

#### Tips and warnings

• These formulas are used every day for practical navigation around the Earth, but they do have small inaccuracies due to the fact that the Earth is not a perfect sphere. They are small enough to make little difference for practical navigation, but do exist.
• These formulas calculate bearing using Rhumb lines in order to maintain a steady course. Rhumb lines, though they will get you to your destination, do not follow a great circle. Great circles require constant changes in course to stay on the circle, but, due to the curve of the Earth, cut away some distance compared to Rhumb line.

### Don't Miss

#### References

• All types
• Articles
• Slideshows
• Videos
##### Sort:
• Most relevant
• Most popular
• Most recent