Get ClassName Method?

28 views
Skip to first unread message

Brad McCarron

unread,
Apr 14, 2014, 3:20:05 PM4/14/14
to leafl...@googlegroups.com
Hello,

I'm currently iterating through a set of marker objects.

I would like to identify the marker by its current icon/class name. Ideally the code would go something like this:

If(marker.className = "iconYellow")
  {
    alert('This is a yellow marker!');
  }

Does anyone know if this is possible?

Thank you in advance.

Brad McCarron

Brad McCarron

unread,
Apr 19, 2014, 6:37:33 PM4/19/14
to leafl...@googlegroups.com
OK here is the solution I came up with:

This function displays everything in the marker object:

function printObject(o) {  
  var out = '';
  for (var p in o) {
   out += p + ': ' + o[p] + '\n';
  }
  alert(out);
}

You can can take a peek at all the properties of the marker by calling the function and passing the marker object to it:

printObject(markerobject);

After printing the object, I found out I could print the icon object directly like this:

alert(markerobject._icon);

After viewing the properties of the icon object, I noticed that my custom div icon was displayed in the outerHTML portion of the _icon attributes.

Printing _icon.outerHTML shows the div icon stuff:

<div class="leaflet-marker-icon whitediv leaflet-zoom-animated leaflet-clickable" title="Resorts" ... etc

From there I just searched for the classes that I needed to identify like this:

if(markerObject._icon.outerHTML.search("bluediv")>0)
{
 // then this marker was set with my bluediv class.. so change its icon to a different shape, but with a blue color...
}

The ability to access the marker object attributes without relying on the built-in methods is a huge help for certain tasks.

Hope this helps someone.

Reply all
Reply to author
Forward
0 new messages