Reputation: 337
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
Upvotes: 26
Views: 33621
Reputation: 1
In addition to setting up API key and enabling APIs as noted by others, I had to do the following:
Upvotes: 0
Reputation: 1
You can use this version of the places api as the dependency.
implementation 'com.google.android.libraries.places:places-compat:1.1.0'
Upvotes: 0
Reputation: 121
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!!! :)
Upvotes: 0
Reputation: 6752
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 linkstep by step guide to implement Autocomplete in our app.
Hope this will help you.Thanks.....
Upvotes: 1
Reputation: 11
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
Upvotes: 1
Reputation: 4782
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
Upvotes: 1
Reputation: 9
googleApiClient.connect();
should be called in the method onMapReady of the interface OnMapReadyCallback.
Upvotes: 0
Reputation: 11565
Step by step Solution that I follows :
Just go to https://console.developers.google.com/
open your project and click on Library from Left tabs
Now in Mobile APIs do click on Google Places API for Android and ENABLE the api
Upvotes: 26
Reputation: 2940
Enable the Google Places API for Android in developers console and check on the credentials page that your key is still present
Upvotes: 51