Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing className attribute in leaflet

This seems trivial but I can't find the answer anywhere. I have set the className attribute of a circleMarker:

var marker = L.circleMarker([lat, lng], {
  radius: 6,
  color: 'black',
  weight: 2,
  opacity: 1,
  fillColor: 'red',
  fillOpacity: 1.0,
  className: 'redSpot'
}).addTo(map);

and I simply want to access the className property later on, something like:

marker.className

But this gives 'undefined' rather than 'redSpot'. Other attempts all give 'undefined':

marker.attr('className')
marker.data('className')

How do you access the className attribute?

like image 527
Bazley Avatar asked Dec 11 '25 19:12

Bazley


1 Answers

I'm not sure what the final story is on this, but in all versions of Leaflet I tested (0.7 - 1.0), the circleMarker class DOES NOT inherit the className option for its constructor...even though the docs said it should. Weird. I know the leaflet team has described caveats and bugs related to this option.

Try this.

var marker = L.circleMarker([lat, lng], {
  radius: 6,
  color: 'black',
  weight: 2,
  opacity: 1,
  fillColor: 'red',
  fillOpacity: 1.0
}).addTo(map);

marker.className = "redSpot";
like image 171
Thomas Horner Avatar answered Dec 13 '25 10:12

Thomas Horner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!