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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 theMapobject, you wouldn’t be able to reshow the marker.In addition, the
setMap()method will not trigger thevisible_changedevent, 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.