Enaam Riyadh
Enaam Riyadh

Reputation: 141

How to set language for Google PlaceAutocomplete

I am using Google PlaceAutocomplete, so the user can select his address.

My code is:

mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
                .addApi(Places.GEO_DATA_API)
                .enableAutoManage(this, GOOGLE_API_CLIENT_ID, this)
                .addConnectionCallbacks(this)
                .build();
        AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()

                .setCountry("IQ")
                .build();


        mAutocompleteTextView.setOnItemClickListener(mAutocompleteClickListener);
        mPlaceArrayAdapter = new PlaceArrayAdapter(this, android.R.layout.simple_list_item_1,
                BOUNDS_MOUNTAIN_VIEW, typeFilter);
        mAutocompleteTextView.setAdapter(mPlaceArrayAdapter);

    }

    private AdapterView.OnItemClickListener mAutocompleteClickListener
            = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            final PlaceArrayAdapter.PlaceAutocomplete item = mPlaceArrayAdapter.getItem(position);
            final String placeId = String.valueOf(item.placeId);
            Log.i(TAG, "Selected: " + item.description);
            PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
                    .getPlaceById(mGoogleApiClient, placeId);
            placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
            Log.i(TAG, "Fetching details for ID: " + item.placeId);
        }
    };

    private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback
            = new ResultCallback<PlaceBuffer>() {
        @Override
        public void onResult(PlaceBuffer places) {
            if (!places.getStatus().isSuccess()) {
                Log.e(TAG, "Place query did not complete. Error: " +
                        places.getStatus().toString());
                return;
            }
            // Selecting the first object buffer.
            final Place place = places.get(0);
            CharSequence attributions = places.getAttributions();

            mNameView.setText(Html.fromHtml(place.getAddress() + ""));


        }
    };

I want the result in Arabic. After some research I found that it depends on user system language, is there a way to make it only in Arabic?

Upvotes: 1

Views: 2121

Answers (4)

Rodrigo Alfonso
Rodrigo Alfonso

Reputation: 129

You can change it in the initialize call:

Places.initialize(applicationContext, "API_KEY", Locale.US)

The third parameter is the language.

Upvotes: 2

Taras Vovkovych
Taras Vovkovych

Reputation: 4252

Places API returns you the Place object with the short address and latlng coordinates, but to get the full address with the EN locale you can do with this:

public Address getFullAddress(Place place){
        Address address;

        Locale aLocale = new Locale.Builder().setLanguage("en").build();
        Geocoder geocoder = new Geocoder(this, aLocale);

        try
        {
            List<Address> addresses = geocoder.getFromLocation(place.getLatLng().latitude,place.getLatLng().longitude, 1);

            address = addresses.get(0);

            return address;

        } catch (IOException e)
        {

            e.printStackTrace();
        }

        return null;
    }

Upvotes: 0

aanshu
aanshu

Reputation: 1602

Here is the list of the languages supported https://developers.google.com/maps/faq#languagesupport & you may get language code from here.

So when you are calling place detail api, you may add language parameter which is optional, to see the details at following url https://developers.google.com/places/web-service/details

Upvotes: 0

ILLIA DEREVIANKO
ILLIA DEREVIANKO

Reputation: 790

Nothing. You can use web api, but:

The language code, indicating in which language the results should be returned, if possible.

More: https://developers.google.com/places/web-service/autocomplete

Upvotes: 0

Related Questions