1

I'm trying to set a different divIcon for each point on a leaflet geoJson layer. I have tried everything under the sun but it just doesn't work for me. This is what I'm doing

geoJsonLayer = L.geoJson(null, {
  pointToLayer: function(feature, latlng) {
    var smallIcon = L.DivIcon.extend({
      options: {
        iconSize: [27, 27],
        html: "<div>" + feature.properties.FEATURE_STYLE.SVG_ELEMENT + "</div>"
      }
    });
    return L.marker(latlng, {icon: new smallIcon()});
  },      
  style: getLayerStyle,
  onEachFeature: setFeatureProperties,
});
geoJsonLayer.addTo(baseMap);

feature.properties.FEATURE_STYLE.SVG_ELEMENT is an html <svg> containing the icon.

The icons are displayed ok, but every feature display the same icon.

I've also tried doing the following:

  1. using L.Icon with different .png in iconUrl for each feature
  2. using L.circleMarker with different colors for each feature

They both works as expected (different color / icon per feature). But I can't seem to get the divIcon to display differently for each feature.

Anyone have idea why this is the case?

Thanks in advance.

UPDATE:

This is what feature.properties.FEATURE_STYLE.SVG_ELEMENT looks like

3
  • Please include a small code (like your SVG) that still reproduces the issue, in the body of your question. See MCVE.
    – ghybs
    Commented Apr 4, 2017 at 4:29
  • the svg that I'm using is in the pastebin link above: "This is what feature.properties.FEATURE_STYLE.SVG_ELEMENT looks like". It's too long for me to include in a code tag. Thanks.
    – TaraC
    Commented Apr 4, 2017 at 16:04
  • Consider refactoring your SVG to get the minimum that still reproduces your issue. I am sure you might discover things by doing so.
    – ghybs
    Commented Apr 4, 2017 at 16:42

1 Answer 1

1

Your code to instantiate a new L.divIcon is more complicated than really necessary, but it works, not considering the SVG part:

https://jsfiddle.net/3v7hd2vx/236/

That being said, please note that:

  • style option is used for vector layers. Therefore in the case of Point features that are rendered as L.divIcon's, it is not used.
  • onEachFeature option is applied after the pointToLayer one, because the latter is needed to create the layer that is fed to onEachFeature. Therefore if you build the feature.properties.FEATURE_STYLE.SVG_ELEMENT in there (as the name of your function setFeatureProperties suggests), it is too late.

If you need further help, you would very probably need to share more code, e.g. the style and onEachFeature options, and some sample data, in particular with feature.properties.FEATURE_STYLE.SVG_ELEMENT.

1
  • Thanks for your quick response. Style is just setting default size, color, opacity in case we're not using icons as marker. and we use onEachFeature to bind popup and tooltip, it does not control any marker in there. I've updated an example to the SVG_ELEMENT above. The problem is that when I use L.icon and pass a .png image in, it works fine. But L.divIcon updates every marker already on the map.
    – TaraC
    Commented Apr 4, 2017 at 0:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.