I want to clear a marker on Google Maps.
What is the difference between marker.setVisible(false)
and marker.setMap(null)
?
But I don't know, which is right?
I want to clear a marker on Google Maps.
What is the difference between marker.setVisible(false)
and marker.setMap(null)
?
But I don't know, which is right?
The difference between the two methods does not seem to be clearly documented. However, note the following:
When you use
setMap(null)
, your marker will lose the reference to theMap
. If you do not keep a reference to theMap
object, you wouldn't be able to reshow the marker.In addition, the
setMap()
method will not trigger thevisible_changed
event, while thesetVisible()
method does (if the visibility is actually toggled).Example:
I guess we should be using the
setVisible(false)
method when we intend to reshow the marker again on the map, and thesetMap(null)
when we will not be showing it again.Another key distinction is that
setMap(NULL)
releases the resources associated with the marker whereassetVisible(false)
just makes the marker invisible, but the resources associated with the marker are still allocated.If you're dealing with 100s or 1000s of markers, this can become a significant performance and memory issue.