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?
Use this working Code
As outlined in the documentation reference, the Places Library
AutocompleteService
does not support AutocompleteOptions. If you think that this would be a valuable feature, please file a Places API - Feature Request.You need to pass the restrictions when you call the service, not when you create it. Here:
You should use this code.
You can specify types, and componentRestrictions using AutocompletionRequest. Here is an example -