从 PlaceAutocompleteFragment android (Google Places API) 获取国家代码
Get country code from PlaceAutocompleteFragment android (Google Places API)
在 Google Places API for Android 中,我使用 PlaceAutocompleteFragment 来显示 city/country。
我在这里获取地址、名称、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()));
}
但我也想要国家代码。有没有办法从 API 的地方获取国家代码?
如果没有,是否有任何替代服务可以在输入时获取国家/地区名称和代码?
通过检索 Place 对象,您可以获得关联的 Locale 对象:
Locale locale = place.getLocale();
使用此 Locale 对象,您可以通过以下方式获取国家/地区代码:
locale.getCountry();
以及国家名称:
locale.getDisplayCountry();
您可以在文档中查看更多可用方法:
http://developer.android.com/reference/java/util/Locale.html
编辑:
如果 Place 对象的区域设置为空,您可以使用地理编码器从 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 并且它也可能检索不到任何答案。
这将给出正确的国家代码。
TelephonyManager tm = (TelephonyManager)activity.getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso();
这里的所有答案都指向已弃用的 SDK 或使用 TelephonyManager 或 GeoCoder(而 OP 明确提到从地方 SDK 响应获取国家代码。我也 它很明显,从 OS(TelephonyManager 或 GeoCoder)中选择国家或国家代码不一定与用户从选择器 中选择的位置返回的位置相匹配。
在较新的 places SDK(com.google.android.libraries.places:places:2.4.0) 中,我按照代码片段设法获取了国家代码
确保包括以下字段(除了您还需要的)
val fields = listOf(
Place.Field.ADDRESS_COMPONENTS,
// ... More fields that you need
)
并按照此代码解析国家代码
place.addressComponents.asList().find { it.types.contains("country") }.shortName
在 Google Places API for Android 中,我使用 PlaceAutocompleteFragment 来显示 city/country。
我在这里获取地址、名称、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()));
}
但我也想要国家代码。有没有办法从 API 的地方获取国家代码? 如果没有,是否有任何替代服务可以在输入时获取国家/地区名称和代码?
通过检索 Place 对象,您可以获得关联的 Locale 对象:
Locale locale = place.getLocale();
使用此 Locale 对象,您可以通过以下方式获取国家/地区代码:
locale.getCountry();
以及国家名称:
locale.getDisplayCountry();
您可以在文档中查看更多可用方法: http://developer.android.com/reference/java/util/Locale.html
编辑:
如果 Place 对象的区域设置为空,您可以使用地理编码器从 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 并且它也可能检索不到任何答案。
这将给出正确的国家代码。
TelephonyManager tm = (TelephonyManager)activity.getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso();
这里的所有答案都指向已弃用的 SDK 或使用 TelephonyManager 或 GeoCoder(而 OP 明确提到从地方 SDK 响应获取国家代码。我也 它很明显,从 OS(TelephonyManager 或 GeoCoder)中选择国家或国家代码不一定与用户从选择器 中选择的位置返回的位置相匹配。 在较新的 places SDK(com.google.android.libraries.places:places:2.4.0) 中,我按照代码片段设法获取了国家代码 确保包括以下字段(除了您还需要的)
val fields = listOf(
Place.Field.ADDRESS_COMPONENTS,
// ... More fields that you need
)
并按照此代码解析国家代码
place.addressComponents.asList().find { it.types.contains("country") }.shortName