当相关标记被隐藏时,如何清理InfoWindow? [英] How do I clean up an InfoWindow when the associated Marker is hidden?

查看:231
本文介绍了当相关标记被隐藏时,如何清理InfoWindow?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道当我们点击一​​个标记时,许多人正在编写代码来打开 InfoWindow 。但是 InfoWindow 会保持原样,直到点击右上角的 X ,这意味着设置关联的 Marker false 的可见性将创建基本上孤立的 InfoWindow 。并且可以同时在地图上显示多个 InfoWindow 实例。我猜这对用户来说很简单,只需点击 InfoWindow 关闭,但感觉隐藏 Marker 应该隐藏关联的 InfoWindow



我已经开始编写如下代码来处理这种情况:



pre $ lt; code> google.maps.event.addListener(marker,click,function(){
var bubble = new google.maps。 InfoWindow({
content:buildBubbleContent(param1,param2)
});
bubble.open(map,marker);
//非常标准的东西到这里,但下一个(对我来说):
google.maps.event.addListenerOnce(marker,visible_changed,function(){
bubble.close();
});
});

这是其他人正在做的事情吗?这感觉就像一个应该叫做 ListenBack 的设计模式。我从未在Google地图文档中看到过这个问题。我不禁想到在 InfoWindow 中内置了一个更简单的机制来自动处理这个问题。有没有一种标准的方法来做到这一点,我刚刚错过了?

解决方案

继续浏览文档并查看许多代码示例,但还没有找到任何其他解决方案。 InfoWindow当然没有提供任何设施来在相关标记关闭时自动从地图中移除它。如果以后有人找到更好的选择,我会很乐意将他们的解决方案标记为更好的答案。

I know that many of us are writing code to open an InfoWindow when a marker is clicked. But the InfoWindow will stay in place until the upper-right X is clicked, which means that setting the associated Markervisibility to false will create what is essentially an orphaned InfoWindow. And there could be multiple InfoWindow instances displayed on the Map at the same time. I guess it's simple enough for the user to just click the InfoWindow closed, but it feels like hiding the Marker should hide the associated InfoWindow.

I have started writing code like the following to deal with this scenario:

google.maps.event.addListener( marker, "click", function() {
    var bubble = new google.maps.InfoWindow({
        content: buildBubbleContent( param1, param2 )
    });
    bubble.open( map, marker );
    //pretty standard stuff to here, but the next line is new (for me):
    google.maps.event.addListenerOnce( marker, "visible_changed", function() {
        bubble.close();
    });
});

Is this what everyone else is doing? It feels like a design pattern that should be called a ListenBack. I've never seen the issue addressed in the Google Maps docs. I can't help but think that there must be a simpler mechanism built into the InfoWindow to take care of this automatically. Is there a standard way to do this that I have just missed?

解决方案

I'm marking this question as answered, because I have continued scouring the docs and looking at many code samples, but haven't found any other solutions. There is certainly no facility provided with the InfoWindow to automatically remove it from the map when the associated marker is turned off. If anyone finds a better option later, I will happily mark their solution as the better answer.

这篇关于当相关标记被隐藏时,如何清理InfoWindow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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