Created
March 5, 2020 22:28
-
-
Save csaborio001/8969fd71cd0dfd4fd3f9d5b1e9bdf83c to your computer and use it in GitHub Desktop.
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
var placeSearch, autocomplete; | |
var componentForm = { | |
postal_code: 'short_name' | |
}; | |
jQuery(document).ready(function($){ | |
$(document).on('can_embed_loaded', function() { | |
$('#postcode').prop('id', 'postal_code'); | |
$('#form-street').prop('id', 'autocomplete'); | |
$('#postal_code').prop('disabled', 'true'); | |
}); | |
}); | |
// Bias the autocomplete object to the user's geographical location, | |
// as supplied by the browser's 'navigator.geolocation' object. | |
function geolocate() { | |
if (navigator.geolocation) { | |
navigator.geolocation.getCurrentPosition(function(position) { | |
var geolocation = { | |
lat: position.coords.latitude, | |
lng: position.coords.longitude | |
}; | |
var circle = new google.maps.Circle( | |
{center: geolocation, radius: position.coords.accuracy}); | |
autocomplete.setBounds(circle.getBounds()); | |
}); | |
} | |
} | |
function initAutocomplete() { | |
// Create the autocomplete object, restricting the search predictions to | |
// geographical location types. | |
console.log(document.getElementById('site-logo')); | |
console.log(document.getElementById('autocomplete')); | |
autocomplete = new google.maps.places.Autocomplete( | |
document.getElementById('autocomplete'), {types: ['geocode']}); | |
// Avoid paying for data that you don't need by restricting the set of | |
// place fields that are returned to just the address components. | |
autocomplete.setFields(['address_component']); | |
// When the user selects an address from the drop-down, populate the | |
// address fields in the form. | |
autocomplete.addListener('place_changed', fillInAddress); | |
} | |
function fillInAddress() { | |
// Get the place details from the autocomplete object. | |
var place = autocomplete.getPlace(); | |
for (var component in componentForm) { | |
document.getElementById(component).value = ''; | |
document.getElementById(component).disabled = false; | |
} | |
// Get each component of the address from the place details, | |
// and then fill-in the corresponding field on the form. | |
for (var i = 0; i < place.address_components.length; i++) { | |
var addressType = place.address_components[i].types[0]; | |
if (componentForm[addressType]) { | |
var val = place.address_components[i][componentForm[addressType]]; | |
document.getElementById(addressType).value = val; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment