简体   繁体   中英

Get country code from PlaceAutocompleteFragment android (Google Places API)

In Google Places API for Android, I am using PlaceAutocompleteFragment for displaying the city/country.
Here am getting the address, name, placeId, etc.
Place object contains only these fields.

@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()));

}

But I want country code also. Is there any way to get the country code from places API? If no, Is there any alternate service for getting the country name & code while typing?

With the Place object retrieved, you can get the Locale object associated :

Locale locale = place.getLocale();

With this Locale object you can then get the Country code via :

locale.getCountry();

And the country name with :

locale.getDisplayCountry();

You can look to more available methods at the docs : http://developer.android.com/reference/java/util/Locale.html

EDIT :

If Locale from the Place object is null you can use a Geocoder to get information from GPS coordinates :

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

Then you can call these methods to get the informations you want

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

More methods on the Address objects : http://developer.android.com/reference/android/location/Address.html

Be aware that the Geocoder methods should be call on a background thread to not block the UI and that it could also retrieve no answer.

This will give the correct country code.

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

Here all answer points to either deprecated SDK or using TelephonyManager or GeoCoder( while OP clearly mentioned getting the country code from places SDK response . I also it should be obvious that choosing a country or country code from the OS(TelephonyManager or GeoCoder) should not necessarily match the location returned by the User selected location from the picker ). In the newer places SDK( com.google.android.libraries.places:places:2.4.0 ), I am managed to get the country code by following the code snippet Make sure to include the following field(in addition to what more you require)

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

And to parse the country code follow this code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM