<p>My little site should be pooling list of items from a table using the active user's location as a filter. Think <a href="http://craigslist.org" rel="nofollow noreferrer">Craigslist</a>, where you search for "dvd' but the results are not from all the DB, they are filtered by a location you select. My question has 2 levels:</p>

<ol>
<li>should I go a-la-craigslist, and ask users to use a city level location? My problem with this is that you need to generate what seems to me a hard coded, hand made list of locations. </li>
<li>should I go a-la-zipCode. The idea of just asking the user to type his zipcode, and then pool all items that are in the same or in a certain distance from his zip code.</li>
</ol>

<p>I seem to prefer the zip code way as it seems more elegant solution, but how on earth do one goes about creating a DB of all zip codes and implement the function that given zip code 12345, gets all zipcodes in 1 mile distance?</p>

<p>this should be fairly common "task" as many sites have a need similar to mine, so I am hoping not to re-invent the wheel here.</p>

[{'answer_id': 12875, 'author': 'Nathan', 'author_id': 541, 'author_profile': 'https://Stackoverflow.com/users/541', 'pm_score': 4, 'selected': True, 'text': '<p>Getting a Zip Code database is no problem. You can try this free one:\n<a href="http://zips.sourceforge.net/" rel="nofollow noreferrer">http://zips.sourceforge.net/</a></p>\n\n<p>Although I don\'t know how current it is, or you can use one of many providers. We have an annual subscription to <a href="http://www.ZipCodeDownload.com" rel="nofollow noreferrer">ZipCodeDownload.com</a>, and for maybe $100 we get monthly updates with the latest Zip Code data complete with Lat/Longs of the centroid of the zip code. </p>\n\n<p>As for querying for all zips within a certain radius, you are going to need a spatial library of some sort. If you just have a table of zips with lats/longs, you will need a database-oriented mechanism. SQL Server 2008 has the capability built in, and there are <a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=MsSqlSpatial" rel="nofollow noreferrer">open source libraries</a> and <a href="http://www.spatialdb.com/default.aspx?AspxAutoDetectCookieSupport=1" rel="nofollow noreferrer">commercial libraries</a> that will add such capabilities to SQL Server 2005. The open source database PostgreSQL has a project, PostGIS that adds this capability to that database. It is here: <a href="http://postgis.refractions.net/" rel="nofollow noreferrer">http://postgis.refractions.net/</a></p>\n\n<p>Other database platforms probably have similar projects, but those are the ones I am aware of. With one of these DB based libraries you should be able to directly query for any zip codes (or any rows of any kind that have lat/long columns) within a given radius.</p>\n\n<p>If you want to go a different route you can use spatial tools with a mapping library. There are open source options here as well, such as <a href="http://www.codeplex.com/SharpMap" rel="nofollow noreferrer">SharpMap</a> and <a href="http://opensourcegis.org/" rel="nofollow noreferrer">many others</a> (<a href="http://www.google.com/search?q=Open+Source+GIS+tools" rel="nofollow noreferrer">Google can help out</a>) that can use the free Tiger maps for the united states as the data source. However, this route is somewhat more complicated and possibly less performant if all you need is a radius search.</p>\n\n<p>Finally, you may want to look into a web service. This, as you say, is a common need, and I imagine there are any number ob web services that you can subscribe to that can provide all zip codes in a given radius from a provided zip code. A quick Google search turned up this:\n<a href="http://www.zip-codes.com/free-zip-code-tools.asp#radius" rel="nofollow noreferrer">http://www.zip-codes.com/free-zip-code-tools.asp#radius</a>\nBut there are <a href="http://www.google.com/search?q=all+zip+codes+in+radius" rel="nofollow noreferrer">MANY resources to be had for the searching</a> on this subject.</p>\n'}, {'answer_id': 12919, 'author': 'Jon Sagara', 'author_id': 731, 'author_profile': 'https://Stackoverflow.com/users/731', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>how on earth do one [...] implement the function that given zip code 12345, gets all zipcodes in 1 mile distance?</p>\n</blockquote>\n\n<p>Here is a sample on how to do that:</p>\n\n<p><a href="http://www.codeproject.com/KB/cs/zipcodeutil.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/cs/zipcodeutil.aspx</a></p>\n'}, {'answer_id': 69669, 'author': 'decibel', 'author_id': 11116, 'author_profile': 'https://Stackoverflow.com/users/11116', 'pm_score': 0, 'selected': False, 'text': "<p>Just to be technical... PostGIS isn't a project of the Postgres community... it's a stand-alone project that is built on top of Postgres. If you want help or support with PostGIS, you'll want to go to it's community instead of Postgres.</p>\n"}, {'answer_id': 113699, 'author': 'Sargun Dhillon', 'author_id': 10432, 'author_profile': 'https://Stackoverflow.com/users/10432', 'pm_score': 0, 'selected': False, 'text': "<p>You can use PostGIS. Additionally, I've used deCarta's mapping libraries. They have technology which allows you to geokey any arbitrary data type. Then you can query these spatially.</p>\n\n<p>disclaimer: I work for deCarta</p>\n"}, {'answer_id': 300420, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Wouldn't it be more efficient to just figure out which cities are within a 1 mile radius and store that information in a table? Then you don't have to do calculations in the database all the time.</p>\n"}]
['https://Stackoverflow.com/questions/12823', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/350/']