Google地图:自动closuresInfoWindows?

在我的网站上 ,我正在使用Google Maps API v3在地图上放置房屋标记。

除非您明确地单击closures图标,否则InfoWindows保持打开状态。 意思是,如果将鼠标hover在地图标记上,可以一次打开2个InfoWindows。

问题 :如何使其仅显示当前活动的InfoWindow已打开且所有其他InfoWindow已closures? 意思是,一次不会超过1个InfoWindow?

InfoWindows有一个close()函数。 只需跟踪上次打开的窗口,并在创build新窗口时调用closuresfunction。

这个演示有你正在寻找的function。 我在Maps API V3演示库中find了它。

使用许多infowindows替代解决scheme:保存prev打开infowindow在一个variables,然后closures它,当新窗口打开

var prev_infowindow =false; ... base.attachInfo = function(marker, i){ var infowindow = new google.maps.InfoWindow({ content: 'yourmarkerinfocontent' }); google.maps.event.addListener(marker, 'click', function(){ if( prev_infowindow ) { prev_infowindow.close(); } prev_infowindow = infowindow; infowindow.open(base.map, marker); }); } 
 //assuming you have a map called 'map' var infowindow = new google.maps.InfoWindow(); var latlng1 = new google.maps.LatLng(0,0); var marker1 = new google.maps.Marker({position:latlng1, map:map}); google.maps.event.addListener(marker1, 'click', function(){ infowindow.close();//hide the infowindow infowindow.setContent('Marker #1');//update the content for this marker infowindow.open(map, marker1);//"move" the info window to the clicked marker and open it } ); var latlng2 = new google.maps.LatLng(10,10); var marker2 = new google.maps.Marker({position:latlng2, map:map}); google.maps.event.addListener(marker2, 'click', function(){ infowindow.close();//hide the infowindow infowindow.setContent('Marker #2');//update the content for this marker infowindow.open(map, marker2);//"move" the info window to the clicked marker and open it } ); 

这将“移动”信息窗口到每个点击标记,实际上closures自己,然后重新开放(和平移以适应视口)在其新的位置。 它在打开之前改变其内容以达到预期的效果。 适用于n个标记。

从此链接http://www.svennerberg.com/2009/09/google-maps-api-3-infowindows/

Teo:最简单的方法是只重复使用InfoWindow对象的一个​​实例。 这样,当你点击一个新的标记时,infoWindow从它当前所在的位置“移动”,指向新的标记。

使用它的setContent方法加载正确的内容。

我的解决scheme

 var map; var infowindow = new google.maps.InfoWindow(); ... function createMarker(...) { var marker = new google.maps.Marker({ ..., descrip: infowindowHtmlContent }); google.maps.event.addListener(marker, 'click', function() { infowindow.setOptions({ content: this.descrip, maxWidth:300 }); infowindow.open(map, marker); } 

除了使用close()函数,还有一个更简单的方法。 如果使用InfoWindow属性创buildvariables,则在打开另一个variables时它会自动closures。

 var info_window; var map; var chicago = new google.maps.LatLng(33.84659, -84.35686); function initialize() { var mapOptions = { center: chicago, zoom: 14, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); info_window = new google.maps.InfoWindow({ content: 'loading' )}; createLocationOnMap('Location Name 1', new google.maps.LatLng(33.84659, -84.35686), '<p><strong>Location Name 1</strong><br/>Address 1</p>'); createLocationOnMap('Location Name 2', new google.maps.LatLng(33.84625, -84.36212), '<p><strong>Location Name 1</strong><br/>Address 2</p>'); } function createLocationOnMap(titulo, posicao, conteudo) { var m = new google.maps.Marker({ map: map, animation: google.maps.Animation.DROP, title: titulo, position: posicao, html: conteudo }); google.maps.event.addListener(m, 'click', function () { info_window.setContent(this.html); info_window.open(map, this); }); } 

怎么样 –

 google.maps.event.addListener(yourMarker, 'mouseover', function () { yourInfoWindow.open(yourMap, yourMarker); }); google.maps.event.addListener(yourMarker, 'mouseout', function () { yourInfoWindow.open(yourMap, yourMarker); }); 

然后你可以把它盘旋在上面,它会自动closures。

 var map; var infowindow; ... function createMarker(...) { var marker = new google.maps.Marker({...}); google.maps.event.addListener(marker, 'click', function() { ... if (infowindow) { infowindow.close(); }; infowindow = new google.maps.InfoWindow({ content: contentString, maxWidth: 300 }); infowindow.open(map, marker); } ... function initialize() { ... map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); ... google.maps.event.addListener(map, 'click', function(event) { if (infowindow) { infowindow.close(); }; ... } } 

我在顶部存储了一个variables,以便跟踪哪个信息窗口当前处于打开状态,如下所示。

 var currentInfoWin = null; google.maps.event.addListener(markers[counter], 'click', function() { if (currentInfoWin !== null) { currentInfoWin.close(map, this); } this.infoWin.open(map, this); currentInfoWin = this.infoWin; }); 
 var contentString = "Location: " + results[1].formatted_address; google.maps.event.addListener(marker,'click', (function(){ infowindow.close(); infowindow = new google.maps.InfoWindow({ content: contentString }); infowindow.open(map, marker); }));