【发布时间】:2020-05-05 15:11:41
【问题描述】:
我关注了migration guide,所以我在build.gradle中做了如下改动
先决条件:
- 在切换之前它正在工作
- 必需的 Maps API 已启用
- 结算功能也已启用
- 使用了正确的 API 密钥
dependencies {
//noinspection GradleCompatible
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation 'com.google.android.gms:play-services-places:17.0.0'
implementation 'com.google.android.libraries.places:places:2.1.0'
implementation 'com.google.android.libraries.places:places-compat:2.1.0'
}
现在我收到 PLACE_DETECTION_API 和 GeoDataApi 的变量错误
这就是我在活动中实现搜索的方式
SeachActivity.java
//onCreate() method
mGoogleApiClient = new GoogleApiClient
.Builder(this)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.enableAutoManage(this, this)
.build();
@Override
public void onConnected(@Nullable Bundle bundle) {
PendingResult<AutocompletePredictionBuffer> result =
Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, "infopark", null, null);
result.setResultCallback(new ResultCallback<AutocompletePredictionBuffer>() {
@Override
public void onResult(@NonNull AutocompletePredictionBuffer autocompletePredictions) {
Log.i(TAG, "onConnected: Result" + autocompletePredictions);
}
});
}
@Override
protected ArrayList<AutocompletePrediction> doInBackground(String... params) {
if (mGoogleApiClient.isConnected()) {
Log.i(TAG, "Starting autocomplete query for: " + strAddress);
PendingResult<AutocompletePredictionBuffer> results =
Places.GeoDataApi
.getAutocompletePredictions(mGoogleApiClient, strAddress, null,
null);
AutocompletePredictionBuffer autocompletePredictions = results
.await(60, TimeUnit.SECONDS);
final com.google.android.gms.common.api.Status status =
autocompletePredictions.getStatus();
if (!status.isSuccess()) {
Log.e(TAG, "Error getting autocomplete prediction API call: " +
status.toString());
autocompletePredictions.release();
return null;
}
Log.i(TAG, "Query completed. Received " + autocompletePredictions.getCount()
+ " predictions.");
return DataBufferUtils.freezeAndClose(autocompletePredictions);
}
Log.e(TAG, "Google API client is not connected for autocomplete query.");
return null;
}
SearchResultsRecycler.java
public ViewHolder(View itemView) {
super(itemView);
txtPlace = (TextView) itemView.findViewById(R.id.txt_place);
txtAddress = (TextView) itemView.findViewById(R.id.txt_address);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AutocompletePrediction item = mResultList.get(getLayoutPosition());
String placeId = item.getPlaceId();
CharSequence primaryText = item.getPrimaryText(null);
PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
.getPlaceById(mGoogleApiClient, placeId);
placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
}
});
}
}
我把初始化改成:
Places.initialize(getApplicationContext(), apiKey);
PlacesClient placesClient = Places.createClient(this);
错误仍然存在。我在这里看到了各种答案,有些告诉使用意图,片段,仍然是同样的问题。这很混乱。
【问题讨论】:
-
请在下面查看我的答案,如果解决方案有效,请告诉我。如果问题仍然存在,请分享您在 logcat 中遇到的错误并提供您的完整代码,以便我们可以从我们这边重现此问题。
标签: java android google-maps google-places-api