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

查看:175
本文介绍了不推荐使用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!)

尝试启用位置时收到错误的原因 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/client-migration#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?

推荐答案

已弃用Android版Google Places SDK,因此我们需要迁移 Places API . 要使用新的Places API实施自动完成放置.,请按照以下步骤操作.

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天全站免登陆