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)'

By Luke Alderton at 8 Feb 2016, 15:30 PM

Tags: JavaScript,Google Maps

Comments

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 dipbleds on Raspberry Pi - Bluetooth using Bluecove on Raspbian
12 Dec 2017, 21:29 PM
By Ich on Replacing the Xamarin Header/Navigation bar with a custom view/template
26 Nov 2017, 17:11 PM
By Ravi Motha on My experience at Umbraco UK Festival 2017
12 Dec 2017, 08:39 AM
By Borges on How to add a Xamarin Forms Loading Screen/Overlay
7 Nov 2017, 19:11 PM
By Rutul Mehta on Auto/Custom height on Xamarin Forms WebView for Android and iOS
4 Nov 2017, 03:15 AM
By Pablo on Xamarin MasterDetailPage has large margin/padding at top
26 Oct 2017, 15:31 PM
By faiza on Using MaryTTS or OpenMary in Java
22 Oct 2017, 12:54 PM
By Nietoperz on Using MaryTTS or OpenMary in Java
29 Sep 2017, 14:34 PM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development