点击并关闭谷歌地图上的事件

click and closeclick event on google maps

本文关键字:事件 谷歌地图      更新时间:2023-09-26

我想在同一个标记上打开两个信息窗口。在"点击"事件中,第一个信息窗口应该关闭(我已经在地图上显示)并打开第二个信息窗口。关闭第二个信息窗口后,我想打开第一个窗口。

这是我的代码:

var infowindow = new google.maps.InfoWindow({
            content: content,
            marker:marker,
            maxWidth: 300,
            image:image,
            id:vehicleNo
        });         
var openinfowindow = new google.maps.InfoWindow({
            content: contentone,
            marker:marker,
            maxWidth: 300,
            image:image,
            id:vehicleNo
        }); 
google.maps.event.addListener(marker,'click', (function(marker,content,infowindow){         
                return function() {                             
                    openinfowindow.close();                 
                    infowindow.setContent(content);
                    infowindow.open(map,marker);
            };              
        })(marker,content,infowindow)); 
    google.maps.event.addListener(infowindow,'closeclick',function(){
            openinfowindow.setContent(contentone);
            openinfowindow.open(map,marker);                                
    });

由于信息窗口之间的唯一区别是所需的内容,您可以使用单个信息窗口并简单地切换内容:

    var infowindow = new google.maps.InfoWindow({
        contents:[content,contentOne],
        marker:marker,
        maxWidth: 300,
        image:image,
        id:vehicleNo
    });         
    google.maps.event.addListener(marker,'click', (function(marker,
                                                            contents,
                                                            infowindow){         
      return function() {                                       
                infowindow.contents=[contents[0],contents[1]];
                infowindow.setContent(contents[0]);
                infowindow.open(map,marker);
      };              
    })(marker,infowindow.contents,infowindow)); 
    google.maps.event.addListener(infowindow,'closeclick',function(){
        var that=this;
        that.contents.reverse();
        that.setContent(this.contents[0]);
        setTimeout(function(){
          that.open(map,marker);},
          100);                                
    });

http://jsfiddle.net/doktormolle/affgpsvt/