在Google Maps API v3中只打开一个InfoWindow

我需要在Google地图上只有一个InfoWindow打开。 在打开一个新窗口之前,我需要closures所有其他InfoWindows。

有人可以告诉我如何做到这一点?

您只需要创build一个InfoWindow对象,保留对其的引用,并对所有标记进行重用。 从Google Maps API文档引用 :

如果您只希望一次显示一个信息窗口(就像Google地图上的行为一样),则只需创build一个信息窗口,您可以在地图事件(例如用户点击)上重新分配到不同的位置或标记。

因此,您可能只需要在初始化地图之后创buildInfoWindow对象,然后按如下方式处理标记的click事件处理程序。 假设你有一个叫做someMarker的标记:

 google.maps.event.addListener(someMarker, 'click', function() { infowindow.setContent('Hello World'); infowindow.open(map, this); }); 

那么当您点击一个新的标记而不必调用close()方法时, InfoWindow应该会自动closures。

创build您的infowindow超出范围,以便您可以分享。

这是一个简单的例子:

 var markers = [AnArrayOfMarkers]; var infowindow = new google.maps.InfoWindow(); for (var i = 0, marker; marker = markers[i]; i++) { google.maps.event.addListener(marker, 'click', function(e) { infowindow.setContent('Marker position: ' + this.getPosition()); infowindow.open(map, this); }); } 

我有同样的问题,但最好的答案并没有完全解决它,我在我的陈述中必须做的是使用这与我目前的标记有关。 也许这有助于某人。

 for(var i = 0; i < markers.length; i++){ name = markers[i].getAttribute("name"); address = markers[i].getAttribute("address"); point = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng"))); contentString = '<div style="font-family: Lucida Grande, Arial, sans-serif;>'+'<div><b>'+ name +'</b></div>'+'<div>'+ address +'</div>'; marker = new google.maps.Marker({ map: map, position: point, title: name+" "+address, buborek: contentString }); google.maps.event.addListener(marker, 'click', function(){ infowindow.setContent(this.buborek); infowindow.open(map,this); }); marker.setMap(map); } 

有点晚了,但我设法只有一个infowindow打开infowindow一个全局variables。

 var infowindow = new google.maps.InfoWindow({}); 

然后在列表器中

 infowindow.close(); infowindow = new google.maps.InfoWindow({ content: '<h1>'+arrondissement+'</h1>'+ gemeentesFiltered }); infowindow.open(map, this); 

声明一个globar var selectedInfoWindow; 并用它来保存打开的信息窗口:

  var infoWindow = new google.maps.InfoWindow({ content: content }); // Open the infowindow on marker click google.maps.event.addListener(marker, "click", function() { //Check if there some info window selected and if is opened then close it if (selectedInfoWindow != null && selectedInfoWindow.getMap() != null) { selectedInfoWindow.close(); //If the clicked window is the selected window, deselect it and return if (selectedInfoWindow == infoWindow) { selectedInfoWindow = null; return; } } //If arrive here, that mean you should open the new info window //because is different from the selected selectedInfoWindow = infoWindow; selectedInfoWindow.open(map, marker); }); 

您需要跟踪以前的InfoWindow对象, 并在处理新标记上的click事件时调用close方法

注意 :不需要在共享信息窗口对象上调用close,调用open不同的标记会自动closures原来的。 详情请参阅Daniel的回答 。

基本上你想要一个函数保持引用一个new InfoBox() =>委托onclick事件。 在创build标记时(循环中)使用bindInfoBox(xhr, map, marker);

 // @param(project): xhr : data for infoBox template // @param(map): object : google.maps.map // @param(marker): object : google.maps.marker bindInfoBox: (function () { var options = $.extend({}, cfg.infoBoxOptions, { pixelOffset: new google.maps.Size(-450, -30) }), infoBox = new window.InfoBox(options); return function (project, map, marker) { var tpl = renderTemplate(project, cfg.infoBoxTpl); // similar to Mustache, Handlebars google.maps.event.addListener(marker, 'click', function () { infoBox.setContent(tpl); infoBox.open(map, marker); }); }; }()) 

var infoBox被asynchronous分配并保存在内存中。 每次调用bindInfoBox() ,都会调用返回函数。 还方便地只通过infoBoxOptions一次!

在我的例子中,我不得不向map添加一个额外的参数,因为我的初始化被tab事件延迟了。

InfoBoxOptions

解决这个问题:

  function window(content){ google.maps.event.addListener(marker,'click', (function(){ infowindow.close(); infowindow = new google.maps.InfoWindow({ content: content }); infowindow.open(map, this); })) } window(contentHtml); 

这是一个解决scheme,不需要只创build一个infoWindow来重用它。 您可以继续创build多个infoWindows,唯一需要的是构build一个closeAllInfoWindows函数,并在打开一个新的infowindow之前调用它。 所以,保持你的代码,你只需要:

  1. 创build一个全局数组来存储所有的infoWindows

     var infoWindows = []; 
  2. 将每个新的infoWindow存储在数组中,紧接着infoWindow = new …

     infoWindows.push(infoWindow); 
  3. 创buildcloseAllInfoWindows函数

     function closeAllInfoWindows() { for (var i=0;i<infoWindows.length;i++) { infoWindows[i].close(); } } 
  4. 在你的代码中,在打开infoWindow之前调用closeAllInfoWindows()。

问候,

Google地图只允许您打开一个信息窗口。 所以如果你打开一个新窗口,另一个自动closures。