We have a situation where user enters a city, state for which we need to find a corresponding zip code. In case there is no corresponding zip code, we are required to find a zip code that close to the city, state.

Can you please throw some ideas as to how to go about doing this using Google maps/geocode API.

EDIT: Is it possible for US that a city entered by user does not have a zip code?

  • Is this for a particular country, or worldwide? If the US (or probably other counties), many cities will have multiple zip codes--does that matter to you or is any zip code close to that city OK? – neuhausr Sep 20 '11 at 13:50
  • @neuhausr If we get multiple zip codes, we will prompt the user to choose one of them. – Mohan Gundlapalli Sep 21 '11 at 7:44
  • The Google Maps API geocoding service will only return a zip code if you give it a street address. Giving just the City and State won't return a zip code, most likely because there are usually multiple zipcodes per city (in the US at least). – Casey Sep 21 '11 at 11:54
  • Every city has at least one ZIP code – Maksim Feb 12 '16 at 14:32
  • @MohanGundlapalli did you get a scenario, where reverse geocoding with google maps was not giving postal code. For e.g. in my case its not giving postal code for Xi'an, Shaanxi, China when i find using reverse geocoding. – whyAto8 Dec 6 '18 at 12:56

I guess you could do something with http://www.geonames.org API for getting back zipcodes with cities ands states.

In case it is not found, you could use the geoname parameter postalcode_startsWith and propose results to your user or to you.

But first of all, I would study how the zipcode is built on the country you are working on. Once this done, you could maybe get back more accurate data from geonames API.

|improve this answer|||||
  • We got the zip codes from geonames for all the cities of US. This helps to get zip code(s) of any city. Thanks. – Mohan Gundlapalli Sep 21 '11 at 7:45

I created a simple API to do that magic using Google Maps Geocoding API.

Github: https://github.com/t4joel/get-address-by-google-maps-api

enter image description here

|improve this answer|||||
  • They need reverse of what you do, as I need that too. – M-- Nov 15 '18 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.