Google Places AutocompleteService filtering by country

jeznag picture jeznag · Feb 2, 2013 · Viewed 26.8k times · Source

I'm setting up a custom autocomplete field where I show locations from Google Places and events from my database that match the search query. For this reason, I'm using the Google Places Autocomplete Service to get the query predictions rather than plugging in the Places Autocomplete directly into my textfield.

The problem is I can't figure out how to filter Places Autocomplete suggestions by country using the AutoComplete service.

I've tried:

var service = new google.maps.places.AutocompleteService(null, {
        types: ['cities'],
        componentRestrictions: {country: "au"}
    });

but it still shows autocomplete options from Germany and France:(

Any suggestions?

Answer

Robert Dodd picture Robert Dodd · Mar 9, 2013

You need to pass the restrictions when you call the service, not when you create it. Here:

//create service
service = new google.maps.places.AutocompleteService();

//perform request. limit results to Australia
var request = {
    input: 'Brisbane',
    componentRestrictions: {country: 'au'},
};
service.getPlacePredictions(request, callback);