Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

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...
Hi! You can search my blog here ⤵
Or browse the recent top tags...

Recent Blog Posts

How to remove caked on stickers from old game carts

How to pass parameters to your PHP script via the command line

jPhotoFrame new layout engine explained with examples

Atari Lynx Multi SD Card cartridge review

Jersey JAX-RS filters and interceptors execution order when throwing Exceptions

Why I'll never renew a domain with GoDaddy again

Fake AirPods - HBQ-i7 TWS wireless headset teardown

How to fix Postman error - Failed to import data: format not recognized

Switching out macOS Mail for Microsoft Outlook

Using DeoxIT to repair old game catridges

Recent Galleries

BMB-012 Nanoblock T-Rex Skeleton Model assembly

Tiny Arcade revision 6 kit assembly and decal application

Atari Lynx repair - Part 5 - McWill LED screen mod installation

Atari Lynx repair - Part 4 - screen cover replacement

Atari Lynx repair - Part 2 - re-capping the motherboard

Atari Lynx repair - Part 3 - broken speaker replacement

Atari Lynx repair - Part 1 - introduction and case disassembly

Building a custom Atari Lynx game box storage shelf unit in a day

Protecting old Atari Lynx game boxes with snug fit plastic sleeves

Monument Valley 2 is released and does not disappoint

Blogs and Friends

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

Blog Activity

Blog Activity