Limit Google Autocomplete results to country and city
To add a location based autocomplete to text inputs using Google Autocomplete, you need to add the following code to a script block or separate JavaScript file. The country is limited to two letter country codes only, e.g. uk, us or fr, see a full list here:
https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
function initialize() { var optionsT = { types: ['geocode'], componentRestrictions: {country: "uk"} }; var autocompleteT = new google.maps.places.Autocomplete(document.getElementById('TarPlacename'), optionsT); }
The above script will limit the results to anywhere in the UK, you can also limit it to just cities by adding the following to the types array.
'(cities)'
Published at 8 Feb 2016, 15:30 PM
Tags: JavaScript,Google Maps
Comments
gh
gff
26/02/2018
hamid
hello, how are you?
please help me to limited result in city ,
for example : search all address in sydney in australia
What changes should I make in the above instructions?
I am sorry to be a beginner please give me a detailed guide
Thankful
HamiD
19/03/2018
ttt asd
hi, where are you
20/03/2018