尝试使用谷歌api拉入所有城市/城镇的邮政编码

时间:2016-02-09 18:26:52

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

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/login_relLayout"
android:background="@drawable/mainImage">

<View
    android:id="@+id/customView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_anchor="@id/login_relLayout"
    android:background="#000000"
    android:alpha="0.8" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Testing"
    android:textSize="30sp"
    android:id="@+id/textView"
    android:textColor="#FFFFFF"
    android:layout_alignTop="@+id/customView"
    android:layout_centerHorizontal="true" />

<android.support.design.widget.TextInputLayout
android:id="@+id/emailTextInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="47dp"
    android:textColorHint="#FFFFFF"
    android:textColor="#FFFFFF"
    >

<EditText
android:layout_width="320dp"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:drawableLeft="@drawable/ic_person"
    android:textColor="#ffffff"
android:id="@+id/email"
android:hint="@string/email"
 />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:id="@+id/passwordTextInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/emailTextInput"
    android:layout_alignLeft="@+id/emailTextInput"
    android:layout_alignStart="@+id/emailTextInput"
    android:layout_alignRight="@+id/emailTextInput"
    android:layout_alignEnd="@+id/emailTextInput"
    android:textColorHint="#ffffff"
    android:textColor="#ffffff">

      <EditText
        android:layout_width="320dp"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:id="@+id/password"
        android:textColor="#ffffff"
        android:drawableLeft="@drawable/ic_lock"
        android:drawableEnd="@drawable/ic_visibility"
        android:drawableRight="@drawable/ic_visibility"
        android:layout_centerHorizontal="true"
        android:hint="@string/password"
        android:imeOptions="actionGo"
        />
  </android.support.design.widget.TextInputLayout>



  <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_below="@id/passwordTextInput"
   android:layout_marginTop="20dp"
   android:layout_alignRight="@+id/passwordTextInput"
   android:layout_alignEnd="@+id/passwordTextInput"
   android:weightSum="1"
   android:id="@+id/linLayout"
   android:layout_alignLeft="@+id/passwordTextInput"
   android:layout_alignStart="@+id/passwordTextInput">

   <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/signIn"
       android:id="@+id/signIn"
       android:layout_weight="0.5"
       android:layout_marginRight="5dp"
       android:layout_below="@+id/passwordTextInput"
       android:background="@drawable/transaprent_button"
       android:textColor="#ffffff" />

   <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Sign up"
       android:layout_weight="0.5"
       android:layout_marginLeft="5dp"
       android:background="@drawable/transaprent_button"
       android:textColor="#ffffff"
       android:id="@+id/signUp"

        />

 </LinearLayout>

</RelativeLayout>

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

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

此示例中的用户位于The Woodlands TX,而不是Spring TX。 我现在要做的是拉入postcode_locality,以便我可以比较列出的任何城市/城镇(Shenandoah,Spring,The Woodlands)

我累了这样做:

<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>

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

有没有办法提取postcode_locality列表?

2 个答案:

答案 0 :(得分:0)

尝试使用以下内容:

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

这可能会对你有帮助。

答案 1 :(得分:0)

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

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。