Search code examples
javascriptkmlleafletmapbox

Leaflet Omnivore KML, style imported paths


I'm trying to import multiple kml files and style them. That is my code as now:

  var comuni = new Array();
  for (var i = nearby.comuni.length - 1; i >= 0; i--) {
    var c = nearby.comuni[i].colore;
    comune = omnivore.kml(nearby.comuni[i].kml);
    comune.setStyle({color: c});
    comuni.push(comune);
  };
  var comuniLayer = L.layerGroup(comuni);

All variables are instanced correctly, the kmls are converted and successfully added to the map, but the fill and stroke colors are always the default blue. The "c" var contains an hex color code. What am I missing?


Solution

  • I was kindly helped on github by https://github.com/tmcw

    The setStyle code had to be called synchrounsly like this:

    comune.on('ready', function() {
            this.setStyle({color: "#FF0000"});
        });
    

    Full example on fiddle: http://jsfiddle.net/oxdnpzcr/3/