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