Android API的地方已被弃用

问题描述 投票:4回答:3

我正在尝试实施Places API。我的代码看起来像这样:

val builder = PlacePicker.IntentBuilder()
startActivityForResult(builder.build(mActivity), PLACE_PICKER_REQUEST)

我的地图凭据是正确的,但是我得到了这个电话

Places API for Android does not seem to be enabled for your app. See https://developers.google.com/places/android/signup for more details.

但是,当我尝试启用“Places API for Android”时,我收到了此错误。

您没有足够的权限来查看此页面。

我尝试退出帐户,再次登录,隐身模式,Safari和Chrome。什么都没有用,所以我联系了支持,这非常快(谢谢你们!)

您尝试启用Places for Android API时收到错误的原因是它已被弃用。现在,启用Places API可以覆盖Android的位置功能。

我询问了我的实施并得到了这个回复。

地点选择器也已被弃用。您可以安装兼容性库以继续使用场地选择器,直到弃用期在7月29日结束。关于这一点的更多信息可以是红色的:https://developers.google.com/places/android-sdk/client-migration#place_picker

我现在在网上找到的文档有点令人困惑,什么是弃用的,什么不是?任何人都可以指出我正确的方向这种功能吗?

android google-maps-api-3 google-places-api googleplacesautocomplete
3个回答
10
投票

Google Places SDK for Android已弃用,因此我们需要迁移Places API。要使用新的Places API实施自动完成展示位置,请按以下步骤操作。

首先在开发人员控制台中启用PlacesAPI,然后通过gradle更新来安装Client Library。

(注意:您只能安装客户端库或兼容性库,而不能同时安装两者)

implementation 'com.google.android.libraries.places:places:1.0.0'

现在在Oncreate()中初始化下面的代码;

 // Add an import statement for the client library.
    import com.google.android.libraries.places.api.Places;

    // Initialize Places.
    Places.initialize(getApplicationContext(), "***YOUR API KEY***");

   // Create a new Places client instance.
   PlacesClient placesClient = Places.createClient(this);

新PlacesAPI已初始化..

对于自动完成的地方使用下面的代码(您也可以使用自动完成片段)

// Set the fields to specify which types of place data to return.
List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
// Start the autocomplete intent.
Intent intent = new Autocomplete.IntentBuilder(
        AutocompleteActivityMode.FULLSCREEN, fields)
        .build(this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Place place = Autocomplete.getPlaceFromIntent(data);
            Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
        } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
            // TODO: Handle the error.
            Status status = Autocomplete.getStatusFromIntent(data);
            Log.i(TAG, status.getStatusMessage());
        } else if (resultCode == RESULT_CANCELED) {
            // The user canceled the operation.
        }
    }
}
  • 确保清单中的权限
  • 生成API密钥。
  • 在Dev Console中启用了Places API。

删除(如果你添加)

implementation 'com.google.android.gms:play-services-places:16.0.0'

必需的头文件

import com.google.android.libraries.places.api.Places;
import com.google.android.libraries.places.api.model.Place;
import com.google.android.libraries.places.api.net.PlacesClient;
import com.google.android.libraries.places.widget.Autocomplete;
import com.google.android.libraries.places.widget.AutocompleteActivity;
import com.google.android.libraries.places.widget.model.AutocompleteActivityMode;

希望这会有所帮助..


0
投票

键可能需要一段时间才能开始工作。根据文件:

完全配置密钥可能需要5分钟。如果密钥不能立即生效,请在5分钟后重试。

https://developers.google.com/places/android-sdk/signup


0
投票

适用于Android的Places SDK的Google Play服务版本(在Google Play Services 16.0.0中)已于2019年1月29日弃用,并将于2019年7月29日关闭。如果您在google API控制台下查看,它只有PLACES API,而且没有像PLACES SDK FOR ANDROID了。

迁移指南新地方SDK客户端库可以找到here

或者在我的repo => NewGooglePlacesSDK中找到自定义实现


0
投票

尽管Google已经为Places SDK提供了新的API,但由于新的结算政策,他们正在放弃对Place Picker的支持,并且有两种方法可以解决这个问题。

  1. 您可以使用新SDK自行实现此逻辑。
  2. 或者幸运的是,这个Ping Place Picker库模仿旧的Place Picker的行为。
© www.soinside.com 2019 - 2024. All rights reserved.