繁体   English   中英

在谷歌地图中只打开一个 InfoWindow API v3

[英]Have just one InfoWindow open in Google Maps API v3

我只需要在我的 Google Map 上打开一个 InfoWindow。在打开一个新的 InfoWindow 之前,我需要关闭所有其他 InfoWindow。

有人可以告诉我该怎么做吗?

您只需要创建一个InfoWindow对象,保留对它的引用,并为所有标记重用 if。 引自 Google Maps API Docs

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

因此,您可能只想在初始化地图后立即创建InfoWindow对象,然后按如下方式处理标记的click事件处理程序。 假设您有一个名为someMarker的标记:

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

然后,当您单击新标记时, InfoWindow应自动关闭,而无需调用close()方法。

在范围之外创建您的信息窗口,以便您可以共享它。

这是一个简单的例子:

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 语句中必须做的是使用与我当前标记相关的 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 设为全局变量只打开了一个信息窗口。

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

然后在监听器里面

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

infowindow.open(map, this);

声明一个全局变量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方法

NB没有必要在共享信息窗口对象上调用 close ,使用不同的标记调用 open 将自动关闭原始。 有关详细信息,请参阅Daniel 的回答

基本上你想要一个函数来保持对一个new InfoBox() => 委托 onclick 事件的引用。 在创建标记时(在循环中)使用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被异步分配并保存在内存中。 每次调用bindInfoBox() ,都会调用返回函数。 只传递一次infoBoxOptions也很方便!

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

信息框选项

这是一种不需要仅创建一个 infoWindow 即可重用它的解决方案。 您可以继续创建许多 infoWindows,您唯一需要做的就是构建一个 closeAllInfoWindows 函数,并在打开新的 infowindow 之前调用它。 因此,保留您的代码,您只需要:

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

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

     infoWindows.push(infoWindow);
  3. 创建 closeAllInfoWindows 函数

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

问候,

使用 jQuery 执行此操作的一种智能简单方法如下:

            google.maps.event.addListener(marker, 'click', function (e) {
                jQuery(".gm-ui-hover-effect").click();
                marker.info.open(map, this);
            });

它将单击工具提示中的所有关闭按钮。

我的方法也允许您切换信息窗口。

全球空间

var infoWindow = new google.maps.InfoWindow();
infoWindow.setContent(contentString);

var lastInfoWindow;

局部空间

marker.addListener("click", (e) => {
  if (lastInfoWindow === e.domEvent.srcElement) {
    infoWindow.close();
    lastInfoWindow = null;
  } else {
    infoWindow.open({
      anchor: marker,
      map,
      shouldFocus: false,
    });
    lastInfoWindow = e.domEvent.srcElement;
  }
});

是这样解决的:

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);

Google 地图允许您只打开一个信息窗口。 因此,如果您打开一个新窗口,另一个窗口会自动关闭。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM