funkwater has asked for the wisdom of the Perl Monks concerning the following question:

I've seen many sites that let you enter your zip code in a search field and return results based on zip codes that surround yours (i.e. find a u-haul or Starbucks store locator). I want to do something like this on a site of mine (without the groovy maps) but couldn't figure out how this is done.

I originally thought to find zip codes within a few numbers of the one entered, but zip codes don't seem to be in any specific numeric order (i.e. 19087 is adjacent to 19406). Has anyone ever done this before?

Replies are listed 'Best First'.
Re: Searching by zip code
by johannz (Hermit) on May 13, 2000 at 02:09 UTC
      I should note that the zip code database on that site seems a bit outdated. (It doesn't have 20194, for example). Still, since it's free, I'm not complaining :)
RE: Searching by zip code
by neshura (Chaplain) on May 12, 2000 at 23:56 UTC
    Most zip code databases of this sort are commercial and expensive. The USPS licenses this information to various vendors -- one more way they recoup costs (zip faq here). I looked for something like this a couple of years ago but ended up buying a license from a commercial vendor. If there's a free version out there, I'd like to know as well.

    e-mail neshura

      i need the zipcode for wall township new jersey
RE: Searching by zip code
by lhoward (Vicar) on May 13, 2000 at 00:25 UTC
    You can buy a commercial zip-code database for $300 or less. One such vendor is Zipinfo.com. Check out their ZIPList5 Geocode database. That looks like just what you need. Once you have it it will generally include latitude and longitude of the center of each zip. Once you have a ZIP to latitude/longitude database you would either:

    • find all zip-codes whose centers are within X miles of the user-entered zip, then find all stores in those zips.
    • find all stores within X miles of the user entered zip. This will require pre-determining the latitude/longitude of all your locations (you can pre-compute the aprox latitude/longitude of each store from the zip-code DB).

      One approach may be faster than another based on how many stores you have. Unless you have as many locations as McDonalds the second option is probably faster.

      Either way you're going to have to compute the distance between 2 points of latitude/longitude which is not an easy formula to derive because of the curvature of the earth. However it has already been derived by many other people. Follow this link for details of how to perform that computation.

      The basic algorithm above can be very slow because it involves computing many distances. However, it can be optimized significantly for actual implementation. It does however start you off on the right track.

Re: Searching by zip code
by IndyZ (Friar) on May 12, 2000 at 23:57 UTC
    This is tough:
    Go through every single zip code (you heard me) and map each zip code to a store or stores in a database. I can't think of an easier way to do it.

    =Brian

      That might be worth-while for only a few stores, but any more than a few and you could just imagine the mess. Neshura is correct about what the Post Office does.

      Roy Alan