【问题标题】:Google Maps Mouseout only applied to last infowindowGoogle Maps Mouseout 仅适用于最后一个信息窗口
【发布时间】:2015-02-07 21:44:37
【问题描述】:

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

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

这是我的 setMarkers sn-p

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

【问题讨论】:

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


    【解决方案1】:

    你没有得到 mouseout 处理程序的函数闭包,但你是 mouseover 处理程序。要解决此问题,请更改:

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

    收件人:

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

    working fiddle

    工作代码sn-p:

    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);
    }
    html,
    body,
    #map_canvas {
      height: 500px;
      width: 500px;
      margin: 0px;
      padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <div id="map_canvas" style="width:750px; height:450px; border: 2px solid #3872ac;"></div>

    【讨论】:

    • 太棒了!做到了,谢谢!闭包是做什么的?我的意思是为什么有必要?
    • 顺便说一句:sn-p 是一个很好的例子,说明了为什么我不会在 infowindows 中使用 mouseover/mouseout。由于 API 调整了地图的居中,因此信息窗口可以在地图画布区域内完全显示,当您将鼠标悬停在靠近顶部边缘的标记时,您将获得 mouseover -&gt; show -&gt; center -&gt; mouseout -&gt; hide 效果我觉得很令人不安。当然,点击也可以实现居中,但信息窗口保持打开状态,就用户体验而言,这对我来说感觉好多了。
    猜你喜欢
    • 2015-09-26
    • 1970-01-01
    • 2020-12-06
    • 2016-02-16
    • 2013-02-13
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 2014-10-16
    相关资源
    最近更新 更多