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

Not long ago I've written about how to use Google's Map AutoComplete API. Now I've been going through my code and refactoring it. The new method is more flexible and easier to use.

My changes rely on jQuery being part of your project.
googlautocmplt.png


This is what the initialisation code looks like now. Note that it does not search for any specific input element ID, instead it searches for all input elements that have the CSS class 'loc_autocomplete' and sets them up as an auto-completable field.
 JavaScript
$(document).ready(function() {
if (typeof(google) != 'undefined') {
var options = {
types: ['(cities)']
};
$('input.loc_autocomplete').each(function(idx, el) {
new google.maps.places.Autocomplete(el, options);
});
}
});


In my HTML, I do something like this...
 HTML
<input id="locationName" name="locationName" type="text" class="loc_autocomplete">




Any input element with the same CSS class will be initialised now.

-i

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.