谷歌地图infowindow关闭事件无法正常工作

时间:2018-01-08 15:30:34

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

我在谷歌地图中创建了一个infowindow:

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

这是infowindow的公开活动。它正在运作

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

这是infowindow的近似事件。它无法正常工作

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

这里我的开放活动正在运作,但是关闭事件没有被触发。我该如何解决这个问题。

2 个答案:

答案 0 :(得分:0)

一旦存在,就需要将{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;
  }
});

https://developer.sabre.com/docs/read/workflows/Low_Fare_Search_and_Book

代码段

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>

答案 1 :(得分:-1)

关闭事件发生在infowindow,而不是标记,如:

InfoWindow.close()