How to toggle the google-maps autocomplete on and off?
The solution of Dr.Molle doesn't work for me.
The solution I found is to clearListener and remove pac-container:
var autocomplete;
var autocompleteListener;
function disableGoogleAutocomplete() {
if (autocomplete !== undefined) {
google.maps.event.removeListener(autocompleteListener);
google.maps.event.clearInstanceListeners(autocomplete);
$(".pac-container").remove();
console.log('disable autocomplete to GOOGLE');
}
}
function enableGoogleAutocomplete() {
autocomplete = new google.maps.places.Autocomplete($("#inputDiv input")[0], options);
autocompleteListener = google.maps.event.addListener(autocomplete, 'place_changed', function() { ... }
console.log('set autocomplete to GOOGLE');
}
Now I can switch on/off google places Autocomplete.
To remove all listeners added to autocomplete.
autocomplete.unbindAll();
To remove all listeners added to input element.
google.maps.event.clearInstanceListeners(input);
See https://code.google.com/p/gmaps-api-issues/issues/detail?id=3429
There are two elements related to the autocomplete
- the input-element
- a div that contains the list-entries. this div will be appended to the body and has the class "pac-container"
So what you can do: show or hide both elements by modifying their display-style.
When it's not possible to hide the input you may replace the input with a clone of the input, this will remove the autocomplete-functionality.
inputObject.parentNode.replaceChild(inputObject.cloneNode(true),input);
To restore the autocomplete do again what you want to do.