Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

android - How to resolve PLACES_API_ACCESS_NOT_CONFIGURED in Place Autocomplete Fragment

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);
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...