Search code examples
leafletleaflet-geoman

How to get instance of L.DivIcon leaflet


I am trying to add classes to markers which works fine. However when it is a divIcon it requires different styling. So I am trying to set another className if the Instance is a DivIcon.

The Code below is always true because L.divIcon is still a marker(?)

if(e.target instanceof L.DivIcon) {
    let icon = e.target._icon
    if(!(L.DomUtil.hasClass(icon, 'leaflet-pm-divIcon-selected'))) {
      L.DomUtil.addClass(e.target._icon, 'leaflet-pm-divIcon-selected')
    }
  } 
  else if (e.target instanceof L.Marker) {
    let icon = e.target._icon
      if(!(L.DomUtil.hasClass(icon, 'leaflet-pm-marker-selected'))) {
      offsetMarker(icon, 8)
      L.DomUtil.addClass(e.target._icon, 'leaflet-pm-marker-selected')
    }
  }

doing e.target instanceof L.Marker && e.target instanceof L.DivIcon also does not work


Solution

  • Check if the layer icon is L.DivIcon:

    e.target.getIcon() instanceof L.DivIcon