JQuery google map plugin adding event listeners

Can someone explain the meaning of the following code snippet ( jQuery.fn[name]) found in google jquery.ui.map plugin :

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

As well as how we could bind the callback function to the click event on the map object, I tried the following, but it eventdoes not have an attribute latLng:

$('#map_canvas').gmap().click(function(event) {
        alert(event.latLng);
    });

Thanks in advance.

+3
source share
4 answers

This piece of code overwrites some of the jQuery methods, so it can be used on some Google Maps objects. For instance.

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

If you need to bind other events like zoom_changed, then just

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

});
+6
source

:) Google, this.addEventListener(name, a, b); ( , . )

:

 google.maps.event.addListener(my_map_object, 'click', function() {
    my_map_object.setZoom(8);
    alert("I've just zoomed by clicking the map!");
 });

, .

. https://developers.google.com/maps/documentation/javascript/events. API Google Maps , :)

+1
google.maps.event.addListener(marker, 'mouseover', function() {
 $('.hover_div').html('<a target="_blank" href="'+marker.url+'">'+marker.hover + marker.title +'</a>').show();
});

    google.maps.event.addListener(marker, 'click', function() {
      window.open(
  marker.url,
  '_blank' // <- This is what makes it open in a new window.
);

, . , .

+1

, , . , trigger() , . - , on("click") , .

. , - . "" , "" , :

click() mapClick, dragend mapDragend ..

jQuery.each(('click rightclick dblclick mouseover mouseout drag dragend').split(' '), function (i, name) {
        jQuery.fn["map" + name[0].toUpperCase() + name.substr(1)] = function (a, b) {
            return this.addEventListener(name, a, b);
        }
});
0

All Articles