Skip to content

Instantly share code, notes, and snippets.

@Razenbull
Last active May 9, 2024 13:42
Show Gist options
  • Save Razenbull/a62527cedba864e24f08 to your computer and use it in GitHub Desktop.
Save Razenbull/a62527cedba864e24f08 to your computer and use it in GitHub Desktop.
google place autocomplete && select first option on enter if !$(".pac-item-selected")
var $addressInput = $('#locationAddressInput');
var setKeyDownListener = selectFirstOptionOnEnter($addressInput[0]);
window.autocomplete = new google.maps.places.Autocomplete($addressInput[0], {
type: ['geocode'],
componentRestrictions: {country: 'be'}
});
google.maps.event.addListener(window.autocomplete, 'place_changed', function () {
var address = window.autocomplete.getPlace();
if (typeof address === 'object') {
address = addressEval(address);
$.ajax({
url: '/fr-be/account/saveAddress',
type: "POST",
data: {place: address, requirePrecise: false},
dataType: "json",
success: function (result) {
if (result.success) {
$addressInput.val('');
// Set input and hidden field if success
for (var i in result.addresses) {
var a = result.addresses[i];
if (a.active) {
$zip.text(a.zipCode);
$lat.val(a.lat);
$lng.val(a.lng).change();
$addressInput.val(a.formatted);
$('#popups-frame').trigger('click');
break;
}
}
}
},
error: function () {
$addressInput.val('');
}
});
}
});
function selectFirstPredictionOnEnter(input) {
// store the original event binding function
var _addEventListener = (input.addEventListener) ? input.addEventListener : input.attachEvent;
function addEventListenerWrapper(type, listener) {
// Simulate a 'down arrow' keypress on hitting 'return' when no pac suggestion is selected,
// and then trigger the original listener.
if(type == 'keydown') {
var orig_listener = listener;
listener = function(event) {
var suggestion_selected = $(".pac-item-selected").length > 0;
if (event.which == 13 && !suggestion_selected) {
var simulated_downarrow = $.Event("keydown", {
keyCode: 40,
which: 40
});
orig_listener.apply(input, [simulated_downarrow]);
}
orig_listener.apply(input, [event]);
};
}
_addEventListener.apply(input, [type, listener]);
}
if (input.addEventListener)
input.addEventListener = addEventListenerWrapper;
else if (input.attachEvent)
input.attachEvent = addEventListenerWrapper;
}
/**
* Receives an PlaceResult object, and checks if it's a precise address.
* Used to prepare a google's address to be saved in ETY.
*
* @param place
* @returns {*}
*/
function addressEval(place) {
console.log('helpers > addressEval',place);
if (!place.geometry) {
return;
}
var cpt = 0;
var dataPlace = {};
$.each(place.address_components, function (i, e) {
$.each(e.types, function (j, type) {
switch (type) {
case 'street_number':
cpt++;
dataPlace.street_number = e.long_name;
break;
case 'route':
cpt++;
dataPlace.route = e.long_name;
break;
case 'country':
cpt++;
dataPlace.country = e.long_name;
break;
case 'locality':
cpt++;
dataPlace.locality = e.long_name;
break;
case 'postal_code':
dataPlace.postal_code = e.long_name;
cpt++;
break;
}
});
});
dataPlace.formatted_address = place.formatted_address;
dataPlace.lat = place.geometry.location.lat();
dataPlace.lng = place.geometry.location.lng();
dataPlace.isPrecise = (cpt >= 5);
return dataPlace;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment