Skip to content Skip to sidebar Skip to footer

Google Maps V3: Check If Marker Is Present On Map?

In Google Maps V3, is there a way to check whether a marker is actually present on the map? I have markers that vanish when clicked. I'd like some logic to check the present visib

Solution 1:

This one-liner will return true if the marker's position is contained under the current map boundary, and return false if not.

map.getBounds().contains(marker.getPosition())

Hope that helps, Cheers!

Solution 2:

start_marker.getMap()

Would return null if you'd previously used start_marker.setMap(null); as in your example.

That said, why not use setVisible and getVisible if you just want to hide and show markers?

Solution 3:

If you wish to just to hide/show the marker you could use the setVisible method of of the marker like:

 start_marker.setVisible(false);//to hide
 start_marker.setVisible(true);//to show

because setMap(null) does not hide the marker but removes the marker from the map.

You could then use getVisible() to get the visibility of the marker like:

console.log('Type of start_marker is now: ' + start_marker.getVisible());

You could read them here: https://developers.google.com/maps/documentation/javascript/overlays#Markershttps://developers.google.com/maps/documentation/javascript/overlays

Solution 4:

Solution 5:

I think you have to change your logic.Why not store your markers in an array and remove them completely from this array when they are clicked.So the remaining markers are the visible ones.

Cheers

Post a Comment for "Google Maps V3: Check If Marker Is Present On Map?"