Places for Android API deprecated alternative
Solution 1:
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..
Solution 2:
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.
You can implement this logic by your self using new SDK.
Or luckily there is this Ping Place Picker library which mimics the behavior of old Place Picker.
Solution 3:
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. ;)