Javascript 如何更改变量中的一个设置
我在地图上有一个圆形标记,它使用FillColor(共8种颜色),具体取决于显示距离从两个起点增加的值(Ankomsttid),距离起点越远,标记的红色越大 我的问题是,我还想根据列值“Plats”使用属性颜色(而不是fillColor)更改圆“外部颜色环”或边框,以区分和显示标记所属的起点(Plats)。所有标记始终使用8个“光谱填充颜色”,但需要根据本例中的(平面)值“S”或“H”更改边框 我是否需要编写一组新的代码,还是可以只更改变量中已经存在的“颜色:”部分?我需要的是改变“颜色:”-部分取决于属性“Plats”的值。例如,如果“Plats”有value=“S”,则更改为“color:'blue'”(而不是黑色),或者如果“Plats”有value=“H”,则更改为“color:'red'(而不是黑色)。这是代码,谢谢Javascript 如何更改变量中的一个设置,javascript,variables,leaflet,Javascript,Variables,Leaflet,我在地图上有一个圆形标记,它使用FillColor(共8种颜色),具体取决于显示距离从两个起点增加的值(Ankomsttid),距离起点越远,标记的红色越大 我的问题是,我还想根据列值“Plats”使用属性颜色(而不是fillColor)更改圆“外部颜色环”或边框,以区分和显示标记所属的起点(Plats)。所有标记始终使用8个“光谱填充颜色”,但需要根据本例中的(平面)值“S”或“H”更改边框 我是否需要编写一组新的代码,还是可以只更改变量中已经存在的“颜色:”部分?我需要的是改变“颜色:”-部
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;
}
pointToLayer:功能(特性,latlng){
var tid=feature.properties.Ankomsttid;
var pla=feature.properties.Plats;
var-avst;
//基圆对象
//因为fillColor是唯一不同的值
//预先定义所有其他的
函数avstCirkel(fillColor){
this.fillColor=fillColor
这个半径=5
this.fill=true
this.color=“#000000”
这个重量=1
该值为1.0
this.fillOpacity=1
}
//现在你可以轻松制作所有卷轴了
//它们可以用avstCirkels[0]引用,
//avstCirkels[1]等。。。
设avstCirkels=[
新avstCirkel(“#3288bd”),
新avstCirkel(“#66c2a5”),
新avstCirkel(“#abdda4”),
新avstCirkel('e6f598'),
新avstCirkel(“#fee08b”),
新avstCirkel(“#fdae61”),
新的avstCirkel(“#f46d43”),
新的avstCirkel(“#d53e4f”)
]
常量颜色={
S:“蓝色”,
H:“红色”
}
//forEach将遍历所有Cirkel并执行一个功能
avstCirkels.forEach(cirk=>cirk.color=colors[pla]| |“黑色”)
//console.log(“colors:,avstCirkels[1]。color,avstCirkels[2]。color)
//过滤器以更改圆形标记器上的填充颜色
如果(tid>0&&tid<201){
avst=新的L.circleMarker(latlng,avstCirkel[0]);
}
否则,如果(tid>200&&tid<401){
avst=新的L.circleMarker(latlng,avstCirkel[1]);
}
否则,如果(tid>400&&tid<601){
avst=新的L.circleMarker(latlng,avstCirkel[2]);
}
否则,如果(tid>600&&tid<801){
avst=新的L.circleMarker(latlng,avstCirkel[3]);
}
否则,如果(tid>800&&tid<1001){
avst=新的L.circleMarker(latlng,avstCirkel[4]);
}
否则,如果(tid>1000&&tid<1201){
avst=新的L.circleMarker(latlng,avstCirkel[5]);
}
否则,如果(tid>1200&&tid<1401){
avst=新的L.circleMarker(latlng,avstCirkel[6]);
}
否则,如果(tid>1400){
avst=新的L.circleMarker(latlng,avstCirkel[7]);
}
否则{
avst=null
}
返回avst;
}
编辑我实际上发现了我的错误,当引用avstCirkel时,我忘记了“s”应该是“avstCirkels[0]”而不是“avstCirkel[0],现在情况好多了:)根据需要设置颜色的时间,有很多方法可以做到这一点。如果在定义圆时知道
Plats
的值,则可以在定义中使用它:
//将平台值映射到颜色:
让pla='S'
常量颜色={
S:“蓝色”,
H:“红色”
}
var avstCirkel_1={
半径:5,
填充:是的,
填充颜色:“#3288bd”,
颜色:颜色[pla]| |'#000000',//如果有颜色映射,则将使用pla,否则为黑色
体重:1,
不透明度:1,
不透明度:1
};
console.log(“circle color:,avstCirkel_1.color)
您可以直接更改对象的属性,即:avstCirkel_8.color=#abab12
这就是您的意思吗?如果满足标准,我必须更改所有8种颜色,因此不仅仅是一种avstCirkel。如果不符合标准,则将使用默认的黑色。我在想,是否有一种“分组”标记并立即更改它们,或者是否有更好的解决方案来执行此操作…非常详细的答案,非常感谢。我知道数组是正确的选择,但我不知道它是否正确,运行您的建议会将所有标记都变成蓝色默认值,我做错了一些事情。。。我确实用更新编辑了这篇文章…@QGIS,Abe所有标记都变为红色,因为pla='H'
。如果planet=“S”
所有颜色都是蓝色,如果pal=undefined
或其他值,所有颜色都是黑色。我以为你想“如果符合标准,就改变所有8种颜色”。对不起,不够清楚。我会尽我最大的努力编辑帖子,因为现在填充颜色是空白的,外环都是蓝色的,但它应该是蓝色和红色的混合,因为Plats有更多的值,而只有S和填充颜色不应该改变