We have
autocomplete.addListener('place_changed', function(){});
But I want to listen to on input. input.addEventListener('input', function(){ ... });
doesn’t work. I want something like place_changed_input.
function initMap()
{
input = document.getElementById('region-input');
const options = {};
autocomplete = new google.maps.places.Autocomplete(input, options);
input.addEventListener('input', function()
{
const value = input.value;
// Check if the current value matches a valid prediction
const validPrediction = autocomplete.getPredictions().some(prediction => prediction.description === value);
if (!validPrediction) {
// No valid prediction found, add custom option
const customOption = new google.maps.places.AutocompletePlace({
name: value,
});
// Get the existing predictions and add your custom option at the beginning
const predictions = autocomplete.getPredictions();
predictions.unshift(customOption);
// Update the autocomplete predictions with the modified array
autocomplete.setPredictions(predictions);
}
});
}