Last active
April 13, 2016 09:30
-
-
Save Fitoussi/78f69d655131731e76d985e9a4272bc3 to your computer and use it in GitHub Desktop.
Apply Google Places Address Autocomplete to an input field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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