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({
...
});
piemontePolygon.setMap(newmap);
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?
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');
EDIT:
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):
google.maps.event.clearInstanceListeners(piemontePolygon);
Case 2:
var listener1 = google.maps.event.addListener(piemontePolygon, 'mouseover', function(
var prova = event;
showInfo(prova, newmap, 'Italy')
});
google.maps.event.removeListener(listener1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With