Places for Android API deprecated alternative

Google Places SDK for Android is Deprecated, so we need to migrate for Places API.
For implementing AutoComplete Place using new Places API.. please follow below steps.

First enable PlacesAPI in developer console, then install Client Library by updating in gradle.

(Note: You can only install either the client library or the
compatibility library, NOT both)

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

Now initialize below code inside Oncreate();

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

    // Initialize Places.
    Places.initialize(getApplicationContext(), "***YOUR API KEY***");

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

New PlacesAPI is initialised..

For AutoComplete places use below code (You can use AutoComplete Fragment also)

// Set the fields to specify which types of place data to return.
List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
// Start the autocomplete intent.
Intent intent = new Autocomplete.IntentBuilder(
        AutocompleteActivityMode.FULLSCREEN, fields)
        .build(this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Place place = Autocomplete.getPlaceFromIntent(data);
            Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
        } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
            // TODO: Handle the error.
            Status status = Autocomplete.getStatusFromIntent(data);
            Log.i(TAG, status.getStatusMessage());
        } else if (resultCode == RESULT_CANCELED) {
            // The user canceled the operation.
        }
    }
}
  • Make sure permissions in manifest
  • API key generated.
  • Places API is enabled in Dev Console.

REMOVE(if you added)

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

Required header files

import com.google.android.libraries.places.api.Places;
import com.google.android.libraries.places.api.model.Place;
import com.google.android.libraries.places.api.net.PlacesClient;
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;

Hope this will help..

Leave a Comment