更改Google地方信息自动完成预测的语言

时间:2018-09-23 12:30:11

标签: android autocomplete google-places-api

我正在使用Google Map,而Google Places API在我的Android应用程序中。我对自动完成预测有疑问,我正在尝试搜索波斯语地址,但我得到的结果是英文。我也得到波斯语结果的主要文本,但是问题出在次要文本(国家和城市名称)上,即英语Always;像这样:

خیابانچارمردون,伊朗,德黑兰

我尝试了多种方法来解决此问题,并且当我将电话语言更改为波斯语时,结果以波斯语显示。有谁知道我该如何解决?

这是我的自动填充过滤器:

AutocompleteFilter provideAutoCompleteFilter() {
    return new AutocompleteFilter.Builder()
            .setCountry("IR")
            .build();
}

这是我的适配器:

public class PlaceArrayAdapter extends ArrayAdapter<PlaceArrayAdapter.PlaceAutoComplete> implements Filterable {

    private static final String TAG = "PlaceArrayAdapter";
    private GoogleApiClient mGoogleApiClient;
    private AutocompleteFilter mPlaceFilter;
    private LatLngBounds mBounds;
    private ArrayList<PlaceAutoComplete> mResultList;
    private LayoutInflater inflater;


    public PlaceArrayAdapter(Context context, int resource, LatLngBounds bounds,
                             AutocompleteFilter filter) {
        super(context, resource);
        mBounds = bounds;
        mPlaceFilter = filter;
        inflater = LayoutInflater.from(getContext());
    }

    public void setGoogleApiClient(GoogleApiClient googleApiClient) {
        if (googleApiClient == null || !googleApiClient.isConnected()) {
            mGoogleApiClient = null;
        } else {
            mGoogleApiClient = googleApiClient;
        }
    }

    @Override
    public int getCount() {
        return mResultList.size();
    }

    @Override
    public PlaceAutoComplete getItem(int position) {
        return mResultList.get(position);
    }


    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                FilterResults results = new FilterResults();
                if (constraint != null) {
                    mResultList = getPredictions(constraint);

                    if (mResultList != null) {
                        results.values = mResultList;
                        results.count = mResultList.size();
                    }
                }

                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                if (results != null && results.count > 0) {
                    // The API returned at least one result, update the data.
                    notifyDataSetChanged();
                } else {
                    // The API did not return any results, invalidate the data set.
                    notifyDataSetInvalidated();
                }
            }
        };
        return filter;
    }


    private ArrayList<PlaceAutoComplete> getPredictions(CharSequence constraint) {

        if (mGoogleApiClient != null) {

            Log.i(TAG, "Executing autocomplete query for -----> " + constraint);

            PendingResult<AutocompletePredictionBuffer> results = Places.GeoDataApi
                    .getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
                            mBounds, mPlaceFilter);

            // Wait for predictions, set the timeout.
            AutocompletePredictionBuffer autocompletePredictions = results
                    .await(60, TimeUnit.SECONDS);

            final Status status = autocompletePredictions.getStatus();
            if (!status.isSuccess()) {
                Log.e(TAG, "Error getting place predictions -----> " + status
                        .toString());
                autocompletePredictions.release();
                return null;
            }

            Log.i(TAG, "Query completed. Received -----> " + autocompletePredictions.getCount()
                    + " predictions.");
            Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
            ArrayList resultList = new ArrayList<>(autocompletePredictions.getCount());
            while (iterator.hasNext()) {
                AutocompletePrediction prediction = iterator.next();
                resultList.add(new PlaceAutoComplete(prediction.getPlaceId(),
                        prediction.getFullText(null)));
            }
            // Buffer release
            autocompletePredictions.release();
            return resultList;
        }

        Log.e(TAG, " -----> Google API client is not connected.");
        return null;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.auto_complete_text_view,parent,false);
        TextView tv = viewGroup.findViewById(R.id.tv);
        tv.setText(getItem(position).description);
        return viewGroup;
    }



    public class PlaceAutoComplete {

        public CharSequence placeId;
        public CharSequence description;

        PlaceAutoComplete(CharSequence placeId, CharSequence description) {
            this.placeId = placeId;
            this.description = description;
        }

        @Override
        public String toString() {
            return description.toString();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我明白了,使用下面的代码通过更改应用程序语言解决了问题:

    Configuration config = getBaseContext().getResources().getConfiguration();
    Locale locale = new Locale("fa"); // <---- your target language
    Locale.setDefault(locale);
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
    getBaseContext().getResources().getDisplayMetrics());

也许这不是真正的方法,但是确实有效!