Google maps api 3 如何检查地图上是否设置了信息窗口?

Google maps api 3 如何检查地图上是否设置了信息窗口?,google-maps-api-3,Google Maps Api 3,有没有办法在使用前先检查地图上是否设置了信息窗口 marker.infowindow.close(); 如果这没有设置,也没有在地图上,脚本就不能工作了?我正在考虑在关闭前检查if语句?您可以在Javascript中使用typeof函数 if (typeof(marker.infowindow) === "undefined"){ //object doesn't exist } else{ //the object does exist marker.infowindow.close();

有没有办法在使用前先检查地图上是否设置了信息窗口

marker.infowindow.close();

如果这没有设置,也没有在地图上,脚本就不能工作了?我正在考虑在关闭前检查if语句?

您可以在Javascript中使用
typeof
函数

if (typeof(marker.infowindow) === "undefined"){
//object doesn't exist
}
else{
//the object does exist
marker.infowindow.close();
}

您可以为用作InfoWindow内容的div提供一个ID。
document.getElementById('content-div-id')
将在地图上打开信息窗口时返回div,否则返回null。

您可以使用变量检查信息窗口是否存在:

var currentInfoWindow = null;

if(currentInfoWindow !== null){
    currentInfoWindow.close();
}
然后,在调用
infoWindow.open()
后,将currentInfoWindow分配给新的infoWindow:

currentInfoWindow = infoWindow;
这对于确保地图上只打开一个信息窗口特别有用