Home » Android » google maps api 3 – Places for Android API deprecated alternative

google maps api 3 – Places for Android API deprecated alternative

Posted by: admin June 15, 2020 Leave a comment

Questions:

I was trying to implement the Places API. My code looked like this:

val builder = PlacePicker.IntentBuilder()
startActivityForResult(builder.build(mActivity), PLACE_PICKER_REQUEST)

My maps credentials were correct, but for this call I got

Places API for Android does not seem to be enabled for your app. See https://developers.google.com/places/android/signup for more details.

However, when I tried to enable the “Places API for Android”, I got this error.

You do not have sufficient permissions to view this page.

I tried logging out of my accounts, logging in again, incognito mode, Safari & Chrome. Nothing worked so I contacted support, which were extremely fast (thanks guys!)

The reason you are receiving an error when trying to enable the Places
for Android API is that it has been deprecated. Places functionality
for android will now be covered by having the Places API enabled.

I asked about my implementation and got this reply.

The place picker has also been deprecated. You can install the
compatibility library to continue using the Place Picker until the
deprecation period ends on July 29th. More about this can be red here:
https://developers.google.com/places/android-sdk/client-migration#place_picker

The docs I find online now are a bit confusing, what is deprecated and what isn’t? Can anyone point me in the right direction for this kind of functionality?

How to&Answers:

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..

Answer:

Despite Google has provided new API for Places SDK, they are dropping support for Place Picker because of new billing policy, and there are two options how to handle this.

  1. You can implement this logic by your self using new SDK.

  2. Or luckily there is this Ping Place Picker library which mimics
    the behavior of old Place Picker.

Answer:

As an alternative to Google Place Picker, you could use Leku. It’s an easy to use library that would fit your needs.

You only need to add the dependency:

dependencies {
    implementation 'com.schibstedspain.android:leku:7.0.0'
}

Declare the activity on your manifest:

<activity
    android:name="com.schibstedspain.leku.LocationPickerActivity"
    android:label="@string/leku_title_activity_location_picker"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    android:windowSoftInputMode="adjustPan"
    android:parentActivityName=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    <meta-data android:name="android.app.searchable"
        android:resource="@xml/leku_searchable" />
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".MainActivity" />
</activity>

Start the activity where you need it:

val locationPickerIntent = LocationPickerActivity.Builder()
    .build(applicationContext)

startActivityForResult(locationPickerIntent, MAP_BUTTON_REQUEST_CODE)

And finally, process the results:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (resultCode == Activity.RESULT_OK && data != null) {
        Log.d("RESULT****", "OK")
        if (requestCode == 1) {
            val latitude = data.getDoubleExtra(LATITUDE, 0.0)
            Log.d("LATITUDE****", latitude.toString())
            val longitude = data.getDoubleExtra(LONGITUDE, 0.0)
            Log.d("LONGITUDE****", longitude.toString())
            val address = data.getStringExtra(LOCATION_ADDRESS)
            Log.d("ADDRESS****", address.toString())
            val postalcode = data.getStringExtra(ZIPCODE)
            Log.d("POSTALCODE****", postalcode.toString())
            val bundle = data.getBundleExtra(TRANSITION_BUNDLE)
            Log.d("BUNDLE TEXT****", bundle.getString("test"))
            val fullAddress = data.getParcelableExtra<Address>(ADDRESS)
            if (fullAddress != null) {
                Log.d("FULL ADDRESS****", fullAddress.toString())
            }
            val timeZoneId = data.getStringExtra(TIME_ZONE_ID)
            Log.d("TIME ZONE ID****", timeZoneId)
            val timeZoneDisplayName = data.getStringExtra(TIME_ZONE_DISPLAY_NAME)
            Log.d("TIME ZONE NAME****", timeZoneDisplayName)
        } else if (requestCode == 2) {
            val latitude = data.getDoubleExtra(LATITUDE, 0.0)
            Log.d("LATITUDE****", latitude.toString())
            val longitude = data.getDoubleExtra(LONGITUDE, 0.0)
            Log.d("LONGITUDE****", longitude.toString())
            val address = data.getStringExtra(LOCATION_ADDRESS)
            Log.d("ADDRESS****", address.toString())
            val lekuPoi = data.getParcelableExtra<LekuPoi>(LEKU_POI)
            Log.d("LekuPoi****", lekuPoi.toString())
        }
    }
    if (resultCode == Activity.RESULT_CANCELED) {
        Log.d("RESULT****", "CANCELLED")
    }
}

And you’re good to go. 😉

Answer:

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.

Migration guide to new places SDK client library can be found here

Or find custom implementation in my repo => NewGooglePlacesSDK