Google Places api:placesClient.findAutocompletePredictions返回任务失败

问题描述

我使用placesClient.findAutocompletePredictions返回位置建议以将其设置为我的搜索栏,但是当我在下面使用此功能时,任务总是不成功,原因是它是通过api键提供的,但是我启用了places api !!。结果总是“预测获取任务失败”

 @Override
                                                    public void onTextChanged(CharSequence s,int start,int before,int count) {
                                                        AutocompleteSessionToken token = AutocompleteSessionToken.newInstance();
                                                        FindAutocompletePredictionsRequest predictionsRequest = FindAutocompletePredictionsRequest.builder()
                                                                .setCountry("sa")
                                                                .setTypeFilter(TypeFilter.ADDRESS)
                                                                .setSessionToken(token)
                                                                .setQuery(s.toString())
                                                                .build();
                                                        placesClient.findAutocompletePredictions(predictionsRequest).addOnCompleteListener(new OnCompleteListener<FindAutocompletePredictionsResponse>() {
                                                            @Override
                                                            public void onComplete(@NonNull Task<FindAutocompletePredictionsResponse> task) {
                                                                if (task.isSuccessful()) {
                                                                    FindAutocompletePredictionsResponse predictionsResponse = task.getResult();
                                                                    if (predictionsResponse != null) {
                                                                        predictionList = predictionsResponse.getAutocompletePredictions();
                                                                        List<String> suggestionsList = new ArrayList<>();
                                                                        for (int i = 0; i < predictionList.size(); i++) {
                                                                            AutocompletePrediction prediction = predictionList.get(i);
                                                                            suggestionsList.add(prediction.getFullText(null).toString());
                                                                        }
                                                                        materialSearchBar.updateLastSuggestions(suggestionsList);
                                                                        if (!materialSearchBar.isSuggestionsVisible()) {
                                                                            materialSearchBar.showSuggestionsList();
                                                                        }
                                                                    }
                                                                } else {
                                                                    Log.i("mytag","prediction fetching task unsuccessful");
                                                                }
                                                            }
                                                        });
                                                    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)