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 annualmars on Raspberry Pi - Running Java app on Raspbian
10 May 2019, 15:48 PM
By Shahriar H. Razi on Xamarin Forms - Working with threads
7 May 2019, 09:25 AM
By Person on MVC forms in Umbraco
30 Apr 2019, 13:33 PM
By christopher on How to add a Xamarin Forms Loading Screen/Overlay
27 Mar 2019, 11:59 AM
By Rezi on Xamarin forms - Stop/Cancel back button event
22 Mar 2019, 13:08 PM
By jack of ass on Replacing the Xamarin Header/Navigation bar with a custom view/template
16 Feb 2019, 03:13 AM
By Zal on Xamarin MasterDetailPage has large margin/padding at top
7 Feb 2019, 12:20 PM
By Ramachandran on Reducing Umbraco memory usage by enabling 32-bit applications
4 Feb 2019, 17:39 PM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development