从PlaceAutoCompleteAndroid(谷歌PlacesAPI)获取国家代码

从PlaceAutoCompleteAndroid(谷歌PlacesAPI)获取国家代码,android,google-places-api,Android,Google Places Api,我们正在使用谷歌PlaceAutocomplete作为城市选择器。我们需要获取选定城市的国家代码。我试图使用place.getLocale(),但它为空。有没有办法从PlaceAutocomplete返回的数据中获取国家ISO代码 在格拉德尔: 编译'com.google.android.gms:play services places:10.0.1' 代码: 迟到但回答正确 您可以使用此选项获取国家代码 将Place.Field.ADDRESS\u组件字段添加到字段列表中 List<Pl

我们正在使用谷歌PlaceAutocomplete作为城市选择器。我们需要获取选定城市的国家代码。我试图使用
place.getLocale()
,但它为空。有没有办法从PlaceAutocomplete返回的数据中获取国家ISO代码

在格拉德尔:
编译'com.google.android.gms:play services places:10.0.1'

代码:

迟到但回答正确

您可以使用此选项获取国家代码

Place.Field.ADDRESS\u组件
字段添加到字段列表中

List<Place.Field> fields = Arrays.asList(Place.Field.ADDRESS_COMPONENTS);
Intent intent = new Autocomplete.IntentBuilder(
            AutocompleteActivityMode.FULLSCREEN, fields)
            .setTypeFilter(TypeFilter.CITIES)
            .build(mActivity);
startActivityForResult(intent, requestCode);

使用地理编码器有一个解决方案
List<Place.Field> fields = Arrays.asList(Place.Field.ADDRESS_COMPONENTS);
Intent intent = new Autocomplete.IntentBuilder(
            AutocompleteActivityMode.FULLSCREEN, fields)
            .setTypeFilter(TypeFilter.CITIES)
            .build(mActivity);
startActivityForResult(intent, requestCode);
if (place.getAddressComponents() != null) {
    List<AddressComponent> addressComponents = place.getAddressComponents().asList();
    for (int i = 0; i < addressComponents.size(); i++) {
         if (addressComponents.get(i).getTypes().get(0).equals("country")) {
              countryCode = addressComponents.get(i).getShortName();
              break;
         }
    }
}