首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

Google Maps V3版本,简略的infowindow.close不成功

2013-01-07 
Google Maps V3版本,简单的infowindow.close不成功var lastInfWnd nullgoogle.maps.event.addListener(

Google Maps V3版本,简单的infowindow.close不成功
var lastInfWnd = null;

google.maps.event.addListener(_marker, 'click', function() {
            var infowindow = new google.maps.InfoWindow({
                content: divMarkerInf
            });
            infowindow.open(map, _marker);
            if (lastInfWnd) {
                lastInfWnd.close();
                
            }
          lastInfWnd = infowindow;
            
        });

就是想点开新的infowindow的时候把上次打开的infowindow关掉。我发现它不成功的原因可能是因为这是个网络程序,有速度问题,鼠标点到marker的时候不执行lastInfWnd.close();,
或者当执行lastInfWnd.close();的时候,还没有关掉lastInfWnd,马上就又执行了lastInfWnd = infowindow;
所以lastInfWnd的值改了,lastInfWnd.close()当然关掉的也不是真正的上个弹窗。

我想到的办法是

google.maps.event.addListener(_marker, 'click', function() {
            var infowindow = new google.maps.InfoWindow({
                content: divMarkerInf
            });
            infowindow.open(map, _marker);
            if (lastInfWnd) {
                //lastInfWnd.close();
                setTimeout(function() { lastInfWnd.close(); }, 1000);

            }
            setTimeout(function() { lastInfWnd = infowindow; }, 3000);
            
        });

调试的时候一步一步,等的时间长,效果是正确的,但真正运行,又是错的,到底为什么呀
[解决办法]
if (lastInfWnd) {
  lastInfWnd.close();
   
  }
var infowindow = new google.maps.InfoWindow({
  content: divMarkerInf
  });
  infowindow.open(map, _marker);

这样的顺序,能拖延lastInfWnd.close();占用的时间

热点排行