2014-10-09 62 views
3

我試圖導入多個kml文件並對它們進行樣式設置。這是我的代碼和現在一樣:單張Omnivore 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); 

所有變量都實例化正確的KML檔案轉換併成功添加到地圖,但填充和筆觸顏色總是默認的藍色。 「c」var包含一個十六進制顏色代碼。我錯過了什麼?

回答

5

我被https://github.com/tmcw

好心幫助在GitHub上的setStyle代碼必須synchrounsly這樣調用:http://jsfiddle.net/oxdnpzcr/3/

+1

我不得不寫:

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

上撥弄完整的示例'layer.setStyle',在我的例子中'this'是事件而不是圖層。 – nathanvda 2015-03-31 14:52:10

+1

感謝您關注馬立克,非常有幫助 – JasTonAChair 2015-09-14 04:00:21

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); 
}