我尝试导入多个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);
所有变量都已正确实例化,将kmls转换并成功添加到地图中,但填充和描边颜色始终为默认蓝色。 " c" var包含十六进制颜色代码。我错过了什么?
答案 0 :(得分:6)
setStyle代码必须像这样同步调用:
comune.on('ready', function() {
this.setStyle({color: "#FF0000"});
});
小提琴的完整示例:http://jsfiddle.net/oxdnpzcr/3/
答案 1 :(得分:0)
//using leaflet
function intialthree(file)
{
var highlightStyle1 = {
fillColor: "00FFFFFF",
weight: 1,
opacity: 1,
color: "#000000",
fillOpacity:0.0
};
city = new L.KML("IndiaKML/"+file, {async: true });
city.on("loaded", function(e) {
this.setStyle(highlightStyle1);
map.fitBounds(e.target.getBounds());
});
map.addLayer(city);
}