Ashok Chandrasekaran
Ashok Chandrasekaran

Reputation: 31

Filter Google Places API results based on City

For one of my applications, I will let the users choose a City and then an Area. What I want to achieve is that based on the user's city selection, the Area field(which is using the autocomplete from Google Places) to display areas from that City. Eg: If user chooses the city as New York, the Area field should autocomplete only the areas from New York. Is this something which can be achieved?

Upvotes: 1

Views: 3640

Answers (2)

kazuri
kazuri

Reputation: 21

1] In autocomplete API, pass the "Lat,Long" in "location" parameter and "100000" in "radius" parameter. It will bias search result within 100Km in that city. Eg: Pass "40.7128,74.0059" for NewYork and it will give you result within 100Km in NewYork city.

OR

2] There is a trick you can use.If a user chooses a city, just add the city name as a prefix in the search string. It will only give the search suggestions in which the user is searching. eg, pass "NewYork" as a prefix in your search string, now type any word, it will only give you results for NewYork city restaurant, cafes, places, etc

Upvotes: 2

KENdi
KENdi

Reputation: 7741

You can do it by restricting the results of your autocomplete by a specified area.

Here are the ways that you can use:

  • Location Biasing - you may bias the results to a specified circle by passing a location and a radius parameter. This instructs the Place Autocomplete service to prefer showing results within that circle. Results outside of the defined area may still be displayed. You can use the components parameter to filter results to show only those places within a specified country.

  • Location Restrict - it can restrict the results to the region defined by location and a radius parameter, by adding the strictbounds parameter. This instructs the Place Autocomplete service to return only results within that region.

  • Places Types - you can restrict results from a Place Autocomplete request to be of a certain type by passing a types parameter. The parameter specifies a type or a type collection, as listed in the supported types below. If nothing is specified, all types are returned.

Hope this information helps you.

Upvotes: 1

Related Questions