I’m working on an Android app in which I want the user to search for data (that my app is providing) in a specific city. For that I have a search field in which the user can type in a city name. I use the Google Places API to retrieve autocomplete results like this:
PendingResult results =
Places.GeoDataApi
.getAutocompletePredictions(mGoogleApiClient, searchQuery,
mBounds, mPlaceFilter);
In my case mBounds represents the bounds of Germany, because I’m only interested in cities in Germany and I don’t want cities in other countries to show up in the autocomplete predictions. Now, obviously Germany isn’t a perfect square, so mBounds contains the whole of Germany, but also parts of surrounding countries like Switzerland. So the questions is: How can I remove all autocomplete predictions for places that are not in Germany?
My first thought was to use a place filter to only get predictions for one country in the first place. According to the documentation this can indeed be used to filter by regions (e.g. by countries), but unfortunately this specific filtering is not supported in Google Places API for Android (according to the documentation).
The (regions) type collection is not supported in Google Places API for Android.
There also doesn’t seem to be any possibility to get the country of a Place from either the AutocompletePrediction or the Place object. So I can’t even do the filtering myself.
The only solutions I can think of are the following:
Get the Place from the prediction and use the Geocoder API to retrieve the Address and from that get the country and do the filtering. However, that’s an additional network call (and another one against my quota) that seems totally unnecessary as it seems to me that the AutocompletePrediction object or at least the Place objects should know in which country a place is. After all, it’s part of the description / toString results of those objects. However, that’s just a string like “Munich, Germany”.
Use the description string mentioned in 1. to determine the country. However that seems like an even worse solution than 1, since the description is localized depending on the user’s locale which could be everything. So just looking for “Germany” wouldn’t do the job.
I would appreciate a solution that’s better than what I have proposed so far. I can’t imagine that there isn’t a better way to do this. Any ideas?
I had same issue with no luck to find a way to filter on specific country so what i did as a stupid workaround is to filter the result if it contain the country name in my case "United Arab Emirates"
while (iterator.hasNext()) {
AutocompletePrediction prediction = iterator.next();
// Get the details of this prediction and copy it into a new PlaceAutocomplete object.
if(prediction.getDescription().contains("United Arab Emirates"))
resultList.add(new PlaceAutocomplete(prediction.getPlaceId(),
prediction.getDescription()));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With