This function will calculate the distance in Nautical Miles or Kilometers between two objects

Friend Function Distance(ByVal lat1 AsDouble, ByVal lon1 AsDouble, ByVal lat2 AsDouble, ByVal lon2 AsDouble, ByVal unit AsChar) AsDouble
Dim theta AsDouble = lon1 - lon2
Dim dist AsDouble = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta))
 
dist = Math.Acos(dist)
dist = rad2deg(dist)
dist = dist * 60 * 1.1515
 
If unit = "K"Then
  dist = dist * 1.609344
ElseIf unit = "N"Then
  dist = dist * 0.8684
EndIf
 
Return dist
End Function
 
 
Friend Function Deg2Rad(ByVal deg AsDouble) AsDouble
   Return (deg * Math.PI / 180.0)
End Function
 
Friend Function Rad2Deg(ByVal rad AsDouble) AsDouble
   Return rad / Math.PI * 180.0
End Function