سؤال

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