我有一些标记根据数据库中的一些数字着色。我的脚本会定期执行询问我的号码的查询。如果满足某些条件,我希望我的标记改变颜色,例如从绿色变为黄色。
一个类似的问题是这些标记的半径在缩放地图时应该增加。我通过更新更改缩放时唤醒的侦听器中的半径解决了这个问题。
var currZoom = map.getView().getZoom();
map.on('moveend', function(e) {
var newZoom = map.getView().getZoom();
if (currZoom != newZoom) {
console.log('zoom end, new zoom: ' + newZoom);
currZoom = newZoom;
vectorSource.clear();
var greenStyle = new ol.style.Style({
image: new ol.style.Circle({
radius: Math.pow(33,newZoom/5)/15000,
fill: new ol.style.Fill({color: 'rgba(0,255,0,0.5)'}),
stroke: new ol.style.Stroke({
color: 'green', width: 1})
})
});
var yellowStyle = new ol.style.Style({
image: new ol.style.Circle({
radius: Math.pow(33,newZoom/5)/15000,
fill: new ol.style.Fill({color: 'rgba(255,255,0,0.5)'}),
stroke: new ol.style.Stroke({
color: 'yellow', width: 1})
})
});
var redStyle = new ol.style.Style({
image: new ol.style.Circle({
radius: Math.pow(33,newZoom/5)/15000,
fill: new ol.style.Fill({color: 'rgba(255,0,0,0.5)'}),
stroke: new ol.style.Stroke({
color: 'red', width: 1})
})
});
for(var i=0;i<Features.length;i++){
var oldcolor = Features[i]["g"]["e"]["a"]["a"];
if(oldcolor=="green" || oldcolor=="yellow" || oldcolor=="red"){
if(oldcolor=="green"){
Features[i].setStyle(greenStyle);
}
else if(oldcolor=="yellow"){
Features[i].setStyle(yellowStyle);
}
}
});
这有效。当我缩放半径时,它的值会发生变化,而圆圈的尺寸也会发生变化。
我希望这会起作用,但它只会删除我的标记。如果我放大或缩小,它们会再次出现(因为缩放侦听器有效)。
相关分类