Created
March 21, 2018 19:52
-
-
Save vstorm83/89cef3844b9c95985d5a57bc149a68b8 to your computer and use it in GitHub Desktop.
google place api autocomplete
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
//Add JS google place api | |
//wp_enqueue_script('google-map-js', 'https://maps.googleapis.com/maps/api/js?key=your_key&libraries=places', null, '1.0', true); | |
/** google map place api */ | |
var placeSearch, autocomplete; | |
var componentForm = { | |
street_number: 'short_name', | |
route: 'long_name', | |
locality: 'long_name', | |
administrative_area_level_1: 'short_name', | |
country: 'long_name', | |
postal_code: 'short_name' | |
}; | |
function initAutocomplete() { | |
// Create the autocomplete object, restricting the search to geographical | |
// location types. | |
var $address = $('[id^=field_delivery_street_address]'); | |
if ($address.length === 0) { | |
return; | |
} | |
var brisbaneBounds = new google.maps.LatLngBounds( | |
new google.maps.LatLng(-27.844153, 152.688982), | |
new google.maps.LatLng(-27.074443, 153.177607)); | |
autocomplete = new google.maps.places.Autocomplete($address[0],{ | |
bounds: brisbaneBounds, | |
strictBounds: true | |
}); | |
autocomplete.setTypes(['address']); | |
autocomplete.setComponentRestrictions({'country': ['aus']}); | |
// When the user selects an address from the dropdown, populate the address | |
// fields in the form. | |
autocomplete.addListener('place_changed', fillInAddress); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment