3

I want to remove event listener for click added by:

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

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

Need something like:

$(where).gmap3().removeEventListener('click');
skaffman
  • 398,947
  • 96
  • 818
  • 769
Wojciech Bednarski
  • 6,033
  • 9
  • 49
  • 73

2 Answers2

3

Didn't realize gmap3 was a wrapper library. Ill remove the duplicate comment.

Browsing through the gmaps3 documentation, I did not see anything specific to remove listeners with the libraries functions, but you can grab the marker with action: 'get' and then clear the listener.

Here is a example that a altered from the documentation. I added a name and tag property to the markers and at the end of this script I remove the mouseover listener from the marker with tag:'2'. For some reason this library is fickle and wants both the name and tag property to find the marker.

$('#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');

Here is an example of this script working: http://jsfiddle.net/5GcP7/. The marker in the middle will not open an infowindow when moused over.

Bryan Weaver
  • 4,455
  • 1
  • 29
  • 39
2

I solved this problem in different way:

 var events = {
     click: function () {
         if (P.settings.mapPinActive === false) {
             return;
         }
         // crazy stuff here :- )
     }
 };

Instead of detaching and attaching events, global properties in settings object.

Wojciech Bednarski
  • 6,033
  • 9
  • 49
  • 73