Google Maps Mouseout仅适用于上一次infowindow

mar*_*rio 3 javascript google-maps infowindow google-maps-api-3

你好,我有一个随机的问题,我一直无法弄清楚.我有一张带有标记的地图,当你将鼠标悬停在它们上面时会显示信息,当你将鼠标移开时它们会关闭.出于某种原因,第二部分(关闭鼠标输出时的信息窗口)仅应用于最后一个标记.

如果有人能够向我解释我哪里出错了以及如何修复我的代码,以便当用户将鼠标移离标记时所有信息都会关闭,我们将不胜感激!谢谢!

这是我的setMarkers片段

function setMarkers(map, locations) {

    for (var i = 0; i < locations.length; i++) {
        var item = locations[i];

        var myLatLng = new google.maps.LatLng(item[1], item[2]);

        var address1 = item[5];

        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
        });

        var content = address;

        var infowindow = new google.maps.InfoWindow()

        google.maps.event.addListener(marker, 'mouseover', (function (marker, content, infowindow) {
            return function () {
                infowindow.setContent(content);
                infowindow.open(map, marker);
            };
        })(marker, content, infowindow));

        google.maps.event.addListener(marker, 'mouseout', function () {
            infowindow.close();
        });
    }

}

google.maps.event.addDomListener(window, 'load', initialize);
Run Code Online (Sandbox Code Playgroud)

geo*_*zip 9

你没有得到mouseout处理程序的函数闭包,但你是为鼠标悬停处理程序.要解决此问题,请更改:

function setMarkers(map, locations) {

      for (var i=0; i < locations.length; i++){
        var item   = locations[i];

        var myLatLng    = new google.maps.LatLng(item[1], item[2]);

        var address1    = item[5];

        var marker = new google.maps.Marker({
          position: myLatLng,
          map: map,
        });

        var content = address;

       var infowindow = new google.maps.InfoWindow()

      google.maps.event.addListener(marker,'mouseover', (function(marker,content,infowindow){ 
        return function() {
           infowindow.setContent(content);
           infowindow.open(map,marker);
        };
    })(marker,content,infowindow)); 

       google.maps.event.addListener(marker, 'mouseout', function(){
          infowindow.close();
       });
      }

    }  

google.maps.event.addDomListener(window, 'load', initialize);
Run Code Online (Sandbox Code Playgroud)

至:

function setMarkers(map, locations) {

      for (var i=0; i < locations.length; i++){
        var item   = locations[i];

        var myLatLng    = new google.maps.LatLng(item[1], item[2]);

        var address    = item[5];

        var marker = new google.maps.Marker({
          position: myLatLng,
          map: map,
        });

        var content = address;

       var infowindow = new google.maps.InfoWindow()

      google.maps.event.addListener(marker,'mouseover', (function(marker,content,infowindow){ 
        return function() {
           infowindow.setContent(content);
           infowindow.open(map,marker);
        };
    })(marker,content,infowindow)); 
      google.maps.event.addListener(marker, 'mouseout', (function(marker,content,infowindow){ 
        return function() {
           infowindow.close();
        };
    })(marker,content,infowindow)); 

    }  
}
google.maps.event.addDomListener(window, 'load', initialize);
Run Code Online (Sandbox Code Playgroud)

工作小提琴

工作代码段:

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

  setMarkers(map, locations);


}
google.maps.event.addDomListener(window, "load", initialize);
var locations = [
  ['Bondi Beach', -33.890542, 151.274856, , , 'Bondi Beach', 4],
  ['Coogee Beach', -33.923036, 151.259052, , , 'Coogee Beach', 5],
  ['Cronulla Beach', -34.028249, 151.157507, , , 'Cronulla Beach', 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, , , 'Manly Beach', 2],
  ['Maroubra Beach', -33.950198, 151.259302, , , 'Maroubra Beach', 1]
];

function setMarkers(map, locations) {
  var bounds = new google.maps.LatLngBounds();
  for (var i = 0; i < locations.length; i++) {
    var item = locations[i];

    var myLatLng = new google.maps.LatLng(item[1], item[2]);
    bounds.extend(myLatLng);
    var address = item[5];

    var marker = new google.maps.Marker({
      position: myLatLng,
      map: map,
    });

    var content = address;

    var infowindow = new google.maps.InfoWindow()

    google.maps.event.addListener(marker, 'mouseover', (function(marker, content, infowindow) {
      return function() {
        infowindow.setContent(content);
        infowindow.open(map, marker);
      };
    })(marker, content, infowindow));
    google.maps.event.addListener(marker, 'mouseout', (function(marker, content, infowindow) {
      return function() {
        infowindow.close();
      };
    })(marker, content, infowindow));

  }
  map.fitBounds(bounds);
}
Run Code Online (Sandbox Code Playgroud)
html,
body,
#map_canvas {
  height: 500px;
  width: 500px;
  margin: 0px;
  padding: 0px
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas" style="width:750px; height:450px; border: 2px solid #3872ac;"></div>
Run Code Online (Sandbox Code Playgroud)

  • 就像旁注一样:该片段是一个很好的例子,说明为什么我不会将mouseover/mouseout用于infowindows.由于API会调整地图的居中位置,以便infowindow可以在地图画布区域内完全显示,当您将鼠标悬停在靠近顶部边缘的标记处时,您将获得*`mouseover - > show - > center - > mouseout - > hide`*效果,我觉得很令人不安.当然也可以通过点击来实现居中,但是infowindow保持开放状态,在UX方面我感觉好多了. (2认同)