[英]How to customize leaflet style function to get color depending on properties values
我在 geojson 屬性上有一個名為 length 的維度。 我有一個下拉列表,例如想要按長度過濾並獲得不同的圖層顏色。
function getColor_by_length(d){
return d > 9 ? '#800026' :
d > 5 ? '#BD0026' :
d > 3 ? '#E31A1C' :
d > 0 ? '#FC4E2A' :
'#FFEDA0';
}
function linkDropDown(){
var linkSelector = document.getElementById('linkSelector')
if (linkSelector.value == "length"){
geo_json_layer.eachLayer(function (layer) {
layer.setStyle({fillColor: getColor_by_length(layer.feature.properties.length)})
});
}
我的所有圖層總是只有一種相同的顏色。 我想不出我錯在哪里。 請問有什么想法嗎?
getColor_by_length
實際上不返回任何東西。 調用getColor_by_length(something)
總是undefined
。 你需要這樣做:
function getColor_by_length(d){
return d > 9 ? '#800026' : // <----- return the value
d > 5 ? '#BD0026' :
d > 3 ? '#E31A1C' :
d > 0 ? '#FC4E2A' :
'#FFEDA0';
}
那應該為你做。 假設d
的參數就是你認為的那樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.