Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Places for Android API deprecated alternative

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?

like image 324
TomCB Avatar asked Feb 11 '19 19:02

TomCB


People also ask

Can I use Places API for free?

Places API is not free, however, once you set up your billing account, you will be entitled for a one time $300 free credit(usable for Google Cloud Platform products) and a monthly recurring $200 free credit(exclusive for Google Maps Platform products), after consuming the credits, you will receive an OVER_QUERY_LIMIT ...

Is Googleapis deprecated?

Content Security Policy update (March 21, 2022 - May 2023)Support for websites using Content Security Policy (CSP) that do not specify googleapis.com in the Maps JavaScript API is deprecated as of v3. 49, and will be unsupported beginning with v3.

What is Place_id?

A place ID is a textual identifier that uniquely identifies a place. The length of the identifier may vary (there is no maximum length for Place IDs). Examples: ChIJgUbEo8cfqokR5lP9_Wh_DaM. GhIJQWDl0CIeQUARxks3icF8U8A.


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

like image 134
Navin Kumar Avatar answered Oct 07 '22 20:10

Navin Kumar


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

like image 20
Ferran Pons Avatar answered Oct 07 '22 19:10

Ferran Pons


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.

like image 32
Roman Nazarevych Avatar answered Oct 07 '22 19:10

Roman Nazarevych