0

I need to make a distinction if a vector layer that's added to a leaflet map is a line or an area. My goal is to automatically generate a legend that has lines (colored by the color attribute and styled by dashArray of the layer.options) or areas (colored by the fillColor attribute), depending on the chosen layer. The Documentation doesn't say anything about a layer's type (or at least I didn't find anything).. so is this possible?

I tried typeof layer (that's object) and layer.constructor (function e())..

Krxldfx
  • 709
  • 1
  • 10
  • 23

1 Answers1

1

You can loop over the objects in the layer and use instanceof:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof

For example:

myLayerGroup.eachLayer(function(layer) {
    if (layer instanceof L.Marker) {
        // it's a marker, do stuff
    }
    if (layer instanceof L.Polyline) {
        // it's a polyline, do stuff
    }
    if (layer instanceof L.Polygon) {
        // it's a polygon, do stuff
    }
});
iH8
  • 27,722
  • 4
  • 67
  • 76
  • Thanks! Just out of curiosity - is there anything that would return the class like `L.Polyline`? – Krxldfx Sep 10 '15 at 11:09
  • Not as far i know. You can try the options in this answer: http://stackoverflow.com/a/1249554/2019281 but to my knowledge they won't work on Leaflet's classes – iH8 Sep 13 '15 at 13:27