如何更改 geojson 文件中已加载点的样式?
How to change style of already loaded points from geojson file?
我创建了一个脚本,它从 geojson 文件中加载特征:
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [5.53204822887795, 51.609252234043296, 0.0]
}
}, etc...
然后我使用样式函数在这些点上创建具有不同颜色和半径大小的圆:
var styleFunction = function(feature) {
var style = new ol.style.Style({
image: new ol.style.Circle({
radius: calculateRadius(feature),
fill: new ol.style.Fill({
color: 'rgba(255, 0, 0, 0.5)'
}),
stroke: null
})
});
return style;
};
我想更改这些功能的样式,但对于我来说,我无法访问样式。我可以访问坐标(我猜是因为它们是点),但是获取半径或颜色似乎是不可能的。
有什么建议吗?
在您的样式函数中,尝试将 return style;
更改为 return [style];
。
在这里查看我的 post
由于每个特征都通过样式函数,您还可以向 geojson 添加可以在样式函数中读取的属性。如果您想根据要素的 属性 做其他事情,例如制作正方形或放大地图要素,这非常好。
我创建了一个脚本,它从 geojson 文件中加载特征:
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [5.53204822887795, 51.609252234043296, 0.0]
}
}, etc...
然后我使用样式函数在这些点上创建具有不同颜色和半径大小的圆:
var styleFunction = function(feature) {
var style = new ol.style.Style({
image: new ol.style.Circle({
radius: calculateRadius(feature),
fill: new ol.style.Fill({
color: 'rgba(255, 0, 0, 0.5)'
}),
stroke: null
})
});
return style;
};
我想更改这些功能的样式,但对于我来说,我无法访问样式。我可以访问坐标(我猜是因为它们是点),但是获取半径或颜色似乎是不可能的。
有什么建议吗?
在您的样式函数中,尝试将 return style;
更改为 return [style];
。
在这里查看我的 post
由于每个特征都通过样式函数,您还可以向 geojson 添加可以在样式函数中读取的属性。如果您想根据要素的 属性 做其他事情,例如制作正方形或放大地图要素,这非常好。