【问题标题】:Issue trying to pull in all cities/towns for a zip code using google api尝试使用 google api 在所有城市/城镇中获取邮政编码的问题
【发布时间】:2016-02-09 18:26:51
【问题描述】:

我正在使用 google maps 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 TX,而不是 Spring TX。 我现在要做的是拉入 postcode_locality 以便我可以比较列出的任何城市/城镇(Shenandoah、Spring、The Woodlands)

我厌倦了这样做:

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

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

有没有办法拉入 postcode_locality 列表?

【问题讨论】:

    标签: java google-maps google-geocoding-api


    【解决方案1】:

    尝试使用以下方法:

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

    这可能会对你有所帮助。

    【讨论】:

    • 谢谢。问题是我使用 resultNodeList 从 google api 提取结果,所以我还没有节点。我居然找到了解决办法,贴在上面。
    【解决方案2】:

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

    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。

    【讨论】:

      猜你喜欢
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      相关资源
      最近更新 更多