Javascript 关闭谷歌地图 api v3 中所有打开的信息窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2966744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Closing any open info windows in google maps api v3
提问by hhj
As the title states, on a given event (for me this happens to be upon opening a new google.maps.InfoWindowI want to be able to close any other currently open info windows. Right now, I can open many at a time..but I want only 1 open at a time.
正如标题所述,在一个给定的事件中(对我来说,这恰好是在打开一个新google.maps.InfoWindow窗口时,我希望能够关闭任何其他当前打开的信息窗口。现在,我可以一次打开多个......但我只想一次打开1个。
I am creating the info windows dynamically (i.e. I don't know ahead of time how many will be generated), so in the click event of the current info window (which is where I want all the other open ones closed) I don't have a reference to any of the other open info windows on which to call close(). I am wondering how I can achieve this. I am not an experienced JavaScript programmer so I don't know if I need to use reflection or something similar here.
我正在动态创建信息窗口(即我不知道会提前生成多少个),所以在当前信息窗口的点击事件中(我希望所有其他打开的窗口关闭)我不没有对要调用的任何其他打开的信息窗口的引用close()。我想知道如何实现这一目标。我不是一个有经验的 JavaScript 程序员,所以我不知道我是否需要在这里使用反射或类似的东西。
Would the best way be just to save all the references in some sort of collection, then loop through the list closing them all?
最好的方法是将所有引用保存在某种集合中,然后遍历列表将它们全部关闭?
Thanks.
谢谢。
回答by jamesaharvey
I ran into the same problem and fixed it by creating a global info window.
我遇到了同样的问题并通过创建一个全局信息窗口来解决它。
var infowindow = new google.maps.InfoWindow();
Then I have a function to do the following on the click listener:
然后我有一个函数可以在点击监听器上执行以下操作:
function getInfoWindowEvent(marker) {
infowindow.close()
infowindow.setContent("This is where my HTML content goes.");
infowindow.open(map, marker);
}
This achieves what I think you're looking for b/c there is now only one info window on the map and I just close it, reload the content and open it again for the given marker.
这实现了我认为您正在寻找的 b/c 现在地图上只有一个信息窗口,我只需关闭它,重新加载内容并为给定的标记再次打开它。
回答by plexer
It should be sufficient to have a global infowindow and then to change the position and content of that infowindow.
拥有一个全局信息窗口然后更改该信息窗口的位置和内容应该就足够了。
var infowindow = new google.maps.InfoWindow();
// Call this function to open an infowindow i.e. on click.
function respondToClick(latlng) {
infowindow.setOptions({
position: latlng,
content" "Hello, world"
});
}
As the same infowindow is used each time, you are guarantee'd to only ever have one open, and it uses less resources / memory than creating and then destroying multiple infowindows.
由于每次都使用相同的信息窗口,因此您可以保证只打开一个,并且与创建然后销毁多个信息窗口相比,它使用的资源/内存更少。
回答by Camilo Cifuentes
infowindow is local variable and window is not available at time of close()
infowindow 是局部变量,并且 window 在 close() 时不可用
var latlng = new google.maps.LatLng(-34.397, 150.644); var infowindow
= null;
...
google.maps.event.addListener(marker, 'click', function() {
if (infowindow) {
infowindow.close();
}
infowindow = new google.maps.InfoWindow();
... });
...
回答by Drako
I have decided to create an array of all dynamically created Infobox. When you click on any first travel the array and then you close all open only one in which you have clicked.
我决定创建一个包含所有动态创建的信息框的数组。当您单击任何第一次旅行时,您关闭所有打开的数组,然后只打开您单击过的一个。
var allInfos = [];
function closeInfos() {
for (i = 0; i < allInfos.length; i++) {
allInfos[i].close();
}
}
Once created the infobox, dynamically are you adding it to the array each as follows:
创建信息框后,您是否将其动态添加到数组中,如下所示:
allInfos.push(infowindow);
回答by André Amaral
You need just one line:
$(".gm-style-iw").next().click();
你只需要一行:
$(".gm-style-iw").next().click();
There's a div with this class, 'gm-style-iw', inside the infowindow. After, there is the div of the close button. So, this code will click every infowindow close button existing in the map
信息窗口中有一个带有此类的 div,“gm-style-iw”。之后,有关闭按钮的div。因此,此代码将单击地图中存在的每个信息窗口关闭按钮
回答by Hugosolar
The best way to do this I think... is having an object with the infowindows that you have opened
我认为最好的方法是……使用您打开的信息窗口创建一个对象
I have two objects, infos have all the infowindows created and markers contains all markers with they infowindows so I just execute this functions that loop the infowindow object and close all the infowindows
我有两个对象,信息创建了所有信息窗口,标记包含所有带有信息窗口的标记,所以我只执行循环信息窗口对象并关闭所有信息窗口的函数
function CloseInfowindows() {
for (var mkey in infos) {
var mobj = markers[mkey];
mobj.infowindow.close();
}
}

