【问题标题】:Is it possible to close an infowindow on certain condition是否可以在某些条件下关闭信息窗口
【发布时间】:2017-03-27 09:41:06
【问题描述】:

信息窗口应该只在您将鼠标悬停在标记上时显示。当您将鼠标从标记上移开时,它应该会消失。 只有当您单击标记时,它才会保留。并在点击信息窗口的关闭按钮时关闭。

【问题讨论】:

    标签: javascript angularjs google-maps


    【解决方案1】:

    您可以尝试使用google.maps.event.addListener:

    浏览器中的 JavaScript 是事件驱动的,这意味着 JavaScript 通过生成事件来响应交互,并期望程序监听有趣的事件。有两种类型的事件:

    • 用户事件(例如“点击”鼠标事件)从 DOM 传播到 Google Maps JavaScript API。这些事件与标准 DOM 事件是分开且不同的。
    • MVC 状态更改通知反映 Maps JavaScript API 对象的更改,并使用 property_changed 约定命名。

    每个 Maps JavaScript API 对象都会导出许多命名事件。对某些事件感兴趣的程序将为这些事件注册 JavaScript 事件侦听器,并在收到这些事件时执行代码,方法是调用 addListener() 在对象上注册事件处理程序。 p>

    您可以尝试使用post中的代码:

    var geocoder;
    var map;
    
    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);
    }
    

    这也可以在jsfiddle 中找到。只要您的事件监听器不与其他监听器冲突,它就会正常工作。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2022-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多