Limit Android Google Places API to search only giv

2019-03-03 09:49发布

问题:

I`m using Android Google Places API to autocomplete streets and addresses. The problem is that it gives all streets from a whole country. Of course I added bounds to limit place for search, but it doesnt work correctly - it gives only priority, so in other words best results will be higher in list, nothing more

So code:

       AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
                .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
                .setCountry("RU")
                .build();

        Intent intent =
                new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
                        .zzih(searchString) //that is for passing search string from toolbar
                        .setFilter(typeFilter)
                        .setBoundsBias(city.getBounds())
                        .build(this);

In short the problem is: When I type in search something like "Lenina Street" I see a lot of useless results out of bounds set in .setBoundsBias(city.getBounds()). Just imagine that something like "Lenina Street" exists in almost every locality!

How can I fix the problem and limit search results?

P.S.

  1. I know I can use Google Places Web API or by GeoDataApi.getAutocompletePredictions() and filter results manually, but that means I have to write UI manually too, what I dont want to do. Thats even worse than I thought. Even if I get results from Web API or through GeoDataApi I have only predictions which doesnt contain coordinates, only placeId. So if I want to filter predictions by coordinates I have to do request for each placeId. In other words if I got 20 places I will have to do 20 more requests to find out coordinates.
  2. Also I can add city name in searchString, that makes results better (but not at all) but it makes writing of address unclear and city name takes place, so its not good solution too.

回答1:

I'm afraid Places API for Android doesn't support strict bounds yet. There is a feature request in Google Issue tracker to implement this:

https://issuetracker.google.com/issues/38188994

Feel free to star this feature request to add your vote and subscribe to notifications from Google.

In the meantime the workaround might be using Places API web service that supports strict bounds and implement the UI manually.

UPDATE

The feature request was marked as Fixed by Google. Have a look at https://stackoverflow.com/a/50134855/5140781 that shows how to apply strict bounds in Places API for Android.