How to set Location Bias in the new places Autocomplete for Android?

Viewed 3081

I'm migrating to the new New Places SDK Client in my android app. I'm trying not to use the compatibility library, but rather use all the new objects. The only feature I use from the SDK is the builtin Autocomplete activity.

I managed to create the Autocomplete.IntentBuilder and I see that it has a setLocationBias() method just like the previous PlaceAutocomplete.IntentBuilder.

However this method takes as a parameter an interface LocationBias that only extends Parcelable and has no members of its own so I'm not sure how to implement it, or how to mention the location that I need in it.

Any help is appreciated.

2 Answers

LocationBias takes RectangularBounds. you need to create two LatLng objects with north east & south west coordinates for the bounds and instantiate the RectangularBounds object with them:

val northEast = LatLng(double, double)
val southWest = LatLng(double, double)

and pass it to:

RectangularBounds.newInstance(southWest, northEast);

I can't comment since my reputation is still under 50.

Just wanted to address a correction of the voted answer as RectangularBounds takes southWest argument first then the northEast argument, as stated here.

So the right way would be

RectangularBounds.newInstance(southWest, northEast);

Doing the opposite would result in an exception error.

Related