Brent Site Admin
Joined: 01 Jul 2005 Posts: 800
|
Posted: Mar 17th, 2009, 8:56am Post subject: [BETA DEMO] ZIP Code Radius Search |
|
|
Sometimes you want to allow users to enter in their ZIP code and a radius to find locations or people near them. This demo does that; simply displaying a comma-delimited list of nearby ZIPs. This could be useful in a SQL query. The ZIPs are grouped into "near" and "far" blocks whether they lie inside or outside the midway radius.
The distance formula used is about 90% accurate. A more accurate formula is commented out, being so much slower.
The demo relies on the Windows "findstr" command to return the base record. If RB implements SEEK, I think it would be possible to guess where the record is and find it quickly.
Code: | ' ZIP Code Radius Search
' By Brent D. Thorn, 3/2009
' Requires "zips.txt" (~2 MB) available from:
' http://www.census.gov/tiger/tms/gazetteer/zips.txt
Dim radii$(4)
radii$(1) = "5 miles"
radii$(2) = "10 miles"
radii$(3) = "25 miles"
radii$(4) = "50 miles"
Print "Enter ZIP code"
TextBox #where, ""
Print
Print "Within ";
ListBox #radius, radii$(), 1
Print
Button #submit, "Submit", [submit]
Wait
[submit]
zip$ = #where Contents$()
radius = Val(#radius Selection$())
midway = radius / 2
Cls
Select Case Platform$
Case "win32"
rec$ = Shell$("findstr ""^......" + zip$ + """ """ + DefaultDir$ + "\zips.txt""")
Case Else
Print Platform$;" not supported."
End
End Select
If Len(rec$) < 10 Then
Print "Sorry, not found"
End
End If
lon1 = Val(Word$(rec$, 5, ","))
lat1 = Val(Word$(rec$, 6, ","))
Open "zips.txt" For Input As #zips
While Not(EOF(#zips))
Line Input #zips, rec$
lon2 = Val(Word$(rec$, 5, ","))
lat2 = Val(Word$(rec$, 6, ","))
x = 69.1 * (lat2 - lat1)
y = 69.1 * (lon2 - lon1) * Cos(lat1 / 57.2958)
d = Sqr(x * x + y * y)
'- d = 3963.0 * Acs(Sin(lat1 / 57.2958) * Sin(lat2 / 57.2958) + Cos(lat1 / 57.2958) * Cos(lat2 / 57.2958) * Cos(lon2 / 57.2958 - lon1 / 57.2958))
If d Then
If d < midway Then
near$ = near$ + "," + Mid$(rec$, 7, 5)
Else
If d < radius Then _
far$ = far$ + "," + Mid$(rec$, 7, 5)
End If
End If
Wend
Close #zips
zips$ = zip$ + near$ + far$
Print "Nearby locations:"
Print zips$
End |
_________________ Brent |
|