# Calculate distance between 2 points

Page 1 of 1

## 0 Replies - 1494 Views - Last Post: 15 May 2008 - 10:32 AM

### #1 PsychoCoder Reputation: 1659
• Posts: 19,853
• Joined: 26-July 07

# Calculate distance between 2 points

Posted 15 May 2008 - 10:32 AM

Description: Pass the function the latitude/longitude for each pointThis is a snippet done in VBScript that can be used to determine the distance between 2 points. The two points being represented by the Longitude/Latitude of each point
```Public Function CalculateDistance(Lat1, Lon1, Lat2, Lon2, distType)
Dim lat_radius1, lon_radius1, lat_radius2, lon_radius2, diff, dist, X, miles

If IsNull(Lat1) Then
Exit Function
End If

If Lat1 = 0 Or Lon1 = 0 Or Lat2 = 0 Or Lon2 = 0 Then
CalculateDistance = Null
Exit Function
ElseIf Lat1 = Lat2 And Lon1 = Lon2 Then
CalculateDistance = 0
Exit Function
End If

lat_radius1 = Lat1 * 3.141592654 / 180
lon_radius1 = Lon1 * 3.141592654 / 180
lat_radius2 = Lat2 * 3.141592654 / 180
lon_radius2 = Lon2 * 3.141592654 / 180
diff = Abs(lon_radius1 - lon_radius2)
X = Sin(lat_radius1) * Sin(lat_radius2) + Cos(lat_radius1) * Cos(lat_radius2) * Cos(diff)
dist = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1)
miles = dist * 3958.754
If UCase("" & distType) = "K" Then
CalculateDistance = miles * 1.609344
Else
CalculateDistance = miles
End If
End Function
```

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; } 