var locationSet = 0; var addressLocationInit = false; var placeSearch, autocompleteAddress; 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' }; var autoCompleteAddressListener = null; var autocompleteAddressInputTimeout = null; function initAutocomplete() { if (addressLocationInit) { return false; } addressLocationInit = true; // Create the autocompleteAddress object, restricting the search to geographical // location types. autocompleteAddress = new google.maps.places.Autocomplete( /** @type {!HTMLInputElement} */(document.getElementById('autocompleteAddress')), { types: ['geocode'], fields: ["geometry.location", 'address_components'] }); // When the user selects an address from the dropdown, populate the address // fields in the form. autoCompleteAddressListener = autocompleteAddress.addListener('place_changed', fillInAddress); setTimeout("google.maps.event.trigger($('#autocompleteAddress').get(0), 'keydown', { keyCode:25 });", 500); } // [START region_fillform] function fillInAddress(data) { // Get the place details from the autocompleteAddress object. console.info('autocompleteAddress', autocompleteAddress); var place = autocompleteAddress.getPlace(); console.info('place', place); console.info('data', data); $('#contactFormFieldset #address').show(); $('#shippingForm #address').show(); $('#shippingAddressForm #address').show(); $('#newShippingAddressForm #address').show(); $('.editShippingTable').show(); combineAddress(); setTimeout(function(){ combineAddress(); }, 100); for (var component in componentForm) { document.getElementById(component).value = ''; document.getElementById(component).disabled = false; } if (!place || !place.address_components) { return; } // Get each component of the address from the place details // and fill 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; if (addressType=='administrative_area_level_1') { if ($(document.getElementById(addressType)).find('option[value="'+val+'"]').length==0) { // console.info(''); $(document.getElementById(addressType)).append(''); $(document.getElementById(addressType)).val(val); } } } } $('#country').change(); } // [END region_fillform] // [START region_geolocation] // Bias the autocompleteAddress object to the user's geographical location, // as supplied by the browser's 'navigator.geolocation' object. function geolocate() { if (typeof isSecureContext == 'undefined' || !isSecureContext) { return false; } if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(success,error,{enableHighAccuracy:true, timeout:10000}); // This loads the location once. locationSet = 1; } } function success(position) { var geolocation = { lat: position.coords.latitude, lng: position.coords.longitude }; var circle = new google.maps.Circle({ center: geolocation, radius: position.coords.accuracy }); initAutocomplete(); if (autocompleteAddress) { // Only call setBounds if autocompleteAddress is defined autocompleteAddress.setBounds(circle.getBounds()); } } function error() { geolocation = null; } // [END region_geolocation] function combineAddress(){ $('#contactFormFieldset #address1').val( $('#street_number').val() + " " + $('#route').val()); $('#shippingForm #address1').val( $('#street_number').val() + " " + $('#route').val()); $('#shippingAddressForm #address1').val( $('#street_number').val() + " " + $('#route').val()); $('#newShippingAddressForm #address1').val( $('#street_number').val() + " " + $('#route').val()); // $('#address1').val( $('#street_number').val() + " " + $('#route').val()); } $(document).ready(function() { if(locationSet === 0){ geolocate(); } $('#autocompleteAddress').on('keyup paste click', function(){ if(locationSet === 0){ geolocate(); } }); // This example displays an address form, using the autocompleteAddress feature // of the Google Places API to help users fill in the information. $('#street_number, #route').on('change keyup paste click', function(){ combineAddress(); }); // initAutocomplete(); $('#autocompleteAddress').keyup(function() { if (autocompleteAddressInputTimeout !== null) { clearTimeout(autocompleteAddressInputTimeout); } autocompleteAddressInputTimeout = setTimeout('initAutocomplete()', 300); }); })