AnyHelpPls
AnyHelpPls

Reputation: 11

Android Map to search only within a city

I'm currently making an android app that has a map in it. I have successfully added the map and can search places. From what I have searched online, I can set my city and limit my search only to my country.

Does anyone know how to limit my search only within a city?

Upvotes: 0

Views: 1133

Answers (2)

PetrM
PetrM

Reputation: 21

it's quite tricky solution, but you can do it by following steps:

Step1: Google places allows you only biasing search results from specified LatLngBounds (in your case city bounds), but search engine may still return Place outside given bounds. See Restrict autocomplete results for more details. I think this is sufficient, but if you need strict restriction, follow step2.

Step2: With all search results from Step1 call Places.GeoDataApi.getPlaceById to get detail info about each Place. You'll get among other things LatLng of each place. Then you can check if each Place lies within given bounds: bounds.contains(LatLng) and if not, remove it manually from search results.

HOWEVER: Step2 solution multiplies number of requests on PlacesAPI and you should think twice if you really need this strict restriction!

UPDATE

As of April 2018 Google added possibility to specify how to treat the bounds in autocomplete predictions. Now you can use the getAutocompletePredictions() method of GeoDataClient class with boundsMode parameter.

See https://stackoverflow.com/a/50134855/5140781 for more details.

Upvotes: 1

AnyHelpPls
AnyHelpPls

Reputation: 11

I have found the solution. This though is set to my city. To anyone who would like to use this, just change the long and lat. sb.append("&types=establishment&strictbounds&location=9.30684,123.305447&radius=2000");

Upvotes: 0

Related Questions