## Distance between two points with curvature

The earth isn't flat. Sometimes you feel like over engineering a simple problem just because you can.

Late last year I put together a mobile web interface and used google maps for a store locator. I also had to use ASP Classic.

In the mids of this it struck me that a customer might find they were a few meters off from the actual distance from their current position, to what I'm reporting. Clearly this just won't do -so I re-worked it to compensate....in ASP CLASSIC, where most of the math is non existent.

enjoy!

Const M_PI = 3.14159265358979323846

Const earth_radius = 6371

Function Log2(X)

Log2 = Log(X) / Log(2)

End Function

function deg2rad(x)

deg2rad = x * M_PI / 180

end function

function rad2deg(x)

rad2deg = x * 180 / M_PI

end function

Function atan2(ys,xs)

Dim theta

If xs <> 0 Then

theta = Atn(ys / xs)

If xs < 0 Then

theta = theta + pi

End If

Else

If ys < 0 Then

theta = 3 * pi / 2 '90

Else

theta = pi / 2 '270

End If

End If

atan2 = theta

End Function

function distance_between(a_lat,a_lon,b_lat,b_lon)

dim dLat, dLon

dLat = deg2rad(a_lat - b_lat)

dLon = deg2rad(a_lon - b_lon)

dim a, c, r, a1

a = (sin(dLat/2) * sin(dLat/2)) + ( cos( deg2rad(a_lat) ) * cos( deg2rad(b_lat))) * ( sin(dLon/2 ) * sin(dLon/2) )

aa = 1-a

c = 2 * atan2 ( a^(1/2), aa^(1/2) )

d = earth_radius * c

distance_between = d

end function

[facebook_ilike]

## Leave a comment