java-Places.GeoDataApi.getAutocompletePredictions
作者:互联网
我在使用Android Google Places API时遇到问题-自动完成功能.
我正在尝试使用Places.GeoDataApi.getAutocompletePredictions()方法获取placeId,但是,它不起作用.(输出甚至不显示“无法获取值”)
有人可以帮我吗?
这是我的Java代码:
PendingResult<AutocompletePredictionBuffer> result =
Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, query,
mLatLonBounds, null);
result.setResultCallback(new ResultCallback<AutocompletePredictionBuffer>() {
@Override
public void onResult(@NonNull AutocompletePredictionBuffer autocompletePredictions) {
if (autocompletePredictions.getStatus().isSuccess()) { //如果回傳SUCCESS
Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
resultList = new ArrayList<>(autocompletePredictions.getCount());
while (iterator.hasNext()) {
AutocompletePrediction prediction = iterator.next();
resultList.add(prediction.getPlaceId().toString());
}
}
else {
resultList.add("can't get the vaule");
}
autocompletePredictions.release();
}
});
textView.setText(resultList.get(0).toString());
谢谢.
解决方法:
我遇到了同样的问题,请按照以下步骤操作
>在开发人员控制台中启用Android版Google Places API,并
在凭证页面上检查您的密钥仍然存在.
>将所需的库添加到模块(play-services-places:11.2.0).
>使用以下代码获取预测
Task<AutocompletePredictionBufferResponse> results = mGeoDataClient.getAutocompletePredictions(yourInputString, yourLatLngBounds, yourTypeFilter);
>添加成功侦听器以进行以下预测
results.addOnSuccessListener(new OnSuccessListener<AutocompletePredictionBufferResponse>() {
@Override
public void onSuccess(AutocompletePredictionBufferResponse autocompletePredictions) {
Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
suggesstions = new ArrayList<String>(autocompletePredictions.getCount());
if (iterator.hasNext()) {
while (iterator.hasNext()) {
AutocompletePrediction prediction = iterator.next();
//do your stuff with prediction here
}
}else {
//Nothing matches...
}
autocompletePredictions.release();
}
});
>添加故障侦听器以进行以下预测
results.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MapsActivity.this,"Error while fetching suggessions : "+e.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
}
});
如果以上所有代码段均已正确放置,则您可能需要检查Internet连接.
希望以上代码对您有所帮助.
谢谢.
标签:google-places-api,autocomplete,java,android 来源: https://codeday.me/bug/20191026/1933015.html