简体   繁体   English

Google Maps Infowindow关闭事件不起作用

[英]Google Maps infowindow close event not working

I have create an infowindow in google maps as: 我在Google地图中创建了一个信息窗口,如下所示:

var setInfoWindow = function() {
    infoWindowOpen = true;
    return new google.maps.InfoWindow({
         content: businessAddress
    });
}

This is the open event for infowindow. 这是infowindow的打开事件。 It is working 这是工作

google.maps.event.addListener(marker, 'click', function() {
   if(!infoWindowOpen) {
       setInfoWindow().open(map,marker);
       currentMark = this;
   }
});

This is the close event for infowindow. 这是infowindow的关闭事件。 It is not working 没用

google.maps.event.addListener(infowindow,'closeclick',function(){
   var infowindow = setInfoWindow();
   console.log("ddd");
   currentMark.setMap(null);
});

Here my open event is working but the close event is not getting triggered. 在这里,我的打开事件正在运行,但关闭事件未触发。 How can I solve this issue. 我该如何解决这个问题。

You need to add the 'closeclick' listener to the google.maps.InfoWindow object once it exists. 您需要将“ closeclick”侦听器添加到google.maps.InfoWindow对象。

var infowindow;
google.maps.event.addListener(marker, 'click', function() {
  if (!infoWindowOpen) {
    infowindow = setInfoWindow();
    infowindow.open(map, this);
    google.maps.event.addListener(infowindow, 'closeclick', function() {
      console.log("ddd");
      currentMark.setMap(null);
      infoWindowOpen = false;
    });
    currentMark = this;
  }
});

proof of concept fiddle 概念证明

code snippet: 代码段:

 var infowindow; var infoWindowOpen; var businessAddress = "Palo Alto, CA" function initialize() { var map = new google.maps.Map( document.getElementById("map_canvas"), { center: new google.maps.LatLng(37.4419, -122.1419), zoom: 13, mapTypeId: google.maps.MapTypeId.ROADMAP }); infowindow = new google.maps.InfoWindow(); var marker = new google.maps.Marker({ map: map, position: map.getCenter() }) google.maps.event.addListener(marker, 'click', function() { if (!infoWindowOpen) { infowindow = setInfoWindow(); infowindow.open(map, this); google.maps.event.addListener(infowindow, 'closeclick', function() { console.log("ddd"); currentMark.setMap(null); infoWindowOpen = false; }); currentMark = this; } }); } google.maps.event.addDomListener(window, "load", initialize); var setInfoWindow = function() { infoWindowOpen = true; return new google.maps.InfoWindow({ content: businessAddress }); } 
 html, body, #map_canvas { height: 100%; width: 100%; margin: 0px; padding: 0px } 
 <script src="https://maps.googleapis.com/maps/api/js"></script> <div id="map_canvas"></div> 

关闭事件发生在信息窗口上,而不是标记上,例如:

InfoWindow.close()

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

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