Igor's Blog
Programming, DIY, Games, Hacks, and Tech

Here's a bit of code that I'm using on a personal project of mine. This code uses the Google Maps Places API auto complete feature without having to display a map. This means you get a text input box where you can start typing in Google Maps locations and fit it into your standard HTML forms.

There is a full places auto complete example on Google's pages but it only shows how to do it with a map present.

With my bit of code you turn this...
googlemaps1.png


Into this...
googlemaps2.png




So lets look at some code.

First I load the JavaScript for the API. This is documented here.
 HTML
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>


Then I add an input text field that looks something like this...
 HTML Form Input
<input id="locationName" name="locationName" type="text">


Then I add a bit of JavaScript that initialises the API and adds a listener to the input text box from above.
 JavaScript
function init() {
var input = document.getElementById('locationName');
var autocomplete = new google.maps.places.Autocomplete(input);
}
google.maps.event.addDomListener(window, 'load', init);



That's pretty much it! The input text box will now allow you to start entering locations and will attempt to auto complete them for you.

However, that is probably not that useful, so in my case I also add filtering to make sure that only 'cities' are displayed as valid auto complete options. The JavaScript to do that is below.
 JavaScript
function init() {
var options = {
types: ['(cities)']
};
var input = document.getElementById('locationName');
var autocomplete = new google.maps.places.Autocomplete(input, options);
}
google.maps.event.addDomListener(window, 'load', init);


-i

Please leave your comments or feedback below!
comments powered by Disqus
Other posts you may like...

Recent Blog Posts

How to enable the full stack trace in Maven's Surefire plugin for JUnit testing

Twelve elements of the Burst Mining Pool interface explained

TPG FTTB settings for the Billion BiPAC 8700AXL 1600 modem router

Protecting old Atari Lynx game boxes with snug fit plastic sleeves

How to fix SoapUI javax.net.ssl.SSLHandshakeException calling WebLogic 12.2 web services on Java 8

Woolworths (WOW) shares disappeared from Computer Share Investor Centre

Connecting the Dell UltraSharp U3415W monitor to a MacBookPro via USB-C

How to add/change PHP versions appearing in MAMP preferences

Fix the ORA-00904: ORA_ROWSCN: invalid identifier error in SQLDeveloper with a few easy steps

G Suite Gmail is broken on Safari due to new Google Content Security Policy settings

Recent Galleries

Protecting old Atari Lynx game boxes with snug fit plastic sleeves

Monument Valley 2 is released and does not disappoint

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

DIY spare parts computer build with a RAIDMAX Anura case

Fake 'Lepin' brand Lego packaging

Hardwood garden bench with clear resin void filler

Fixing a 3D printer extruder that stopped heating up

Easily increase disk space in a Lenovo Ideapad 100S 14" laptop with an M.2 SSD

Making a multi-piece 3D printed solder spool holder stand

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
300 George St Blogumentary

My Online Tools

UUID to OID Converter
Guru JSON-RPC Tester
Extrudifier Object Designer
Travel ┬ÁBlog

Blogs and Friends

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Don't forget to
my Facebook page for more great articles!
Don't show this again