谷歌地图:自动关闭打开InfoWindows?

时间:2010-02-08 17:37:55

标签: javascript google-maps google-maps-api-3

On my site,我正在使用Google Maps API v3在地图上放置自家标记。

除非您明确单击关闭图标,否则InfoWindows将保持打开状态。这意味着,如果将鼠标悬停在地图标记上,则可以一次打开2个以上的InfoWindows。

问题:我该怎么做才能只打开当前活动的InfoWindow并关闭所有其他InfoWindows?这意味着,一次只能打开1个InfoWindow?

12 个答案:

答案 0 :(得分:145)

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

This demo具有您正在寻找的功能。我在Maps API V3 demo gallery中找到了它。

答案 1 :(得分:58)

使用许多infowindows的替代解决方案: 保存prev在变量中打开infowindow,然后在新窗口打开时关闭它

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

答案 2 :(得分:26)

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

这将“移动”信息窗口到每个单击的标记,实际上关闭自身,然后重新打开(并平移以适应视口)在其新位置。它在打开之前改变其内容以产生期望的效果。适用于n个标记。

答案 3 :(得分:13)

我的解决方案。

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

答案 4 :(得分:9)

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

  

Teo:最简单的方法是   只有一个实例   您重复使用的InfoWindow对象   再来一次。那样的话   单击infoWindow所在的新标记   从目前的位置“移动”,   指向新标记。

     

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

答案 5 :(得分:5)

除了使用close()函数之外,还有一种更简单的方法。如果使用InfoWindow属性创建变量,则在打开另一个变量时会自动关闭。

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

答案 6 :(得分:2)

怎么样 -

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

    });

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

    });

然后你可以将鼠标悬停在它上面它会自动关闭。

答案 7 :(得分:1)

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();
        };
        ...
    }
}

答案 8 :(得分:1)

我在顶部存储了一个变量来跟踪当前打开的信息窗口,见下文。

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

答案 9 :(得分:1)

  

为活动窗口声明变量

 marker.addListener('click', function () {
    if (activeInfoWindow) { activeInfoWindow.close();}
    infowindow.open(map, marker);
    activeInfoWindow = infowindow;
});
  

并将此代码绑定到标记侦听器中

array.reshape(8,3)

答案 10 :(得分:0)

如果您在for循环中使用许多标记(此处为Django),这就是我使用的方式。您可以在每个标记上设置一个索引,并在每次打开窗口时设置该索引。关闭先前保存的索引:

markers = Array();
infoWindows = Array();
var prev_infowindow =false;
{% for obj in objects %}
var contentString = 'your content'
var infowindow = new google.maps.InfoWindow({
            content: contentString,
          });
var marker = new google.maps.Marker({
               position: {lat: {{ obj.lat }}, lng: {{ obj.lon }}},
               map: map,
               title: '{{ obj.name }}',
               infoWindowIndex : {{ forloop.counter0 }}
          });
google.maps.event.addListener(marker, 'click',
            function(event)
            {
           if( prev_infowindow ) {
               infoWindows[prev_infowindow].close();
                }
                prev_infowindow = this.infoWindowIndex;
                infoWindows[this.infoWindowIndex].open(map, this);
            }
        );

        infoWindows.push(infowindow);
        markers.push(marker);

      {% endfor %}

答案 11 :(得分:-1)

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);
}));
相关问题