在Google Maps API v3中只打开一个InfoWindow [英] Have just one InfoWindow open in Google Maps API v3

查看:96
本文介绍了在Google Maps API v3中只打开一个InfoWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的Google地图上只有一个InfoWindow打开。我需要关闭所有其他信息窗口,然后再打开一个新窗口。

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 对象,保留对其的引用,并对所有标记重用。 从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 对象,然后处理标记的事件处理程序,如下所示。假设您有一个名为 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆