Jquery谷歌地图插件,添加事件监听器

Dil*_*sha 3 jquery callback addeventlistener google-maps-api-3 jquery-ui-map

有人可以解释google jquery.ui.map插件中的以下代码片段(jQuery.fn[name])的含义:

jQuery.each(('click mousedown rightclick dblclick mouseover mouseout drag dragend').split(' '), function(i, name) {
    jQuery.fn[name] = function(a, b) {
        return this.addEventListener(name, a, b);
    };
});
Run Code Online (Sandbox Code Playgroud)

以及我们如何将回调函数绑定到地图对象上的click事件,我尝试了以下但是event没有latLng属性:

$('#map_canvas').gmap().click(function(event) {
        alert(event.latLng);
    });
Run Code Online (Sandbox Code Playgroud)

提前致谢.

joh*_*son 6

这段代码会覆盖一些jQuery方法,以便可以在某些 Google Maps对象上使用.例如

    var map = $('#map_canvas').gmap('get', 'map')
    $(map).click(function() {
        var self = this; // this is the map object
    });

    $('#map_canvas').gmap('addMarker', { ... }).click(function() {
        var self = this; // this refers to the marker object
    }).hover(function() {
         var self = this; // this refers to the marker object
    });
Run Code Online (Sandbox Code Playgroud)

如果你需要绑定其他事件,比如zoom_changed那么

var map = $('#map_canvas').gmap('get', 'map');
$(map).addEventListener('zoom_changed', function() {

});
Run Code Online (Sandbox Code Playgroud)