简体   繁体   English

如何打开和关闭 google-maps 自动完成功能?

[英]How to toggle the google-maps autocomplete on and off?

I'm instantiating an autocomplete input for Google Maps API (level 3), like so:我正在为 Google Maps API(级别 3)实例化一个自动完成输入,如下所示:

var input = document.getElementById('szukanyAdres');
autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);

How to turn the autocompletion functionality on and off at runtime?如何在运行时打开和关闭自动完成功能?

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请参阅https://code.google.com/p/gmaps-api-issues/issues/detail?id=3429

The solution of Dr.Molle doesn't work for me. Dr.Molle 的解决方案对我不起作用。

The solution I found is to clearListener and remove pac-container:我找到的解决方案是 clearListener 并删除 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.现在我可以打开/关闭谷歌地点自动完成功能。

There are two elements related to the autocomplete有两个元素与自动完成相关

  1. the input-element输入元素
  2. a div that contains the list-entries.一个包含列表条目的 div。 this div will be appended to the body and has the class "pac-container"此 div 将附加到正文并具有“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.要恢复自动完成,请再次执行您想要执行的操作。

If anything written here does not work for you, you can try to just hide it.如果此处写的任何内容对您不起作用,您可以尝试隐藏它。 The field with places appearing while typing is in css class .pac-container, so just declare it in css display: none.打字时出现位置的字段在 css 类 .pac-container 中,所以只需在 css display: none 中声明它。 Then it will not appear during typing in searchbox.然后它不会在搜索框中输入时出现。 Actually it is not the way to customize it.实际上这不是自定义它的方式。

您可以尝试使用 JQuery 隐藏它:

$(".pac-container").hide(); //hide autocomplete

I faced the problem as well (in my app I wanted to run the google's autocomplete only when user typed at least 4 digits into my input field - I am using my custom smart search when typed string has less than 4 digits).我也遇到了这个问题(在我的应用程序中,只有当用户在我的输入字段中输入至少 4 位数字时,我才想运行 google 的自动完成 - 当输入的字符串少于 4 位数字时,我正在使用我的自定义智能搜索)。

In my case above solutions didn't work either, so my workaround is:在我的情况下,上述解决方案也不起作用,所以我的解决方法是:

  1. Set opacity: 0 to the $('.pac-container') when you need.需要时将 opacity: 0 设置为 $('.pac-container') 。
  2. Optionally set also low z-index if needed.如果需要,也可以选择设置低 z-index。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM