gmap3 移除事件监听器

3

我想要移除通过以下方式添加的点击事件监听器:

var events = {
    click: function () {
        // crazy stuff here :- )
    }
};

$(where).gmap3(
    {
        events: events
    }
);

需要类似如下的内容:
$(where).gmap3().removeEventListener('click');

为此,我需要引用特定的事件监听器对象。您知道如何将其添加到上面的代码中吗? - Wojciech Bednarski
2个回答

3

没有意识到gmap3是一个包装库。我会删除重复的评论。

浏览 gmaps3 文档,我没有看到任何特定的函数可以使用库函数来移除监听器,但您可以使用 action: 'get' 获取标记,然后清除监听器。

这是一个从文档中修改过的示例。我为标记添加了名称和标签属性,并在脚本末尾中删除了具有 tag:'2' 的标记的 mouseover 监听器。由于某种原因,这个库很棘手,需要同时使用 nametag 属性才能找到标记。

$('#test').gmap3({
    action: 'init',
    options: {
        center: [46.578498, 2.457275],
        zoom: 5
    }
}, {
    action: 'addMarkers',
    markers: [
        {
        name : 'marker', 
        tag: '1',
        lat: 48.8620722,
        lng: 2.352047,
        data: 'Paris !'},
        {
        name : 'marker',
        tag: '2',
        lat: 46.59433,
        lng: 0.342236,
        data: 'Poitiers : great city !'},
        {
        name : 'marker',
        tag: '3',
        lat: 42.704931,
        lng: 2.894697,
        data: 'Perpignan !  GO USAP !'}
    ],
    marker: {
        options: {
            draggable: false
        },
        events: {
            mouseover: function(marker, event, data) {
                var map = $(this).gmap3('get'),
                    infowindow = $(this).gmap3({
                        action: 'get',
                        name: 'infowindow'
                    });
                if (infowindow) {
                    infowindow.open(map, marker);
                    infowindow.setContent(data);
                } else {
                    $(this).gmap3({
                        action: 'addinfowindow',
                        anchor: marker,
                        options: {
                            content: data
                        }
                    });
                }
            },
            mouseout: function() {
                var infowindow = $(this).gmap3({
                    action: 'get',
                    name: 'infowindow'
                });
                if (infowindow) {
                    infowindow.close();
                }
            }
        }
    }
});


//get the marker by name and tag
var mark = $('#test').gmap3({
    action: 'get',
    name:'marker',
    tag: '2'
});

//remove the event listener
google.maps.event.clearListeners(mark, 'mouseover');

这是该脚本的一个示例: http://jsfiddle.net/5GcP7/。当鼠标悬停在中间的标记上时,不会打开信息窗口。

2
我用不同的方法解决了这个问题:
 var events = {
     click: function () {
         if (P.settings.mapPinActive === false) {
             return;
         }
         // crazy stuff here :- )
     }
 };

不需要分离和附加事件,使用设置对象中的全局属性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接