首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试使用google api获取邮政编码的所有城市/城镇的问题

尝试使用google api获取邮政编码的所有城市/城镇的问题
EN

Stack Overflow用户
提问于 2016-02-10 02:26:52
回答 2查看 446关注 0票数 1

我正在使用google地图api检查用户添加的地址,方法是传递邮政编码并返回格式化的地址,我可以比较该地址是否匹配。这就是调用:

代码语言:javascript
复制
resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result/formatted_address", geocoderResultDocument, XPathConstants.NODESET)

问题是一些邮政编码可以是多个城市/城镇的一部分。以下是邮政编码为77380的google api的结果:

代码语言:javascript
复制
<GeocodeResponse>
<status>OK</status>
<result>
<type>postal_code</type>
<formatted_address>Spring, TX 77380, USA</formatted_address>
<address_component>
<long_name>77380</long_name>
<short_name>77380</short_name>
<type>postal_code</type>
</address_component>
<address_component>
<long_name>Spring</long_name>
<short_name>Spring</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Texas</long_name>
<short_name>TX</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>United States</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>...</geometry>
<postcode_locality>Shenandoah</postcode_locality>
<postcode_locality>Spring</postcode_locality>
<postcode_locality>The Woodlands</postcode_locality>
<place_id>ChIJUXEWvwI0R4YRWJe_Gb6C8Bk</place_id>
</result>
</GeocodeResponse>

本例中的用户居住在Woodlands,而不是Spring TX。我现在尝试做的是拉入postcode_locality,这样我就可以比较列出的任何城市/城镇(雪兰多,春天,林地)

我厌倦了这样做:

代码语言:javascript
复制
resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result/postcode_locality", geocoderResultDocument, XPathConstants.NODESET);

但我只拿回了谢南多。我可以执行/GeocodeResponse/result/postcode_locality1并手动获取每个位置,但此代码需要适用于输入的任何邮政编码。

有没有办法拉入postcode_locality的列表?

EN

回答 2

Stack Overflow用户

发布于 2016-02-10 05:47:27

请尝试使用下面的命令:

代码语言:javascript
复制
    Element e = (Element) node;
    NodeList resultNodeList = e.getElementsByTagName("postcode_locality");

这可能会对你有帮助。

票数 0
EN

Stack Overflow用户

发布于 2016-02-10 05:55:08

我认为我提出的解决方案太复杂了。我修复了我的问题,通过传递城市名称与邮政编码。

代码语言:javascript
复制
private static final String GEOCODER_REQUEST = "http://maps.google.com/maps/api/geocode/xml";
.
.
.
URL url = new URL( GEOCODER_REQUEST + "?address=" +address.getZip()+address.getCity().replaceAll("\\s+","%20"));

http://maps.google.com/maps/api/geocode/xml?address=77380%20Woodlands

代码语言:javascript
复制
<GeocodeResponse>
<status>OK</status>
<result>
<type>postal_code</type>
<formatted_address>The Woodlands, TX 77380, USA</formatted_address>
<address_component>
<long_name>77380</long_name>
<short_name>77380</short_name>
<type>postal_code</type>
</address_component>
<address_component>
<long_name>The Woodlands</long_name>
<short_name>The Woodlands</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Texas</long_name>
<short_name>TX</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>United States</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>30.1261952</lat>
<lng>-95.4672962</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>30.1032280</lat>
<lng>-95.5058700</lng>
</southwest>
<northeast>
<lat>30.1782430</lat>
<lng>-95.4368399</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>30.1032280</lat>
<lng>-95.5058700</lng>
</southwest>
<northeast>
<lat>30.1782430</lat>
<lng>-95.4368399</lng>
</northeast>
</bounds>
</geometry>
<partial_match>true</partial_match>
<place_id>ChIJUXEWvwI0R4YRWJe_Gb6C8Bk</place_id>
</result>
</GeocodeResponse>

这提供了更准确的formatted_address。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35299465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档