【问题标题】:Migration error while switching to new Google Maps SDK切换到新的 Google Maps SDK 时出现迁移错误
【发布时间】:2020-05-05 15:11:41
【问题描述】:

我关注了migration guide,所以我在build.gradle中做了如下改动

先决条件:

  1. 在切换之前它正在工作
  2. 必需的 Maps API 已启用
  3. 结算功能也已启用
  4. 使用了正确的 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


【解决方案1】:

您依赖于已弃用的 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

希望这会有所帮助!

【讨论】:

  • 它有帮助,现在我正在使用片段方法。
  • 很高兴听到! :)
猜你喜欢
  • 2016-07-02
  • 2014-11-30
  • 2019-07-01
  • 2016-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-10
相关资源
最近更新 更多