切换到新 Google Maps SDK 时出现迁移错误
Migration error while switching to new Google Maps SDK
我遵循了 migration guide,所以我在 build.gradle
中做了以下更改
先决条件:
- 切换前正常工作
- 所需地图 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
的变量错误
这就是我在 activity
中实现搜索的方式
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);
错误依然存在。我在这里看到了各种答案,有些告诉我要使用意图,片段,仍然是同样的问题。真是一头雾水。
您依赖于已弃用的 SDK、客户端库和兼容性库。您只能实现这 3 个中的一个。尝试删除这 2 个依赖项:
implementation 'com.google.android.gms:play-services-places:17.0.0'
implementation 'com.google.android.libraries.places:places-compat:2.1.0'
以便您仅在 build.gradle
:
中实现客户端库
implementation 'com.google.android.libraries.places:places:2.1.0'
然后确保你有:
(1) 迁移到 AndroidX
(2) 通过 import com.google.android.libraries.places.api.Places
导入客户端库
(3) 在您的项目
上启用了 Places API
希望对您有所帮助!
我遵循了 migration guide,所以我在 build.gradle
中做了以下更改先决条件:
- 切换前正常工作
- 所需地图 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
这就是我在 activity
中实现搜索的方式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);
错误依然存在。我在这里看到了各种答案,有些告诉我要使用意图,片段,仍然是同样的问题。真是一头雾水。
您依赖于已弃用的 SDK、客户端库和兼容性库。您只能实现这 3 个中的一个。尝试删除这 2 个依赖项:
implementation 'com.google.android.gms:play-services-places:17.0.0'
implementation 'com.google.android.libraries.places:places-compat:2.1.0'
以便您仅在 build.gradle
:
implementation 'com.google.android.libraries.places:places:2.1.0'
然后确保你有:
(1) 迁移到 AndroidX
(2) 通过 import com.google.android.libraries.places.api.Places
导入客户端库
(3) 在您的项目
希望对您有所帮助!