【问题标题】:Get country code from PlaceAutocompleteFragment android (Google Places API)从 PlaceAutocompleteFragment android (Google Places API) 获取国家代码
【发布时间】:2016-05-05 13:27:33
【问题描述】:

在适用于 Android 的 Google Places API 中,我使用 PlaceAutocompleteFragment 来显示城市/国家。
这里获取地址、名称、placeId 等。
Place 对象仅包含这些字段。

@Override
public void onPlaceSelected(Place place) {

    Log.i(TAG, "Place Selected: " + place.getName());

    // Format the returned place's details and display them in the TextView.
    mPlaceDetailsText.setText(formatPlaceDetails(getResources(), place.getName(), place.getId(),
            place.getAddress(), place.getPhoneNumber()+" "+place.getAttributions()+" :: "+place.getLocale(), place.getWebsiteUri()));

}

但我也想要国家代码。有没有办法从 places API 获取国家代码? 如果没有,是否有任何替代服务可以在输入时获取国家/地区名称和代码?

【问题讨论】:

  • 什么电话的国家代码或其他什么请解释一下??
  • @Nowshad :它不是基于电话的。当我输入 PlaceAutocompleteFragment 时,我得到了城市/国家的列表。从选择的结果中没有得到国家代码。

标签: android google-places-api


【解决方案1】:

检索到 Place 对象后,您可以获得关联的 Locale 对象:

Locale locale = place.getLocale();

使用此 Locale 对象,您可以通过以下方式获取国家/地区代码:

locale.getCountry();

国家名称带有:

locale.getDisplayCountry();

您可以在文档中查看更多可用方法: http://developer.android.com/reference/java/util/Locale.html

编辑:

如果 Place 对象中的 Locale 为 null,您可以使用地理编码器从 GPS 坐标中获取信息:

LatLng coordinates = place.getLatLng(); // Get the coordinates from your place
Geocoder geocoder = new Geocoder(this, Locale.getDefault());

List<Address> addresses = geocoder.getFromLocation(
                coordinates.latitude,
                coordinates.longitude,
                1); // Only retrieve 1 address
Address address = addresses.get(0);

然后你可以调用这些方法来获取你想要的信息

address.getCountryCode();
address.getCountryName();

Address 对象的更多方法:http://developer.android.com/reference/android/location/Address.html

请注意,应在后台线程上调用 Geocoder 方法以不阻塞 UI,并且它也可能无法检索到任何答案。

【讨论】:

  • place.getLocale() 给出 null。
  • 获取区域设置为空
  • 区域设置中的这个国家/地区代码可能基于您的手机设置,并且可能与您选择的位置无关。我认为这不是一个好的解决方案。
  • 对于 Locale 对象,返回的国家/地区代码应为空字符串、大写 ISO 3166 2 字母代码或独立于语言的 UN M.49 3 位代码。 (见developer.android.com/reference/java/util/Locale#getCountry())。至于其他参数,似乎是 Places API 的限制。有多个问题要求 API 处理这种情况(请参阅issuetracker.google.com/issues/63829150)。对于 Geocoder 解决方案,您可以根据需要传递您选择的区域设置。
【解决方案2】:

这将给出正确的国家代码。

TelephonyManager tm = (TelephonyManager)activity.getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso(); 

【讨论】:

    【解决方案3】:

    这里所有答案都指向已弃用的 SDK 或使用 TelephonyManager 或 GeoCoder(OP 明确提到从地方 SDK 响应中获取国家/地区代码。我也 选择国家/地区应该很明显来自操作系统(TelephonyManager 或 GeoCoder)的国家代码或国家代码不一定与用户从选择器中选择的位置返回的位置相匹配)。 在较新的地方 SDK(com.google.android.libraries.places:places:2.4.0),我设法通过遵循代码 sn-p 获取国家代码 确保包括以下字段(除了您需要的更多内容)

    val fields = listOf(
        Place.Field.ADDRESS_COMPONENTS,
        // ... More fields that you need
    )
    

    要解析国家代码,请遵循此代码

    place.addressComponents.asList().find { it.types.contains("country") }.shortName
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 1970-01-01
      • 2017-04-09
      相关资源
      最近更新 更多