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 DavidassuG on MVC forms in Umbraco
12 Jul 2018, 10:54 AM
By Luke on Setting up tables in SQLite with a primary key on a Xamarin Forms app
11 Jul 2018, 09:18 AM
By Travis on Setting up tables in SQLite with a primary key on a Xamarin Forms app
7 Jul 2018, 15:44 PM
By Luke on Auto/Custom height on Xamarin Forms WebView for Android and iOS
15 Jun 2018, 09:43 AM
By vikki on Auto/Custom height on Xamarin Forms WebView for Android and iOS
14 Jun 2018, 13:35 PM
By jimbo on JQuery Unobtrusive Validation for dynamically created elements
13 Jun 2018, 17:13 PM
By ScottKem on MVC forms in Umbraco
8 Jun 2018, 10:29 AM
By Alaa on java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
3 Jun 2018, 20:16 PM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development