How to easily get a zipcode from a generic address with google maps api?

I am trying to get the postal code of a generic address such as "los angeles, ca". When I do this:

gcode = new google.maps.Geocoder()
gcode.geocode({'address': 'Los Angeles, CA'}, function(results, status) { log(results); });
>> [Object { address_components=[4], formatted_address="Los Angeles, CA, USA", geometry={...}, more...}]

I get an object returned that does not have a zipcode... However, if I then take the location object returned from that, then I do get access to a zipcode:

gcode.geocode({'latLng': results[0].geometry.location}, function(results, status) { log(results[0].address_components[7].long_name) });
>> "90012"

.. But this seems wasteful as I am having to make two calls to the API to do this.. Is there a way to force Google to initially give me a zipcode?

4

1 Answer

Why not using (for instance 1600+Amphitheatre+Parkway,+Mountain+View is your address)

And then parse the JSON for

"long_name": "94043", "short_name": "94043", "types": [ "postal_code" ]
} ]

see here ->

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like