select first option from google autocomplete for location input

This commit is contained in:
Bilal Catic
2019-10-13 22:47:07 +02:00
parent 0a7aaec4c6
commit 105ed47276

View File

@@ -99,6 +99,7 @@
autocomplete = new google.maps.places.Autocomplete(inputElement, initialAutocompleteParams);
autocomplete.bindTo('bounds', map);
autocomplete.addListener('place_changed', onPlaceChanged);
pacSelectFirst(inputElement);
}
function onPlaceChanged() {
@@ -110,6 +111,38 @@
}
}
function pacSelectFirst(input) {
// store the original event binding function
const _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') {
const originalListener = listener
listener = function (event) {
const suggestionSelected = $('.pac-item-selected').length > 0
if (event.key == 'Enter' && !suggestionSelected) {
const simulatedDownArrow = $.Event('keydown', {
keyCode: 40,
which: 40
})
originalListener.apply(input, [simulatedDownArrow])
}
originalListener.apply(input, [event])
}
}
_addEventListener.apply(input, [type, listener])
}
input.addEventListener = addEventListenerWrapper
input.attachEvent = addEventListenerWrapper
}
$(document).ready(() => {
$("#submit").click(() => {
const mapBounds = map.getBounds();