如何轻松从谷歌地图api的通用地址获取邮政编码? [英] How to easily get a zipcode from a generic address with google maps api?

查看:805
本文介绍了如何轻松从谷歌地图api的通用地址获取邮政编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获得通用地址的邮政编码,例如los angeles,ca。当我这样做时:

  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 ...}]

我得到一个没有邮编的返回对象...但是,如果我接着从那里返回位置对象,那么我可以访问一个邮政编码:

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

..但这似乎很浪费,因为我不得不对API进行两次调用为什么不使用(例如1600 + Amphitheater + Parkway,+ Mountain + View是您的地址)

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

然后解析JSON为

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

请参阅此处 - > http://code.google.com/apis/maps/documentation / geocoding /


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?

解决方案

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/

这篇关于如何轻松从谷歌地图api的通用地址获取邮政编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆