google-mapsgoogle-maps-api-3google-places-apiappcelerator-titanium

Restricting Google Places Autocomplete API to specific countries


I am implementing a Titanium based mobile application in which I want to show address autocomplete suggestions. I am able to get the autocomplete suggestion in my app with current position and radius but I want to restrict those results to specific country or countries.

I am creating a URL by appending parameters which looks like this:

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=110 Ward&location=40.7128,-74.0059&radius=500&types=address&key=XXXXXXXXXXXXXXX

In the URL location are coordinates (center), radius is distance from coordinates (center) and input is the text upon which autocomplete suggestions are shown.

So how this works is: 1. If there is some address present in specified radius from current position it will be shown on the top of suggested address. 2. If there is no address present in provided radius but address are available outside the radius, those address will be shown because I am not using strictBounds property of API.

What I want is I if address based on user input are not available in specified radius it should show matching addresses outside the radius but NOT outside the specified country.

How can I append the specific country or countries in this URL.

Thanks in advance.


Solution

  • You can set a country filter using the components parameter as specified in the documentation.

    You can set up to 5 countries in components filter. For example, to show results from the USA and Canada:

    https://maps.googleapis.com/maps/api/place/autocomplete/json?input=110%20Ward&location=40.7128%2C-74.0059&radius=500&types=address&components=country%3ACA%7Ccountry%3AUS&key=YOUR_API_KEY