Leaflet Omnivore KML,风格导入路径

Mar*_*zio 4 javascript kml leaflet mapbox

我正在尝试导入多个kml文件并设置样式.那是我现在的代码:

  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);
Run Code Online (Sandbox Code Playgroud)

所有变量都正确实例化,kmls被转换并成功添加到地图中,但填充和描边颜色始终是默认的蓝色."c"var包含十六进制颜色代码.我错过了什么?

Mar*_*zio 6

我通过https://github.com/tmcw在github上得到了很好的帮助

setStyle代码必须像这样同步调用:

comune.on('ready', function() {
        this.setStyle({color: "#FF0000"});
    });
Run Code Online (Sandbox Code Playgroud)

小提琴上的完整示例:http://jsfiddle.net/oxdnpzcr/3/

  • 我不得不写`layer.setStyle`,在我的情况下`this`是事件而不是图层. (2认同)