# Module: Distance in Category Geospatial Mapping : Addresses and Distances from Total Visual SourceBook

### Measure the distance between two latitude and longitude coordinates using the spherical law of cosines or Haversine formula in VBA or VB6.

Due to the curvature of the earth, the distance between two points is not a straight line. These routines calculate the distance over the earth as a sphere.

## Procedure List

 Procedure Name Type Description (Declarations) Declarations Declarations and private variables for the modDistance module. DistanceCosines Procedure Calculate the arc distance between two latitude/longitude points based on the spherical law of cosines. In spherical trigonometry, the law of cosines (also called the cosine rule for sides) is a theorem relating the sides and angles of spherical triangles, analogous to the ordinary law of cosines from plane trigonometry. For more information, visit Spherical Law of Cosines. DistanceHaversine Procedure Calculate the distance between two latitude/longitude points based on the Haversine formula. The Haversine formula is an equation important in navigation, giving great-circle distances between two points on a sphere from their longitudes and latitudes. It is a special case of a more general formula in spherical trigonometry, the Law of Haversines, relating the sides and angles of spherical triangles. For more information, visit Haversine Formula. ArcCosine Procedure Get the arc cosine of the supplied radians. DegreesToRadians Procedure Convert degrees to radians (180 degrees equals PI radians).

## Example Code for Using Module: Distance

```' Example of modDistance
'
' To use this example, create a new module and paste this code into it.
' Then run the procedure by putting the cursor in the procedure and pressing:
'    F5 to run it, or
'    F8 to step through it line-by-line (see the Debug menu for more options)

Private Sub Example_modDistance()
' Comments: Examples of using the modDistance routines to measure the distance between two points in VBA and VB6.
'           See the results in the Immediate Window.

' These are sample locations.
' To get values for a particular location, use the LatitudeLongitudeFromAddress function in the modGoogleMapsAPI module

' Location of the White House in Washington, DC
Const cdblLatitude1 As Double = 38.8976831
Const cdblLongitude1 As Double = -77.0364972

' Location of the Sydney Opera House, Australia
Const cdblLatitude2 As Double = -33.857767
Const cdblLongitude2 As Double = 151.2149929

' Location of Microsoft in Redmond, WA
Const cdblLatitude3 As Double = 47.6447015
Const cdblLongitude3 As Double = -122.1301371

' Location of the Forbidden City in Beijing, China
Const cdblLatitude4 As Double = 39.91559
Const cdblLongitude4 As Double = 116.396977

' Location of FMS offices at 8150 Leesburg Pike, Vienna, VA 22182, USA
Const cdblLatitude5 As Double = 38.9165052
Const cdblLongitude5 As Double = -77.2259664

' DistanceCosines: Calculate the arc distance between two latitude/longitude points based on the spherical law of cosines
Debug.Print "Distance using the law of cosines between the White House and Syndey Opera House"
Debug.Print DistanceCosines("M", cdblLatitude1, cdblLongitude1, cdblLatitude2, cdblLongitude2) & " miles"
Debug.Print DistanceCosines("K", cdblLatitude1, cdblLongitude1, cdblLatitude2, cdblLongitude2) & " kilometers"
Debug.Print DistanceCosines("N", cdblLatitude1, cdblLongitude1, cdblLatitude2, cdblLongitude2) & " nautical miles"
Debug.Print

' DistanceHaversine: Calculate the distance between two latitude/longitude points based on the Haversine formula
Debug.Print "Distance using the Haversine formula between the White House and Syndey Opera House"
Debug.Print DistanceHaversine("M", cdblLatitude1, cdblLongitude1, cdblLatitude2, cdblLongitude2) & " miles"
Debug.Print DistanceHaversine("K", cdblLatitude1, cdblLongitude1, cdblLatitude2, cdblLongitude2) & " kilometers"
Debug.Print DistanceHaversine("N", cdblLatitude1, cdblLongitude1, cdblLatitude2, cdblLongitude2) & " nautical miles"
Debug.Print

' DistanceCosines: Calculate the arc distance between two latitude/longitude points based on the spherical law of cosines
Debug.Print "Distance using the law of cosines between FMS and the White House"
Debug.Print DistanceCosines("M", cdblLatitude1, cdblLongitude1, cdblLatitude5, cdblLongitude5) & " miles"
Debug.Print DistanceCosines("K", cdblLatitude1, cdblLongitude1, cdblLatitude5, cdblLongitude5) & " kilometers"
Debug.Print DistanceCosines("N", cdblLatitude1, cdblLongitude1, cdblLatitude5, cdblLongitude5) & " nautical miles"
Debug.Print

' DistanceHaversine: Calculate the distance between two latitude/longitude points based on the Haversine formula
Debug.Print "Distance using the law of cosines between FMS and the White House"
Debug.Print DistanceHaversine("M", cdblLatitude1, cdblLongitude1, cdblLatitude5, cdblLongitude5) & " miles"
Debug.Print DistanceHaversine("K", cdblLatitude1, cdblLongitude1, cdblLatitude5, cdblLongitude5) & " kilometers"
Debug.Print DistanceHaversine("N", cdblLatitude1, cdblLongitude1, cdblLatitude5, cdblLongitude5) & " nautical miles"

End Sub
```

## Overview of Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

## Total Visual SourceBook

### Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version is Shipping!

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!

### Reviews

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP