在 Google Maps API v3 中只打开一个 InfoWindow [英] Have just one InfoWindow open in Google Maps API v3
问题描述
我只需要在我的 Google 地图上打开一个信息窗口.在打开一个新的 InfoWindows 之前,我需要关闭所有其他 InfoWindows.
I need to have only one InfoWindow open on my Google Map. I need to close all other InfoWindows before I open a new one.
有人可以告诉我怎么做吗?
Can someone show me how to do this?
推荐答案
您只需要创建一个 InfoWindow
对象,保留对它的引用,并为所有标记重复使用 if.引自 Google Maps API 文档:
You need to create just one InfoWindow
object, keep a reference to it, and reuse if for all the markers. Quoting from the Google Maps API Docs:
如果您只想一次显示一个信息窗口(就像 Google 地图上的行为一样),您只需要创建一个信息窗口,您可以根据地图事件(例如用户点击)将其重新分配到不同的位置或标记).
If you only want one info window to display at a time (as is the behavior on Google Maps), you need only create one info window, which you can reassign to different locations or markers upon map events (such as user clicks).
因此,您可能只想在初始化地图后创建 InfoWindow
对象,然后按如下方式处理标记的 click
事件处理程序.假设您有一个名为 someMarker
的标记:
Therefore, you may simply want to create the InfoWindow
object just after you initialize your map, and then handle the click
event handlers of your markers as follows. Let's say you have a marker called someMarker
:
google.maps.event.addListener(someMarker, 'click', function() {
infowindow.setContent('Hello World');
infowindow.open(map, this);
});
然后 InfoWindow
应该在您点击新标记时自动关闭,而无需调用 close()
方法.
Then the InfoWindow
should automatically close when you click on a new marker without having to call the close()
method.
这篇关于在 Google Maps API v3 中只打开一个 InfoWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!