简体   繁体   中英

Android Google Places API, getAutocompletePredictions returns status 'PLACES_API_ACCESS_NOT_CONFIGURED'

I'm having problems with Android Google Places API - auto complete feature. I use the same key that i used for Android Google Maps API (and in the documentation, it is written this is ok). Here is my definition in manifest:

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

But getAutocompletePredictions returns 'PLACES_API_ACCESS_NOT_CONFIGURED' message as status.

Here is my Java code:

GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
        .addApi(Places.GEO_DATA_API)
        .addApi(Places.PLACE_DETECTION_API)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();
googleApiClient.connect();

LatLngBounds latLngBounds = new LatLngBounds.Builder().
        include(SphericalUtil.computeOffset(latlon, RADIUS, 0)).
        include(SphericalUtil.computeOffset(latlon, RADIUS, 90)).
        include(SphericalUtil.computeOffset(latlon, RADIUS, 180)).
        include(SphericalUtil.computeOffset(latlon, RADIUS, 270)).build();

PendingResult<AutocompletePredictionBuffer> result = Places.GeoDataApi.getAutocompletePredictions(googleApiClient, constraint.toString(), latLngBounds, null);

AutocompletePredictionBuffer autocompletePredictions = result.await(Config.DATA_TIMEOUT, TimeUnit.MILLISECONDS);

Status status = autocompletePredictions.getStatus();
if (status.isSuccess()) {
    Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
    while (iterator.hasNext()) {
        AutocompletePrediction prediction = iterator.next();
        //... do stuff here ...
    }
}
else {
    Log.d(TAG, status.getStatusMessage());
}

autocompletePredictions.release();
googleApiClient.disconnect();

Thanks in advance

在开发人员控制台中启用Android版Google Places API,并在凭据页面上检查您的密钥仍然存在

Step by step Solution that I follows :

在此处输入图片说明

I think you tried enabling Google Places API for Android

Enable this as well it works . You can found this below Google Places API for Android

Google Places API Web Service

Hi Friends I have successfully resolve this issue.I was implementing Auto Complete Goole places in my app. This was provided by Google play services. Now the main issue is that Place SDK for Android is deprecated from Google play services on 29 Jan.Now,A new SDK is released instead of. Place sdk for Android. Now you can use this migration guide to implement place sdk in your project. Link is available here.

Place SDK for Android migration guide.

Also see the step by step guide how to implement AutoComplete in your project .Look here this link step by step guide to implement Autocomplete in our app.

Hope this will help you.Thanks.....

After a lot of searching and spending a lot of time, I found out that we need to replace From implementation 'com.google.android.gms:play-services-places:15.0.1'

Replace to implementation 'com.google.android.libraries.places:places-compat:1.0.0'

The code demo can be found at following location: https://github.com/googlemaps/android-places-demos

googleApiClient.connect(); should be called in the method onMapReady of the interface OnMapReadyCallback.

there are two ways to get your Place API working either by Intent or AutocompleteFragment, in the case of intent you can restrict your search to a specific country but the same is not applicable to the AutocompleteFragment yet. below is the code that worked for me:

  <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"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

activityCode:

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.api.Status;
import com.google.android.libraries.places.api.Places;
import com.google.android.libraries.places.api.model.Place;
import com.google.android.libraries.places.widget.AutocompleteSupportFragment;
import com.google.android.libraries.places.widget.listener.PlaceSelectionListener;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;

public class AutocompleteFragmentActivity extends AppCompatActivity {
    private String TAG ="FRAGMENT_AUTOCOMPLETE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_autocomplete_fragment);

        /**
         * 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(), "API_KEY");
        }

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

        assert autocompleteFragment != null;
        autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));


        autocompleteFragment.setOnPlaceSelectedListener(placeSelectionListener);

    }
    PlaceSelectionListener placeSelectionListener = new PlaceSelectionListener() {


        @Override
        public void onPlaceSelected(@NonNull Place place) {
            Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
            Toast.makeText(AutocompleteFragmentActivity.this, place.getName()+", "+ place.getAddress(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(@NotNull Status status) {
            Log.i(TAG, "An error occurred: " + status);

        }
    };

}

activity class for Intent

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.widget.Toast
import com.google.android.libraries.places.api.Places
import com.google.android.libraries.places.api.model.Place
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
import java.util.*


class AutocompleteActivity : AppCompatActivity() {
    private var AUTOCOMPLETE_REQUEST_CODE = 0
    private var TAG: String = "AUTOCOMPLETE_ACTIVITY"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_autocomplete)

        if (!Places.isInitialized()){
            Places.initialize(this, "API_KEY")
        }
       // val placesClient = Places.createClient(this)
        val fields = Arrays.asList(Place.Field.ID, Place.Field.NAME,Place.Field.ADDRESS)


        val intent = Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields)
            .setCountry("NG")
            .build(this)

        startActivityForResult(intent,AUTOCOMPLETE_REQUEST_CODE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == AUTOCOMPLETE_REQUEST_CODE){
            if (resultCode == Activity.RESULT_OK){
                val place = Autocomplete.getPlaceFromIntent(data!!)
                Log.i(TAG,"Place: "+ place.name + ", "+ place.id)
            }
            else if (resultCode == AutocompleteActivity.RESULT_ERROR){
                val status = Autocomplete.getStatusFromIntent(data!!)
                Log.i(TAG, status.statusMessage)
            }
            else if (resultCode == Activity.RESULT_CANCELED){
                Toast.makeText(this@AutocompleteActivity, "You cancelled the operation", Toast.LENGTH_LONG).show()

            }
        }
    }
}

Don't forget to add your dependencies

dependencies {

    implementation 'com.google.android.gms:play-services-places:16.0.0'
    implementation 'com.google.android.libraries.places:places:1.0.0'
}

For this to work you must add both to your gradle file Try this it should work!!! :)

您可以将此版本的places api用作依赖项。

implementation 'com.google.android.libraries.places:places-compat:1.1.0'

In addition to setting up API key and enabling APIs as noted by others, I had to do the following:

  1. Set up a billing account (I was getting "maximum limit exceeded..." error
  2. Switch to using the Places SDK client library as outlined here: https://developers.google.com/places/android-sdk/client-migration . (Note that I did not choose to use the compatibility library since Google Play Services version of the Places SDK for Android will be turned off on July 29, 2019.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM