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);
}
有帮助吗?

解决方案

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);

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top