我正在使用google地图api检查用户添加的地址,方法是传递邮政编码并返回格式化的地址,我可以比较该地址是否匹配。这就是调用:
resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result/formatted_address", geocoderResultDocument, XPathConstants.NODESET)问题是一些邮政编码可以是多个城市/城镇的一部分。以下是邮政编码为77380的google api的结果:
<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,这样我就可以比较列出的任何城市/城镇(雪兰多,春天,林地)
我厌倦了这样做:
resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result/postcode_locality", geocoderResultDocument, XPathConstants.NODESET);但我只拿回了谢南多。我可以执行/GeocodeResponse/result/postcode_locality1并手动获取每个位置,但此代码需要适用于输入的任何邮政编码。
有没有办法拉入postcode_locality的列表?
发布于 2016-02-10 05:47:27
请尝试使用下面的命令:
Element e = (Element) node;
NodeList resultNodeList = e.getElementsByTagName("postcode_locality");这可能会对你有帮助。
发布于 2016-02-10 05:55:08
我认为我提出的解决方案太复杂了。我修复了我的问题,通过传递城市名称与邮政编码。
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
<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。
https://stackoverflow.com/questions/35299465
复制相似问题