Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Google Maps V3 doesn't remove event listener

I have a problem with Google Maps API v3. I'm trying to remove a mouseover listener when zoom changes.

Here is my code:

$(document).ready(function() {

    var myOptions = {

    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    function colormaps(map) {
        var newmap = map;
        var piemonteCoords = [
        var piemontePolygon = new google.maps.Polygon({
        google.maps.event.addListener(piemontePolygon, 'mouseover', function(event) {
            var prova = event;
            showInfo(prova, newmap, 'Italy');

        google.maps.event.addListener(newmap, 'zoom_changed', function() {
            zoomLevel = map.getZoom();
            if (zoomLevel >= 6) {
                google.maps.event.clearListeners(newmap, 'mouseover');
            else {
        } //fine colormaps
        google.maps.event.addDomListener(window, 'load', colormaps(map));

When zoom reaches the target level, the listener is not removed. What's wrong?

like image 378
Andrea Avatar asked Dec 26 '22 12:12


1 Answers

You're adding listener to piemontePolygon object, but you're clearing it from newmap object, which probably is wrong (I'm not an expert in that area so I can't be sure).

Try doing it like:

google.maps.event.clearListeners(piemontePolygon, 'mouseover');

According to the docs you could try it with another approach, if the above code fails.

Case 1 (you could pass an event type as a second argument as well):


Case 2:

var listener1 = google.maps.event.addListener(piemontePolygon, 'mouseover', function(
    var prova = event;
    showInfo(prova, newmap, 'Italy')

like image 73
Jan.J Avatar answered Dec 31 '22 13:12