如何为Google PlaceAutocomplete设置语言

时间:2018-11-01 13:46:19

标签: android google-places-api

我正在使用Google PlaceAutocomplete,因此用户可以选择其地址。

我的代码是:

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() + ""));


        }
    };

我想要阿拉伯文的结果。经过研究,我发现它取决于用户系统语言,有没有办法仅使用阿拉伯语?

4 个答案:

答案 0 :(得分:1)

您可以在initialize调用中对其进行更改:

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

第三个参数是语言。

答案 1 :(得分:0)

什么都没有。您可以使用网络API ,但是:

  

语言代码,指示应以哪种语言返回结果。

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

答案 2 :(得分:0)

以下是https://developers.google.com/maps/faq#languagesupport支持的语言列表,您可以从这里获取语言代码。

因此,当您调用地点详细信息api时,可以添加language参数,该参数是可选的,以查看以下网址中的详细信息 https://developers.google.com/places/web-service/details

答案 3 :(得分:0)

Places API 返回具有短地址和latlng坐标的Place对象,但是要获得具有 EN语言环境的完整地址,您可以执行以下操作:

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