gpt4 book ai didi

java - 尝试使用 google api 获取所有城市/城镇的邮政编码时出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 12:57:05 25 4
gpt4 key购买 nike

我正在使用谷歌地图 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>

此示例中的用户居住在德克萨斯州伍德兰兹,而不是德克萨斯州斯普林。我现在想做的是拉入邮政编码位置,以便我可以比较列出的任何城市/城镇(Shenandoah、Spring、The Woodlands)

我厌倦了这样做:

resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result/postcode_locality", geocoderResultDocument, XPathConstants.NODESET);

但我只回到了 Shenandoah。我可以执行/GeocodeResponse/result/postcode_locality[1] 并手动获取每个位置,但此代码需要适用于输入的任何邮政编码。

有没有办法拉入邮政编码地点列表?

最佳答案

尝试使用以下内容:

    Element e = (Element) node;
NodeList resultNodeList = e.getElementsByTagName("postcode_locality");

这可能对你有帮助。

关于java - 尝试使用 google api 获取所有城市/城镇的邮政编码时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35299465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com