如何从 Google Places GeoDataApi 解析 Android 的地址,(缺少 address_components)

How to parse Address from Google Places GeoDataApi for Android, (address_components missing)

我们使用与示例应用类似的 Google Places API 预测。在选择结果时,我们使用以下方法获取预测位置的详细信息:

Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId);

这很好用,我的问题是结果只包含一个字符串作为地址。

当使用 JS version of the API 时,结果包含:

"address_components" : [
     {
        "long_name" : "522",
        "short_name" : "522",
        "types" : [ "street_number" ]
     },
     {
        "long_name" : "Overtoom",
        "short_name" : "Overtoom",
        "types" : [ "route" ]
     },
     {
        "long_name" : "Amsterdam-West",
        "short_name" : "Amsterdam-West",
        "types" : [ "sublocality_level_1", "sublocality", "political" ]
     },
     {
        "long_name" : "Amsterdam",
        "short_name" : "Amsterdam",
        "types" : [ "locality", "political" ]
     },
     {
        "long_name" : "Amsterdam",
        "short_name" : "Amsterdam",
        "types" : [ "administrative_area_level_2", "political" ]
     },
     {
        "long_name" : "Noord-Holland",
        "short_name" : "NH",
        "types" : [ "administrative_area_level_1", "political" ]
     },
     {
        "long_name" : "Nederland",
        "short_name" : "NL",
        "types" : [ "country", "political" ]
     },
     {
        "long_name" : "1054",
        "short_name" : "1054",
        "types" : [ "postal_code_prefix", "postal_code" ]
     }
  ],

但是在 Android 上使用 API 时,address_components 中的上述信息丢失了。 我如何使用 Google 位置 API 在 Android 上获取此信息?

不幸的是答案是"You can't."

Google 不向 API 的 Android 实现提供结构化响应数据。

您可以尝试解析地址文本,但根据 ,这绝非易事:

  1. You figure out a clever way to parse that string. Unfortunately the actual street/suburb/town/province structure is not consistent across different areas (e.g. Cape Town in South Africa has a different sentence structure to Johannesburg in South Africa). So your parsing rules need to be very clever.
  2. You use a different Google API. The JavaScript API provides structured data for the related call. Unfortunately Google recommends against using this technique.

我的一个同事用 Google 记录了这个问题:

Android 的 Places SDK 现在支持 ADDRESS_COMPONENTS 字段...:-)