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

浪子不回头ぞ 提交于 2019-12-13 11:40:24

问题


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?


回答1:


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

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false

And then parse the JSON for

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

see here -> http://code.google.com/apis/maps/documentation/geocoding/



来源:https://stackoverflow.com/questions/6888185/how-to-easily-get-a-zipcode-from-a-generic-address-with-google-maps-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!