How to resolve PLACES_API_ACCESS_NOT_CONFIGURED in Place Autocomplete Fragment

心不动则不痛 提交于 2019-11-28 12:46:00

问题


I'm having problems with Android Google Places API - autocomplete feature. I use the same key that I used for Android Google Maps API but it is not open place fragment. Its only show Maps but when open a Place Autocomplete Fragment but its open and automatically close and show PLACES_API_ACCESS_NOT_CONFIGURED error. Please help me out to this issue and show the right to handle the issue. Here My Manifest XML:

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

And Here My Code for open Place AutocolmpleteFragment:

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

            } catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {

                // TODO: Handle the error.

            }

And Here My code for getting a place response:

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);
        }
    }

回答1:


I was having the same problem but i have been able to fix it.

The problem is that The Google Play Services version of the Places SDK for Android (in Google Play Services 16.0.0) is deprecated as of January 29, 2019, and will be turned off on July 29, 2019. And if you check under the google API console, it only have the PLACES API and there is nothing like PLACES SDK FOR ANDROID anymore.

In the dependencies section of your app-level build.gradle file, add a dependency for the new SDK client library, as shown in the following example:

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

Then, you initialize it.

// 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);

Programmatic autocomplete

The following changes were made to autocomplete: PlaceAutocomplete is renamed to Autocomplete. PlaceAutocomplete.getPlace is renamed to Autocomplete.getPlaceFromIntent. PlaceAutocomplete.getStatus is renamed to Autocomplete.getStatusFromIntent. PlaceAutocomplete.RESULT_ERROR is renamed to AutocompleteActivity.RESULT_ERROR (error handling for the autocomplete fragment has NOT changed).

If you are using the autocomplete widget like my case you can use this.

<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"
  />

Then initialize places

/**
 * 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);
    }
});

Check here for more information

Migrating to the New Place SDK




回答2:


looks like you are getting the PLACES_API_ACCESS_NOT_CONFIGURED alert because you don't have some configuration that needs to be done :

1) Go to google cloud platform

2) Enable your place API inside the console

3) If you already did - you need to check 2 things :

3-A) Is your API key is up to date?(in the credentials page)

3-B) In the credentials page - check for any kind of restrictions that could prevent your from getting acces to the API.



来源:https://stackoverflow.com/questions/54669261/how-to-resolve-places-api-access-not-configured-in-place-autocomplete-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!