Skip to content

Instantly share code, notes, and snippets.

@Fitoussi
Last active April 13, 2016 09:30
Show Gist options
  • Select an option

  • Save Fitoussi/78f69d655131731e76d985e9a4272bc3 to your computer and use it in GitHub Desktop.

Select an option

Save Fitoussi/78f69d655131731e76d985e9a4272bc3 to your computer and use it in GitHub Desktop.
Apply Google Places Address Autocomplete to an input field
function gmw_google_address_autocomplete() {
?>
<script>
jQuery(document).ready(function($) {
//Array of input fields ID.
var gacFields = ["gmw-address-1","gmw-address-7"];
$.each( gacFields, function( key, field ) {
var input = document.getElementById(field);
console.log(input);
//varify the field
if ( input != null ) {
//basic options of Google places API.
//see this page https://developers.google.com/maps/documentation/javascript/places-autocomplete
//for other avaliable options
var options = {
types: ['geocode'],
};
var autocomplete = new google.maps.places.Autocomplete(input, options);
google.maps.event.addListener(autocomplete, 'place_changed', function(e) {
var place = autocomplete.getPlace();
if (!place.geometry) {
return;
}
});
}
});
});
</script>
<?php
}
add_action( 'wp_footer','gmw_google_address_autocomplete' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment