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

Tags: JavaScript,Google Maps

Luke Alderton

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
Post a comment
Sit tight...
We're adding your comment.
Thank you.
Your comment has been added.
There's been a problem.
Please try again later.
Share with
Tags
Latest Comments
By AnubisMus on MVC forms in Umbraco
24 Sep 2018, 16:54 PM
By Richardcax on MVC forms in Umbraco
23 Sep 2018, 18:31 PM
By Normanambug on MVC forms in Umbraco
19 Sep 2018, 04:42 AM
By BabloPridi on MVC forms in Umbraco
17 Sep 2018, 09:23 AM
By hario on MVC forms in Umbraco
12 Sep 2018, 02:41 AM
By Jasonced on MVC forms in Umbraco
11 Sep 2018, 12:17 PM
By VadimProSEO on MVC forms in Umbraco
10 Sep 2018, 14:52 PM
By vennnile on MVC forms in Umbraco
8 Sep 2018, 05:49 AM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development