不推荐使用的 Android API 替代方案 [英] Places for Android API deprecated alternative

查看:26
本文介绍了不推荐使用的 Android API 替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现 Places API.我的代码如下所示:

I was trying to implement the Places API. My code looked like this:

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

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

My maps credentials were correct, but for this call I got

Android 版 Places API 似乎没有为您的应用启用.请参阅 https://developers.google.com/places/android/signup 了解更多信息详情.

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

但是,当我尝试启用Android 版 Places API"时,出现此错误.

However, when I tried to enable the "Places API for Android", I got this error.

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

You do not have sufficient permissions to view this page.

我尝试注销我的帐户,重新登录,隐身模式,Safari &铬合金.没有任何效果,所以我联系了支持人员,速度非常快(谢谢大家!)

I tried logging out of my accounts, logging in again, incognito mode, Safari & Chrome. Nothing worked so I contacted support, which were extremely fast (thanks guys!)

您在尝试启用 Places 时收到错误的原因对于 Android API,它已被弃用.地方功能现在将通过启用 Places API 来涵盖 Android 版.

The reason you are receiving an error when trying to enable the Places for Android API is that it has been deprecated. Places functionality for android will now be covered by having the Places API enabled.

我询问了我的实施并得到了这个答复.

I asked about my implementation and got this reply.

地点选择器也已弃用.您可以安装继续使用位置选择器的兼容性库,直到弃用期将于 7 月 29 日结束.更多关于这方面的信息可以在这里显示:https://developers.google.com/places/android-sdk/客户端迁移#place_picker

The place picker has also been deprecated. You can install the compatibility library to continue using the Place Picker until the deprecation period ends on July 29th. More about this can be red here: https://developers.google.com/places/android-sdk/client-migration#place_picker

我现在在网上找到的文档有点混乱,哪些是已弃用的,哪些不是?任何人都可以为这种功能指出正确的方向吗?

The docs I find online now are a bit confusing, what is deprecated and what isn't? Can anyone point me in the right direction for this kind of functionality?

推荐答案

Google Places SDK for Android 已弃用,因此我们需要针对 Places API 进行迁移.要使用新的 Places API 实现 AutoComplete Place.. 请按照以下步骤操作.

Google Places SDK for Android is Deprecated, so we need to migrate for Places API. For implementing AutoComplete Place using new Places API.. please follow below steps.

首先在开发者控制台中启用 PlacesAPI,然后通过在 gradle 中更新来安装客户端库.

First enable PlacesAPI in developer console, then install Client Library by updating in gradle.

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

(Note: You can only install either the client library or the compatibility library, NOT both)

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

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

Now initialize below code inside 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 已初始化..

New PlacesAPI is initialised..

对于自动完成位置使用以下代码(您也可以使用自动完成片段)

For AutoComplete places use below code (You can use AutoComplete Fragment also)

// 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 密钥已生成.
  • 在开发控制台中启用了 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;
    

    希望这会有所帮助..

    这篇关于不推荐使用的 Android API 替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆