【发布时间】:2018-08-08 14:51:21
【问题描述】:
我在地图上有圆形标记,它使用 fillColors(总共 8 种颜色),具体取决于哪个值 (Ankomsttid) 必须显示从两个起点开始的距离如何增加,离起点越远,标记越红是。
我的问题是我还想使用属性颜色更改圆圈“外部颜色环”或边框:(不是填充颜色)取决于列值“Plats”以区分并显示标记所属的起点(Plats)到。所有标记始终使用 8 个“光谱填充颜色”,但需要根据此示例中的(Plats)值“S”或“H”更改边框。
我是否需要编写一组新代码,或者我可以只更改变量中已经存在的“颜色:”部分吗?我需要的是根据“Plats”的属性值更改“颜色:”部分。例如,如果“Plats”的值为“S”,则更改为“color: 'blue'”(而不是黑色),或者如果“Plats”的值为“H”,则更改为“color:'red'(而不是黑色)。这是代码,谢谢...
pointToLayer: function(feature, latlng) {
var tid = feature.properties.Ankomsttid;
var pla = feature.properties.Plats;
var avst;
// base Circkel object
// Since fillColor is the only different value
// predefine all the others
function avstCirkel(fillColor){
this.fillColor = fillColor
this.radius = 5
this.fill = true
this.color = "#000000"
this.weight = 1
this.opacity = 1
this.fillOpacity = 1
}
// Now you can make all your cirkels easily
// and they can be referenced with avstCirkels[0],
// avstCirkels[1], etc...
let avstCirkels = [
new avstCirkel('#3288bd'),
new avstCirkel('#66c2a5'),
new avstCirkel('#abdda4'),
new avstCirkel('#e6f598'),
new avstCirkel('#fee08b'),
new avstCirkel('#fdae61'),
new avstCirkel('#f46d43'),
new avstCirkel('#d53e4f')
]
const colors = {
S: "blue",
H: "red"
}
// forEach will loop through all the cirkels and perform a function
avstCirkels.forEach(cirk => cirk.color = colors[pla] || "black")
//console.log("colors: ", avstCirkels[1].color, avstCirkels[2].color)
// Filter to change the fillColor on circleMarkers
if (tid > 0 && tid < 201){
avst = new L.circleMarker(latlng, avstCirkel[0]);
}
else if (tid > 200 && tid < 401){
avst = new L.circleMarker(latlng, avstCirkel[1]);
}
else if (tid > 400 && tid < 601){
avst = new L.circleMarker(latlng, avstCirkel[2]);
}
else if (tid > 600 && tid < 801){
avst = new L.circleMarker(latlng, avstCirkel[3]);
}
else if (tid > 800 && tid < 1001){
avst = new L.circleMarker(latlng, avstCirkel[4]);
}
else if (tid > 1000 && tid < 1201){
avst = new L.circleMarker(latlng, avstCirkel[5]);
}
else if (tid > 1200 && tid < 1401){
avst = new L.circleMarker(latlng, avstCirkel[6]);
}
else if (tid > 1400){
avst = new L.circleMarker(latlng, avstCirkel[7]);
}
else {
avst = null
}
return avst;
}
编辑我实际上发现了我的错误,在引用 avstCirkel 时我忘记了“s”应该读为“avstCirkels[0]”而不是“avstCirkel[0]”,现在情况变得更好了 :)
【问题讨论】:
-
您可以直接更改对象的属性,即:
avstCirkel_8.color = #abab12是您的意思吗? -
如果满足条件,我必须更改所有 8 种颜色,因此不仅适用于一个 avstCirkel。如果不满足条件,它将使用默认的黑色。我在想是否有一种“分组”标记并立即更改它们,或者是否有更好的解决方案......
标签: javascript variables leaflet