Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Google Maps v3, does removing a marker from memory automatically kill any listeners?

In my Google Maps v3 app, I am Creating Markers and putting them on the map. For each, I am adding an event listener on 'click' so I can show an Info Window when a user clicks on them.

I am keeping my Markers in a javascript array and use .setMap() to show/hide them on the map. There are some cases where a user wants them deleted from the map. In this case I do marker.setMap(null) and then delete the marker from my array.

Is it recommended to also keep an array of the event listeners on the markers so I can remove those when I delete the marker? Or will the event listener get removed from memory when it's listened-to object is removed from memory?

From an end-user standpoint, I don't think it matters to do this, but I am curious if the event listener is still in memory somewhere even though I delete the marker. I'd like to be as tidy as possible.

like image 243
hemmeter Avatar asked Oct 05 '11 00:10

hemmeter


1 Answers

I don't know for sure if the listeners are removed, but my guess is not. My guess is that since the listener is still listening, even though you no longer have a reference to the object, the listener does, so it will remain in memory. You don't need to keep a reference to the listener, you can call

google.maps.event.clearInstanceListeners(marker);

to clear all listeners before you delete it from your array.

like image 123
Danny Cohn Avatar answered Oct 23 '22 16:10

Danny Cohn