Search code examples
google-mapsgoogle-places-apigoogle-places-autocomplete

Set componentRestrictions with Google Place Autocomplete API


I'm making a custom autocomplete field and my goal is to fetch cities from only one country.

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=amsterdam&inputtype=textquery&types=(cities)&fields=geometry,name&language=nl&key=APIKEY

I've read about the componentRestrictions to bound a specific country. How do I include the param inside the request?

Adding param &componentRestrictions=nl doesn't work.


Solution

  • Per the documentation for the web service the parameter is components:

    • components — A grouping of places to which you would like to restrict your results. Currently, you can use components to filter by up to 5 countries. Countries must be passed as a two character, ISO 3166-1 Alpha-2 compatible country code. For example: components=country:fr would restrict your results to places within France. Multiple countries must be passed as multiple country:XX filters, with the pipe character (|) as a separator. For example: components=country:us|country:pr|country:vi|country:gu|country:mp would restrict your results to places within the United States and its unincorporated organized territories.

    Example: components=country:nl

    Full URL:

    https://maps.googleapis.com/maps/api/place/autocomplete/json?input=amsterdam&inputtype=textquery&types=(cities)&fields=geometry,name&language=nl&components=country:nl&key=APIKEY