Santry Technology Solutions, Content Management, DotNetNuke, SharePoint Consulting
Register | Login
Saturday, July 04, 2009

Sections
  
About Us
  
Partners
Downloads
  
 WWWCoder.com Resource Directory

Zip Code Radius Search
9/23/2004 11:10:57 AM

This code will show you how you can search within a specific radius of an area using a zip code.

This code snip provides a way of implementing a radius search within your application. One of the first things you need to do is obtain a database of zipcodes with latitude and longitude coordinates. You can do a search on Google and find databases containing the information for under $100.



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


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 9/21/2008 8:41:47 PM by Anonymous
Comments: ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
Left on 11/12/2007 7:11:05 PM by Anonymous
Comments: check this out. This solution contains a Stored Procedure that accepts a zip code and mile radius and returns all zip codes within that radius. (Example: Give me all the zip codes within a 10-mile radius of 50325). The Solution comes with and a dataload script that will create a SQL Server database, the Stored Procedure containing the algorithm, and load over 42,00 zip codes.

http://www.spikesolutions.net/ViewSolution.aspx?ID=1e7d197c-7441-40f4-8e87-5ecc3ef5ab60
No ratings available.
Left on 9/29/2007 12:46:02 AM by Anonymous
Comments:
Left on 7/1/2007 7:49:53 PM by Anonymous
Comments: neat, you should wrap this up as a component, slap a web interface over the top of it and sell it on spikesolutions.net
Left on 5/11/2006 11:40:42 AM by Anonymous
Comments: so how can I get zip code radius.  Its no where on this page!
Left on 4/1/2006 8:57:39 AM by Anonymous
Comments: To trim the box, just do a distance < radius when displaying
No ratings available.
Left on 3/29/2006 10:53:27 AM by Anonymous
Comments: I've been trying to figure out an efficient way to handle donut searches for a while without much luck.  Everything I've come up with either involves a huge lookup table, or a processor intensive search.  Today I had a thought about first getting zips in a box, and then only doing the actual calculations for those, and dropping a 30k list of zip codes for comparison down to well under 1k... HUGE processor savings... just needs an additional step to trim out the extra results... maybe not the best way to calculate, but still a good reference for limiting results, and building a much more efficient donut search...
Left on 9/2/2005 1:18:23 AM by Anonymous
Comments: Very good article. Contrary to others comments, this code uses the Haversine formula which according to the Census Bureau is the best way to calculate distances between two points on a sphere (the Earth). Read this article http://www.faqs.org/faqs/geography/infosystems-faq/
No ratings available.
Left on 8/17/2005 12:50:08 PM by Anonymous
Comments: Here's another article on doing this entirely in SQL using a stored procedure. http://www.wwwcoder.com/main/parentid/462/site/5746/68/default.aspx
No ratings available.
Left on 7/5/2005 10:05:01 PM by Anonymous
Comments: It's missing:
Function DegToRads(Deg)
pi = 3.14159265358979323846
DegToRads = cdbl(Deg * pi / 180)
End Function
Left on 6/12/2005 7:34:37 PM by Anonymous
Comments: Not a radius, rather a grid, but definitly not useless.
No ratings available.
Left on 6/12/2005 7:10:35 PM by Anonymous
Comments: Produces a list of zips in a box, not a radius. Usless. When browsing for code to do this, look for something like (Lat^2 + Lon^2) as in indicator that the code actually deals with a radius/circle. This crap can lead people astray.
Left on 2/2/2005 11:45:46 AM by Anonymous
Comments: I like the monkey.  Go move to France if you prefer comunist agendas and pushover, UN loving leadership.

Oh & nice code example...
No ratings available.
Left on 11/3/2004 7:24:27 PM by Anonymous
Comments: bad ass, saved me from buyin a component to do this

you, sir, rock. Unlike our newly elected monkey.
Left on 10/15/2004 2:41:10 AM by Anonymous
Comments: Isn't the SQL searching with a box rather than a radius?

I notice some records are going about 10 miles more than what they should, which makes me wonder if these cities go to the corner of the box..?
No ratings available.
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970