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

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • 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