83

I basically want to retrieve a list of city and state within a zipcode. Is Google's Geocode API capable of doing so? I've tried looking into the documentation but found the information overwhelming.

Any help would be appreciated. If there is another method of accomplishing such task, please let me know.

Thanks

EDIT: I was able to retrieve the City and State through: http://maps.google.com/maps/geo?output=xml&q=14606 but is there a limitation against that?

skaffman
  • 381,978
  • 94
  • 789
  • 754
meenxo
  • 1,146
  • 1
  • 11
  • 11
  • You can use the [**HERE Maps** Geocoding REST API](https://developer.here.com/documentation/geocoder/topics/request-first-geocode.html). [Create a project](https://developer.here.com/projects) and get the REST credentials. Wait for 1 hour for them to go live (showed me an *InvalidCredentials* error at first) and then call e.g. `geocoder.cit.api.here.com/6.2/geocode.json?PostalCode=2400&country=Denmark&app_id={APP_ID}&app_code={APP_CODE}&gen=9` (you can provide [the address parts](https://developer.here.com/documentation/geocoder/topics/resource-type-response-geocode.html#ariaid-title7)) – aexl Sep 05 '17 at 20:51

4 Answers4

112

Use the GeoCoding API

For example, to lookup zip 77379 use a request like this:

https://maps.googleapis.com/maps/api/geocode/json?address=77379&sensor=true&key=YOUR_GOOGLE_PLATFORM_API_KEY

gtamborero
  • 1,881
  • 18
  • 21
Jason
  • 73,476
  • 14
  • 119
  • 139
  • 10
    You mean [sensor=false](http://code.google.com/apis/maps/documentation/staticmaps/#Sensor)? – Rup Jan 20 '11 at 16:49
  • Jason if I enter a postal code from another country Google returns all matching results around the world. Is there a parameter where I can set an ISO country code? – Anagio Jul 31 '12 at 19:44
  • look at the section on "Region Biasing" in the linked API doc – Jason Jul 31 '12 at 19:57
  • 19
    "Note: the Geocoding API may only be used in conjunction with a Google map; geocoding results without displaying them on a map is prohibited." – Andy Ray Sep 24 '12 at 23:45
  • An alternative is https://github.com/Loceo/loceo-jquery-plugin – Gustav Nov 08 '12 at 17:12
  • @Anagio `components=country:us` for example – onmyway133 Jan 11 '15 at 05:02
  • Unfortunately, Loceo is dead as of 2014-09-03 – vphilipnyc Mar 26 '15 at 21:25
  • here is a simple API also, it returns city ,state and a GeoJson boundary representation of the zipcode(or multiple) queried.: http://api.reaperfire.com/reaperfire/rest/v1/public/boundary?zipcode=20002,20001&format=geojson – Jeryl Cook May 14 '15 at 23:32
  • 2
    @Rup sensor is not required anymore – Dejell May 18 '15 at 18:13
  • @JerylCook how can I send country to it? – Dejell May 18 '15 at 18:14
  • @Dejel It was when I wrote that four years ago. And if you are going to pass it for a zip code lookup, like in this answer, you should say false. – Rup May 18 '15 at 19:14
  • @Dejel , https://www.mashape.com/vanitysoft/boundaries-io is the official entry point for the API, not its US state,city, and zipcodes right now. – Jeryl Cook May 19 '15 at 20:24
  • @JerylCook thanks but it's for US address only. I am looking for something global – Dejell May 20 '15 at 15:29
  • 3
    This is the first hit on google when googling "city zip code api", and and this is obviously not that :( – DuckPuncher Mar 30 '16 at 15:45
  • 1
    This doesnt work always. Say if your postal code is 00001, http://maps.googleapis.com/maps/api/geocode/json?address=00001&sensor=true, will return an address with postal code 88030 – zookastos Sep 20 '16 at 21:32
  • You can restrict this to lookup only postal codes by setting the `result_type` param to `postal_code`. Here's a full example: http://maps.googleapis.com/maps/api/geocode/json?address=01810&result_type=postal_code&sensor=true – Kevin Leary Sep 28 '16 at 16:07
  • 7
    @KevinLeary result_type does not restrict to the postal code. Google's geocoder will mistake zip codes for street address numbers (try zip 89100). Instead the format to use is http://maps.googleapis.com/maps/api/geocode/json?components=country:US|postal_code:00001 as documented here: https://developers.google.com/maps/documentation/geocoding/intro#ComponentFiltering – Mike Kormendy Nov 15 '16 at 01:24
  • How were you able to do this without a API key? The documentation states you have to use a API key, so I was under the impression I had to have one. – dman Mar 06 '17 at 04:12
  • doesnt return for 97703 zip code. what can be an alternative – Sana Mar 28 '17 at 07:43
  • @AndyRay That is only half correct. As per Google, you can display data that is not on a map but you must show the 'Powered By Google' logo near the data in the app. source: https://developers.google.com/maps/documentation/geocoding/policies – nasaQuant Jan 03 '19 at 06:59
  • If you need to lookup a zip code for a unique country you can do it adding to the URL parameter this: &components=country:ES (for SPAIN in my case) ... for example: https://maps.googleapis.com/maps/api/geocode/json?components=country:ES&address=77379 – gtamborero Nov 10 '20 at 22:47
54

I found a couple of ways to do this with web based APIs. I think the US Postal Service would be the most accurate, since Zip codes are their thing, but Ziptastic looks much easier.

Using the US Postal Service HTTP/XML API

According to this page on the US Postal Service website which documents their XML based web API, specifically Section 4.0 (page 22) of this PDF document, they have a URL where you can send an XML request containing a 5 digit Zip Code and they will respond with an XML document containing the corresponding City and State.

According to their documentation, here's what you would send:

http://SERVERNAME/ShippingAPITest.dll?API=CityStateLookup&XML=<CityStateLookupRequest%20USERID="xxxxxxx"><ZipCode ID= "0"><Zip5>90210</Zip5></ZipCode></CityStateLookupRequest>

And here's what you would receive back:

<?xml version="1.0"?> 
<CityStateLookupResponse> 
    <ZipCode ID="0"> 
        <Zip5>90210</Zip5> 
        <City>BEVERLY HILLS</City> 
        <State>CA</State> 
    </ZipCode> 
</CityStateLookupResponse>

USPS does require that you register with them before you can use the API, but, as far as I could tell, there is no charge for access. By the way, their API has some other features: you can do Address Standardization and Zip Code Lookup, as well as the whole suite of tracking, shipping, labels, etc.

Using the Ziptastic HTTP/JSON API (no longer supported)

Update: As of August 13, 2017, Ziptastic is now a paid API and can be found here

This is a pretty new service, but according to their documentation, it looks like all you need to do is send a GET request to http://ziptasticapi.com, like so:

GET http://ziptasticapi.com/48867

And they will return a JSON object along the lines of:

{"country": "US", "state": "MI", "city": "OWOSSO"}

Indeed, it works. You can test this from a command line by doing something like:

curl http://ziptasticapi.com/48867 
jjspace
  • 176
  • 1
  • 11
Richard Jones
  • 4,400
  • 3
  • 23
  • 33
  • 8
    Major love for the Ziptastic recommendation. <3! – ceejayoz Jul 18 '12 at 02:12
  • 5
    US Postal Service will probably deny your request for access unless you are using their API for activities specifically related to mailing. At least, that's been my experience. – eaj Oct 16 '12 at 17:17
  • "Ziptastic" is convenient to use – Ishan Jain Jul 18 '13 at 12:28
  • Unfortunate that it doesn't provide proper capitalization though. – Zach Sep 08 '14 at 08:45
  • how can I provide country to ziptastic? – Dejell May 18 '15 at 16:52
  • This answer does NOT work with international postal codes. US based only. For a global product use google. – Case Oct 21 '15 at 22:34
  • 1
    @eaj That's true: "The Address Validation APIs can be used in conjunction with USPS SHIPPING OR MAILING SERVICES ONLY." – dragan.stepanovic Dec 22 '15 at 12:50
  • 2
    [Ziptastic is no longer supported.](https://github.com/joshstrange/Ziptastic) It should not be used anymore. – vaindil Jul 20 '16 at 21:55
  • Thanks for the heads up @Vaindil. Updated the answer to reflect this. – Richard Jones Jul 21 '16 at 00:26
  • 1
    it doesn't surprise me that the US post office uses an XML API.... lol.... – rgenito Sep 28 '16 at 06:43
  • Updated link for documentation of USPS- https://www.usps.com/business/web-tools-apis/address-information-api.htm#_Toc410982978 – nik Mar 08 '17 at 14:02
  • 1
    Doesn't look very "non-supported" to me https://www.getziptastic.com/ . That said, unless they have improved their API (which they may have), it will only return one city/state per zip code. This is quite incorrect if accuracy is important. Sometime zip codes even cross state lines, and, certainly often span more than one city. – RationalRabbit Aug 03 '17 at 06:03
  • 2
    @RationalRabbit: I submitted an update noting that Ziptastic is now a paid API. – Richard Jones Aug 13 '17 at 17:57
  • Not working with UK zip code "HA88NP" – Kamlesh Oct 01 '19 at 09:57
15
function getCityState($zip, $blnUSA = true) {
    $url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $zip . "&sensor=true";

    $address_info = file_get_contents($url);
    $json = json_decode($address_info);
    $city = "";
    $state = "";
    $country = "";
    if (count($json->results) > 0) {
        //break up the components
        $arrComponents = $json->results[0]->address_components;

        foreach($arrComponents as $index=>$component) {
            $type = $component->types[0];

            if ($city == "" && ($type == "sublocality_level_1" || $type == "locality") ) {
                $city = trim($component->short_name);
            }
            if ($state == "" && $type=="administrative_area_level_1") {
                $state = trim($component->short_name);
            }
            if ($country == "" && $type=="country") {
                $country = trim($component->short_name);

                if ($blnUSA && $country!="US") {
                    $city = "";
                    $state = "";
                    break;
                }
            }
            if ($city != "" && $state != "" && $country != "") {
                //we're done
                break;
            }
        }
    }
    $arrReturn = array("city"=>$city, "state"=>$state, "country"=>$country);

    die(json_encode($arrReturn));
}
Nicolas Giszpenc
  • 503
  • 4
  • 10
6

couple of months back, I had the same requirement for one of my projects. I searched a bit for it and found out the following solution. This is not the only solution but I found it to one of the simpler one.

Use the webservice at http://www.webservicex.net/uszip.asmx.
Specifically GetInfoByZIP() method.

You will be able to query by any zipcode (ex: 40220) and you will have a response back as the following...

<?xml version="1.0" encoding="UTF-8"?>
 <NewDataSet>
  <Table>
   <CITY>Louisville</CITY> 
   <STATE>KY</STATE> 
   <ZIP>40220</ZIP> 
   <AREA_CODE>502</AREA_CODE> 
   <TIME_ZONE>E</TIME_ZONE> 
  </Table> 
</NewDataSet>

Hope this helps...

Florent
  • 11,917
  • 10
  • 44
  • 56
Som Poddar
  • 1,348
  • 14
  • 22