Google maps api 3 检查infowindow是否已打开Google Maps v3
求你了,我需要帮助 我想检查我的信息窗口是否打开 例如:Google maps api 3 检查infowindow是否已打开Google Maps v3,google-maps-api-3,infowindow,Google Maps Api 3,Infowindow,求你了,我需要帮助 我想检查我的信息窗口是否打开 例如: if (infowindow.isOpened) { doSomething() } 或 我不知道该怎么做,这是一个未记录的特性,因此可能会在不通知的情况下更改,但是infoWindow.close()方法将对象上的映射设置为null(这就是infoWindow.open(map[anchor])要求您传入map)的原因,因此,您可以检查此属性以判断当前是否正在显示: function isInfoWindowOpen(infoW
if (infowindow.isOpened)
{
doSomething()
}
或
我不知道该怎么做,这是一个未记录的特性,因此可能会在不通知的情况下更改,但是
infoWindow.close()
方法将对象上的映射设置为null
(这就是infoWindow.open(map[anchor])
要求您传入map
)的原因,因此,您可以检查此属性以判断当前是否正在显示:
function isInfoWindowOpen(infoWindow){
var map = infoWindow.getMap();
return (map !== null && typeof map !== "undefined");
}
if (isInfoWindowOpen(infoWindow)){
// do something if it is open
} else {
// do something if it is closed
}
更新:
另一种可能有用的编写方法是将isOpen()
方法添加到InfoWindow
prototype
google.maps.InfoWindow.prototype.isOpen = function(){
var map = this.getMap();
return (map !== null && typeof map !== "undefined");
}
在google没有给我们更好的方法之前,你可以在infoWindow对象中添加一个属性。比如:
google.maps.InfoWindow.prototype.opened = false;
infoWindow = new google.maps.InfoWindow({content: '<h1> Olá mundo </h1>'});
if(infoWindow.opened){
// do something
infoWindow.opened = false;
}
else{
// do something else
infoWindow.opened = true;
}
google.maps.InfoWindow.prototype.opened=false;
infoWindow=new google.maps.infoWindow({content:'Olámundo'});
如果(infoWindow.opened){
//做点什么
infoWindow.opened=false;
}
否则{
//做点别的
infoWindow.opened=true;
}
我修改了google.maps.InfoWindow的原型,并将打开/关闭更改为设置/清除属性:
//
// modify the prototype for google.maps.Infowindow so that it is capable of tracking
// the opened state of the window. we track the state via boolean which is set when
// open() or close() are called. in addition to these, the closeclick event is
// monitored so that the value of _openedState can be set when the close button is
// clicked (see code at bottom of this file).
//
google.maps.InfoWindow.prototype._open = google.maps.InfoWindow.prototype.open;
google.maps.InfoWindow.prototype._close = google.maps.InfoWindow.prototype.close;
google.maps.InfoWindow.prototype._openedState = false;
google.maps.InfoWindow.prototype.open =
function (map, anchor) {
this._openedState = true;
this._open(map, anchor);
};
google.maps.InfoWindow.prototype.close =
function () {
this._openedState = false;
this._close();
};
google.maps.InfoWindow.prototype.getOpenedState =
function () {
return this._openedState;
};
google.maps.InfoWindow.prototype.setOpenedState =
function (val) {
this._openedState = val;
};
您还需要监视closeclick事件,因为单击close按钮不会调用close()
调用InfoWindow.getOpenedState()
返回一个布尔值,该值反映InfoWindow的状态(打开/关闭)
我选择这样做,而不是使用InfoWindow.getMap()
或MVCObject.get('map')
方法,因为使用未记录的行为存在众所周知的缺陷。但是,google使用MVCObject.set('map',null)
强制从DOM中删除InfoWindow,因此这不太可能改变…InfoWindow.getMap()
如果InfoWindow
关闭,则返回null
因此,您可以简单地使用:
if (infowindow.getMap());
您只需为infoWindow设置
键
和值
:infoWindow.set('closed',true)代码>
示例:
const infoWindow=new google.maps.infoWindow({
内容:“foo”,
职位:{
拉特:一些号码,
液化天然气:一些
}
});
infoWindow.set('closed',true);
//单击此示例中的多段线
//也可以是标记
polyline.addListener(
“点击”,
() => {
if(infoWindow.get('closed')){
打开(地图);
infoWindow.set('closed',false);
}否则{
infoWindow.close();
infoWindow.set('closed',true);
}
}
);
infoWindow类没有名为.getMap()的方法。在文档()中没有列出它,但它适合我。你可以在Chrome控制台上看到它的屏幕截图:这篇Google Groups的帖子也有同样的建议:关于未记录的功能,它们可能会改变,或者完全消失,在未来的更新中不会发出警告。我会建议不要使用它们,即使它们现在似乎在这里工作。你能发布完整的代码吗?我用我自己的一些实现进行了测试,它确实有效,只是没有@Marcelo提到的官方支持。您不需要像我那样定义函数,假设变量名为infoWindow
,您也可以将其编写为:if(infoWindow.getMap()){window.alert('visible');}else{window.alert('hidden');}
谢谢。这比使用未注册的谷歌功能要好。我当然更喜欢这种方法。无论如何,你可能会有某种形式的“标记管理器”,所以实现这一点很容易。清晰快速的解决方案,工作了近7年。谢谢为什么不只是检查信息窗口的内容就有parentElement?InfoWindow有一个记录在案的“内容”属性,因此您只需检查它是否有parentElement:可能这取决于您如何设置内容(字符串或节点)。迄今为止,这是最好、最简单的解决方案,无需破解或修改/添加功能,只需使用提供的功能即可。想知道OP提问时set方法是否可用!Dakujem velmi pekneI只是偶然发现了类似的需求,并看到了这个答案。我喜欢。非常简单和轻便!省得我用一个额外的函数来扩充代码。
//
// monitor the closelick event and set opened state false when the close
// button is clicked.
//
(function (w) {
google.maps.event.addListener(w, "closeclick", function (e) {
w.setOpenedState(false);
});
})(infowindow);
if (infowindow.getMap());