Question

I want to implement something by Google maps and I need to know if setMap is alreay set or is null. How can I implement something like this:

if (marker.setMap == null)
{
    marker.setMap(map);
}
Was it helpful?

Solution

You have to call the getMap function, not check if the setMap function is null. This will toggle the marker:

if (marker.getMap() == null) marker.setMap(map);
else marker.setMap(null);

or even

if (!marker.getMap()) marker.setMap(map);
else marker.setMap(null);

OTHER TIPS

You can't do that, because marker.setMap removes the reference to the map.
Instead, use marker.setVisible(false) to hide your marker, then, simply test visibility of your marker. setVisible just hides the marker, but the reference to the map is not lost.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top