Convert zip code to latitude/longitude coordinates using Google Maps API?

RobVious picture RobVious · Sep 24, 2013 · Viewed 9.7k times · Source

I've been looking through the docs and all over the web for an example of this and I can't find an example of this use-case.

Given a zip code, I need a rough approximation of the user's location in the form of lat/long coordinates. All I have is the zip code, nothing else.

Can this be done using Google Maps API? If not, what other resources would you suggest looking at?

Answer

ffflabs picture ffflabs · Sep 24, 2013

You can pass any text as the address key for the geocoder. If the geocoding is successsful, you should get an array of results.

From then on you can pick the first result or iterate over the array. Each of its elements should contain an address object, and postal code is one posible field on that obj.

Keep in mind that usps zipcodes are not points nor shapes. They are arrays of points, and the centroid of the polygon drawn by using those points as vertexes probably doesn't have any special meaning.