Question

Would there be a way to do a zip code lookup based on City/State input in a form? I'm thinking the Google geocode API might be the right direction. Any thoughts? I have a site built on Wordpress so the code would have to utilize PHP. Thanks in advance.

Was it helpful?

Solution

YQL can do things like this:

select  name from geo.places.children where parent_woeid in (select woeid from geo.places where text="sunnyvale, usa" limit 1) AND placetype = 11

returns:

{
 "query": {
  "count": 6,
  "created": "2011-03-16T06:49:09Z",
  "lang": "en-US",
  "results": {
   "place": [
    {
     "name": "94086"
    },
    {
     "name": "94087"
    },
    {
     "name": "94088"
    },
    {
     "name": "94089"
    },
    {
     "name": "94090"
    },
    {
     "name": "94085"
    }
   ]
  }
 }
}

YQL Console

There are examples on there on how to implement queries like this in both PHP and Javascript on their site.

OTHER TIPS

Geocoding is where you find the coordinates of an address. Yes you could geocode a city,state but this would give you he center of the city (as defined by the geocoder's internal database - typically a centroid or 'city hall'.

Most cities have multiple zip codes: Do you want all of these? Similarly a zip code could contain multiple cities - especially in rural areas where zip codes can be large and cities are what other countries would call 'villages' and 'hamlets'

So you best bet is probably to get a database. There might be some free ones around (Geonames comes to mind but I don't think it has zip codes), but you might end up having to buy one.

First a note on the Google API: be aware of Google's TOS so you don't take a wasted path as others have done (sometimes unknowingly). Specifically: "Note: the Geocoding API may only be used in conjunction with a Google map; geocoding results without displaying them on a map is prohibited.".

Your best bet is to get a free zip code database if your project is not mission-critical; otherwise, you'll probably need a good commercial-grade database. Just google "commercial grade zip code database".

Also, see a good stack-overflow thread about this topic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top