如何解决放置自动完成片段中的PLACES_API_ACCESS_NOT_CONFIGURED

问题描述 投票:0回答:2

我遇到Android Google Places API问题 - 自动填充功能。我使用的是与Android Google Maps API相同的密钥,但它不是开放的地方片段。它只显示地图,但是当打开一个Place Autocomplete Fragment但它打开并自动关闭并显示PLACES_API_ACCESS_NOT_CONFIGURED错误。请帮助我解决此问题,并显示处理该问题的权利。这里我的Manifest XML:

  <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyARQ78yaNEJH12aJLM1Y-112WY12p95ZOEGI"/>

在这里我的代码用于打开Place AutocolmpleteFragment:

 try {
                Intent intent =
                        new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
                                .build(getActivity());
                startActivityForResult(intent, 1);

            } catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {

                // TODO: Handle the error.

            }

在这里获取地点响应的代码:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if (resultCode ==RESULT_OK) {
            Place place = PlaceAutocomplete.getPlace(getActivity(), data);
            onPlaceSelected(place,1);
            bar.setVisibility(View.GONE);
            autoCompleteTextViewPickup.setFocusable(true);
        } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {

            Status status = PlaceAutocomplete.getStatus(getActivity(), data);
            this.onError(status,1);
            bar.setVisibility(View.GONE);
            autoCompleteTextViewPickup.setFocusable(true);
        }
    }
android google-maps google-places-api google-places
2个回答
11
投票

我有同样的问题但我能够解决它。

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

在app-level build.gradle文件的dependencies部分中,为新的SDK客户端库添加依赖项,如以下示例所示:

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

然后,您初始化它。

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

...

// Initialize Places.
Places.initialize(getApplicationContext(), apiKey);

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

程序化自动完成

对自动填充功能进行了以下更改:PlaceAutocomplete已重命名为自动填充功能。 PlaceAutocomplete.getPlace重命名为Autocomplete.getPlaceFromIntent。 PlaceAutocomplete.getStatus重命名为Autocomplete.getStatusFromIntent。 PlaceAutocomplete.RESULT_ERROR重命名为AutocompleteActivity.RESULT_ERROR(自动完成片段的错误处理未更改)。

如果你像我的情况一样使用自动完成小部件,你可以使用它。

<fragment
  android:id="@+id/autocomplete_fragment"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:name=
"com.google.android.libraries.places.widget.AutocompleteSupportFragment"
  />

然后初始化地点

/**
 * Initialize Places. For simplicity, the API key is hard-coded. In a production
 * environment we recommend using a secure mechanism to manage API keys.
 */
if (!Places.isInitialized()) {
    Places.initialize(getApplicationContext(), "YOUR_API_KEY");
}

// Initialize the AutocompleteSupportFragment.
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
        getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);

autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        // TODO: Get info about the selected place.
        Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
    }

    @Override
    public void onError(Status status) {
        // TODO: Handle the error.
        Log.i(TAG, "An error occurred: " + status);
    }
});

点击此处了解更多信息

Migrating to the New Place SDK


1
投票

看起来您正在获取PLACES_API_ACCESS_NOT_CONFIGURED警报,因为您没有需要完成的配置:

1)去google cloud platform 2)在控制台中启用place API 3)如果你已经做过 - 你需要检查两件事: 3-A)您的API密钥是最新的吗?(在凭据页面中) 3-B)在凭证页面中 - 检查可能阻止您访问API的任何类型的限制。

© www.soinside.com 2019 - 2024. All rights reserved.