KeyLimeTie Blog

Zip Code Search Within A Radius (C#)

By Brian Pautsch – 3/1/2006 10:15:21 PM. Posted to Applications.

There are a lot of industries that need to show their data results based on proximity to a specified zip code. Some include car dealers, real estate agencies, job listers, etc. A few of my customers have asked for this capability and instead of buying a boxed solution and subscribing to a zip code list, I decided to build it myself, maintain it myself and bill each customer for usage. Actually, it's not that difficult. Once you figure out some of the radius and distance calculations and where to get current zip code data from, it's as simple as creating a few SQL Server User Defined Functions and a service to periodcially update your database.

Download code

Sample House Search Application


Database
At a minimum, you'll need a database with zip codes and their latitude and longitude coordinates. My example includes a database with all of the zip codes for Illinois along with the City, State, Latitude and Longitude.

Functions
RadiusFunc : Accepts @ZipCode (zip code) and @Miles (distance from zip code in miles) and returns the Maximum Latitudes and Longitudes for the radius of the zip code.
DistanceFunc : Accepts Latitudes and Longitudes for two zip codes and returns their distance Miles.

Stored Procedures
spHouses_GetNearZipcode: Accepts @ZipCode (zip code) and @Miles (distance from zip code in miles) and returns the houses listed in the radius.

Default.aspx
Default.aspx: Simple example where the user enters in a zip code, selects a distance from zip code radius and clicks enter to get results. Results are then bound to a repeater.

Default.aspx.cs
Default.aspx.cs : Nothing special about this code...the real logic for zip code searching is in the database!

Comments

On 4/13/2010 Chetna said:
Excellent article! I have plugged in my application and worked like a charm. Thank you!

On 1/8/2010 shalini said:
thanks a lot...

On 1/8/2010 Chetan Kashid said:
For US Data for States

On 1/8/2010 Ron said:
keep in mind...to get a more comprehensive db,

On 1/8/2010 Teddy said:
Hi,

On 1/8/2010 Prathibha said:
Good one. I was looking at a number of posts on devx and coders.net - but this was the one that worked.

On 2/14/2009 Karen said:
You ROCK!!! This is awesome!

On 1/29/2009 Matt said:
Thanks, I researched a method to do this for quite a while and your solution was perfect and very simple to implement! My GEO ZIP tables plugged into this seamlessly!

On 10/30/2008 Mark said:
Very solid coding retaining the very basics for logic. Thanks! This is an excellent post of all that I've seen addressing radius calcs.

On 5/24/2006 Brian Pautsch said:
Prathibha - Thanks for recommending that site for a zipcode database.

On 4/5/2006 Brian Pautsch said:
Thanks

On 4/3/2006 Bret said:
Excellent article, Steve, and solid code. Thanks for saving me lots of time.

On 3/7/2006 Steve said:
Thanks! I've been looking for this for a long time! Where can I get the data for all US states from?

Leave a Comment

Name:
Email:
URL:
Comment:
Security Code:
Type Security Code:

Photos on Flickr

More Photos »

Search Blog


Get Email Updates

Like what you read here at KeyLimeTie? Sign up for our email list!

Subscribe