Bay Six Software Forum Index Bay Six Software
Beyond the Basics
 
 FAQFAQ   SearchSearch   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[BETA DEMO] ZIP Code Radius Search

 
Post new topic   Reply to topic    Bay Six Software Forum Index -> Internet Programming
View previous topic :: View next topic  
Author Message
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 728

PostPosted: Mar 17th, 2009, 8:56am    Post subject: [BETA DEMO] ZIP Code Radius Search Reply with quote

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
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Bay Six Software Forum Index -> Internet Programming All times are GMT
Page 1 of 1
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum



Lo-Fi Version
Powered by phpBB © 2001, 2005 phpBB Group