16

How to convert post code to city name, is there an API available?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Ling
  • 269
  • 1
  • 3
  • 15
  • 4
    I'm sure someone will be able to suggest something helpful, but be careful. The mapping of postal codes to cities/towns is not one-to-one. For example, 13209 covers both Syracuse and Solvay NY – Timothy May 17 '10 at 03:20
  • HI, Thanks Timothy, that's quite true, which makes the job complicated. Hi Greg, for the world wide. – Ling May 17 '10 at 03:59

5 Answers5

22

Two years later, IMO the Google Maps Geocoding API is the best way to go.

Just pass in the postal code as the address parameter:

http://maps.googleapis.com/maps/api/geocode/json?address=94043&sensor=false
lreichold
  • 755
  • 12
  • 27
  • 1
    It's helpful to use components here - e.g., if you know you are looking for a postcode in the United States you can do: `http://maps.googleapis.com/maps/api/geocode/json?components=postal_code:94043|country:US` This makes it more specific and can help eliminate unwanted results – But those new buttons though.. Feb 02 '18 at 18:58
11

You need a service that does geocoding.


Yahoo's Geocoder API is getting much better results globally.

110021 is the postal code for New Delhi among several other places in the world.

This seems to get them all.


Google Maps API is another such service but I'm not happy with the geocoded results as I don't get all results back for 110021.

geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': '110021'}, function(results, status) {
    console.log(results);
});
Anurag
  • 140,337
  • 36
  • 221
  • 257
  • Hi Anurag, Thanks so much for that information. Acctually, I think your second answer gives me the best solution, cause I have long/lat information at hand, and want them to be converted to City/Country. I hope it works fine. Thanks, Ling – Ling May 17 '10 at 04:02
  • @Ling, you can use the Yahoo API to convert lat/long to a city/country as well. See the documentation link to see what input params you can pass - http://developer.yahoo.com/maps/rest/V1/geocode.html – Anurag May 17 '10 at 09:18
  • Thanks all, I think Google API works for me. Cheers – Ling Jun 15 '10 at 00:47
  • I believe this answer is NOT correct starting with the first sentence. If what you have is Postal code, that is considered as part of the address so you will use Forward(normal) Geocoding and not Reverse. Reverse geocoding is for when you have the coordinates and want to find its address. – jbdeguzman Dec 06 '17 at 23:46
  • Thanks.. good callout! Will fix – Anurag Dec 07 '17 at 00:51
0

RedLine offers a free zipcode API service (up to 250 requests per hour). There are 4 different varieties covering distances between two zipcodes, zipcodes within a radius of a zipcode, City, state and Lat/long of a zipcode and the zipcode of a city. http://zipcodedistanceapi.redline13.com/

Bob Bickel
  • 349
  • 2
  • 3
0

Just to put in my .02, using the Google Maps API mentioned above, you can look up global information. I am currently working on a project and am pretty much implementing it as my solution for this issue. Ok, off my tiny soap box.

numberwhun
  • 956
  • 9
  • 7
0

The US Post Office sells a subscription to map 5 digit zip codes to City/State values. As @Timothy says, the mapping is one to many. However, the post office db does mark which city/state is the "primary" for a given zip code.

Don't know about other countries. Check with your country's postal authority.

See the Address APIs section of the USPS page

Larry K
  • 47,808
  • 15
  • 87
  • 140
  • This data is available on CDROM and is *not* a webservice API. – Clint Pachl Dec 10 '10 at 10:23
  • 1
    It appears that this is a free service now: https://www.usps.com/business/web-tools-apis/address-information.htm – mjhm Jun 27 '14 at 19:42
  • @Larry K , do you have more details on the 'primary' flag mentioned , what is the specific usps product and attribute to get it . thanks – Jorge Vidinha Jan 17 '23 at 16:41
  • I've updated the link in my answer. See the USPS site. The 'primary' flag is when a zip code is associated with two place names. Eg 14618 is a zip code with a primary place name of Rochester, NY. A secondary place name for that zip code is Brighton, NY. – Larry K Jan 18 '23 at 20:09