Java Android谷歌地址自动完成Pendingreult将永远等待
我和我的项目团队正在尝试使用android的谷歌API获取一个地址来自动完成。 到目前为止,我们正在尝试用一个词来测试它,而不必担心在autoCompleteView中添加侦听器。不管怎样,我们的第一次测试无法进行。 我们的GoogleAppClient设置如下:Java Android谷歌地址自动完成Pendingreult将永远等待,java,android,google-maps,google-maps-android-api-2,google-geocoding-api,Java,Android,Google Maps,Google Maps Android Api 2,Google Geocoding Api,我和我的项目团队正在尝试使用android的谷歌API获取一个地址来自动完成。 到目前为止,我们正在尝试用一个词来测试它,而不必担心在autoCompleteView中添加侦听器。不管怎样,我们的第一次测试无法进行。 我们的GoogleAppClient设置如下: mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Places.GEO_DATA_API) .addCon
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Places.GEO_DATA_API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
@Override
public void onConnected(Bundle connectionHint) {
new GetPredictions().execute("boulevard");
}
@Override
protected AutocompletePredictionBuffer doInBackground(String... test) {
LatLng sudOuestLyon = new LatLng(45.708931, 4.745801);
LatLng nordEstLyon = new LatLng(45.805918, 4.924447);
LatLngBounds rectangleLyon = new LatLngBounds(sudOuestLyon, nordEstLyon);
PendingResult<AutocompletePredictionBuffer> results =
Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, test[0],
rectangleLyon, null);
AutocompletePredictionBuffer autocompletePredictions = results
.await();
return autocompletePredictions;
}
我们有一个连接侦听器,如下所示:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Places.GEO_DATA_API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
@Override
public void onConnected(Bundle connectionHint) {
new GetPredictions().execute("boulevard");
}
@Override
protected AutocompletePredictionBuffer doInBackground(String... test) {
LatLng sudOuestLyon = new LatLng(45.708931, 4.745801);
LatLng nordEstLyon = new LatLng(45.805918, 4.924447);
LatLngBounds rectangleLyon = new LatLngBounds(sudOuestLyon, nordEstLyon);
PendingResult<AutocompletePredictionBuffer> results =
Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, test[0],
rectangleLyon, null);
AutocompletePredictionBuffer autocompletePredictions = results
.await();
return autocompletePredictions;
}
最后(当然不是最后,但这是我们的代码在等待中被阻止之前所做的),我们的AsyncTask类GetPredictions有一个doInBackground方法,如下所示:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Places.GEO_DATA_API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
@Override
public void onConnected(Bundle connectionHint) {
new GetPredictions().execute("boulevard");
}
@Override
protected AutocompletePredictionBuffer doInBackground(String... test) {
LatLng sudOuestLyon = new LatLng(45.708931, 4.745801);
LatLng nordEstLyon = new LatLng(45.805918, 4.924447);
LatLngBounds rectangleLyon = new LatLngBounds(sudOuestLyon, nordEstLyon);
PendingResult<AutocompletePredictionBuffer> results =
Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, test[0],
rectangleLyon, null);
AutocompletePredictionBuffer autocompletePredictions = results
.await();
return autocompletePredictions;
}
@覆盖
受保护的AutocompletePredictionBuffer doInBackground(字符串…测试){
LatLng sudOuestLyon=新LatLng(45.708931,4.745801);
LatLng nordEstLyon=新LatLng(45.805918,4.924447);
LatLngBounds矩形=新的LatLngBounds(sudOuestLyon,nordEstLyon);
Pendingreult结果=
Places.GeoDataApi.getAutocompletePredictions(mgoogleAppClient,测试[0],
矩形,空);
AutocompletePredictionBuffer autocompletePredictions=结果
.等待();
返回自动完成预测;
}
在调试之后,我们看到我们被困在“等待”中(并且有时间限制的版本超时)。我们尝试给它一些时间(几分钟),但应用程序没有通过这个方法。研究区(长方形里昂)是一个长方形,覆盖法国里昂市及其周边地区。这不是一个很大的区域,也没有太多的林荫大道,所以我们认为搜索其中的“林荫大道”会很快返回结果。我们怀疑我们的GoogleAppClient可能配置不正确,但它可以连接,因为我们传入的方法只能通过OnConnected调用,而错误的请求一开始不会返回Pendingreult,对吗
为什么此代码会阻塞
results.await()
?因此,经过更多搜索后,问题是清单中自动声明密钥的行。密钥声明需要有“android:name=“com.google.android.geo.API_Key”,而不是原来的名称。我想你可以先试试谷歌地图自动完成的官方示例来看看它是如何工作的。