1

I have two pairs of latitude/longitude, and want to find the distance between them. I am stuck with using Classic ASP on this particular website.

I found plenty of code examples using the Haversine equation, but not in ASP (which lacks the ACos function, and doesn't have pi built in! I eventually got some code working, but after careful testing it proved to be flawed. My understanding of spherical geometry isn't good enough, so please can anyone say if they have done this in ASP before?! Thanks.

Magnus Smith
  • 5,895
  • 7
  • 43
  • 64

2 Answers2

4

Oh dear. I have just been a complete idiot, and failed to spot a little bit of code that was setting my lat/long to integers before I even got into the trigonometry. I feel very stupid....

At least it means that my code is correct, so I'll post it here in case it helps someone else:

'calculate distance in miles between two coordinates
Function DistanceBetweenPoints(iLat1, iLong1, iLat2, iLong2)
    Dim iDistance
    'first assume that the earth is spherical (ha ha)
    iDistance = ACos( ( Sin(ToRad(iLat1)) * Sin(ToRad(iLat2)) ) + ( Cos(ToRad(iLat1)) * Cos(ToRad(iLat2)) * Cos(ToRad(iLong2) - ToRad(iLong1)) ) ) * 3959 'mean radius of earth in miles
    DistanceBetweenPoints = CInt(iDistance) 'round up to lose decimal place
End Function

'ASP doesnt have these two trigonometric functions, or pi, built in (needed above)
Const PI = 3.141592653589793100 'use this value from SQL
Function ToRad(iDegrees)
    ToRad = CDbl(iDegrees) * PI / 180
End Function 
Function ACos(x)
If x = 1 Then
        ACos = 0
    ElseIf x= -1 Then
        Acos = PI
    Else
        ACos = ATn(-x / Sqr(-x * x + 1)) + 2 * ATn(1)
    End If
End Function
Magnus Smith
  • 5,895
  • 7
  • 43
  • 64
2

This code source is more complete : distance can be in miles, or kilometers, or nautical miles.. http://www.geodatasource.com/developers/asp

Guillaume
  • 1,076
  • 10
  • 21