【发布时间】:2016-03-21 18:45:02
【问题描述】:
我正在将一个项目从 OL2 更新到 OL3,但我一直在纠结如何在更改特征样式后重绘特征。
在 OL2 中,这有效:
hidePoints: function(id) {
if (! this.getMap().center) {
return;
}
var i,
feature,
len = this.points.features.length;
if (id !== null) {
for( i = 0; i < len; i++ ) {
feature = this.points.features[i];
if (feature.attributes.aces_id == id) {
feature.style = null;
} else {
feature.style = { display: 'none' };
}
}
} else {
for( i = 0; i < len; i++ ) {
feature = this.points.features[i];
feature.style = { display: 'none' };
}
}
this.points.redraw();
},
在 OL3 中,我尝试更新函数以隐藏点图层,但 redraw() 不再存在,并且由于我正在使用的图层是 ol.layer.Vector,因此我找不到任何 updateParams 选项像其他除了向量之外的来源。调度事件和更改也不起作用。我希望改变会,但没有任何反应。
hidePoints: function(id) {
if (! this.getMap().getView().getCenter()) {
return;
}
var i,
feature,
layerSourceFeatures = this.pointsLayer.getSource().getFeatures(),
len = layerSourceFeatures.length;
if (id !== null) {
for( i = 0; i < len; i++ ) {
feature = this.pointsLayer.getSource().getFeatures()[i];
if (feature.get('aces_id') == id) {
feature.style = null;
} else {
feature.style = { display: 'none' };
}
}
} else {
for( i = 0; i < len; i++ ) {
feature = this.pointsLayer.getSource().getFeatures()[i];
feature.style = { display: 'none' };
}
}
//this.pointsLayer.redraw();
//this.pointsLayer.dispatchEvent(goog.events.EventType.CHANGE);
this.pointsLayer.changed();
},
我还想知道是否以这种方式更改样式(将每个功能获取到另一个 var),或者这是否不会仅更改该功能并保持原始不变。再加上总是获取getSource().getFeatures() 似乎对性能有辱骂......但我似乎找不到其他方法。
无论如何,现在如何在 OL3 中执行重绘以渲染样式已更改的特征?可以将图层设置为可见,但我不想一直隐藏/显示所有功能。有时我只想根据给定的 id 隐藏/显示一些。
【问题讨论】:
标签: javascript openlayers openlayers-3