...

View Full Version : Can someone break down this zipcode locator



ntaylor1
09-22-2005, 12:33 AM
I have the following code. I believe it is supposed to find zipcodes which are within a certain radius of the given zipcode. Could someone break it down step by step so I can adapt it to my application?

Dim HighLatitude
Dim LowLatitude
Dim LowLongitude
Dim HighLongitude

Function GetDistance(Lat1, Long1, Lat2, Long2, Unit)
Dim x
x = (sin(DegToRads(Lat1)) * sin(DegToRads(Lat2)) + cos(DegToRads(Lat1)) * _
cos(DegToRads(Lat2)) * cos(abs((DegToRads(long2))-(DegToRads(long1)))))
' Get Acos(x)
x = atn((sqr(1-x^2))/x)
' Get distance in kilometers
GetDistance = 1.852 * 60.0 * ((x/pi)*180)
' Convert units if necessary
Select Case ucase(Unit)
Case "M"
GetDistance = GetDistance / 1.609344
Case "N"
GetDistance = GetDistance / 1.852
End Select
End Function
'_________________________________
Sub SetRadius(iRadius, iZip)
' THIS VARIABLE SETS THE RADIUS IN MILES

Dim rs, SQLQ, iStartlat, iStartlong, LatRange, LongRange
SQLQ = "SELECT LATITUDE, LONGITUDE FROM ZIPCODES WHERE ZIPCODE = '" & iZip & "'"
Set rs = DBConn.Execute(SQLQ)
If Not rs.EOF Then
iStartlat = rs(0)
iStartlong = rs(1)
End If
rs.Close
Set rs = Nothing
LatRange = iRadius / ((6076 / 5280) * 60)
LongRange = iRadius / (((cos(cdbl(iStartLat * 3.141592653589 / 180)) * 6076.) / 5280.) * 60)

LowLatitude = iStartlat - LatRange
HighLatitude = iStartlat + LatRange
LowLongitude = iStartlong - LongRange
HighLongitude = iStartlong + LongRange
End Sub
'_________________________________________________________
'condition for building SQL String:

If Request("radius") <> "0" Then
Call SetRadius(CLng(Request("radius")), Request("zipcode"))
SQLQ = SQLQ & JoinWord & " Company_Postal_Code IN(SELECT ZIPCODE " & _
"FROM ZIPCODES WHERE LATITUDE <= " & HighLatitude & " AND LATITUDE >= " & LowLatitude & _
" AND LONGITUDE >= " & LowLongitude & _
" AND LONGITUDE <= " & HighLongitude & ")"
JoinWord = " AND "
End If

mRW1Zard
09-23-2005, 07:41 PM
How do you want it broken down? As an interface, as SQL to put into a Stored Proc, ...? How are you planning on using this code, so that I can break this down for you in a usable way.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum