[英]OpenLayers 3: stylefunction is not called for unselected features after zoom/pan
我為我的問題准備了一個小提琴: 小提琴
到現在為止還挺好。 現在,如果您改為執行以下操作:
-> 兩個圓圈均為紅色,而不是取消選擇第一個!
我所擁有的只是一個selectInteraction
和一個styleFunction
這樣的功能(我做了更多的事情,例如文本和樣式緩存,但效果是一樣的):
function styleFunction(feature, resolution) {
var selected = selectInteraction.getFeatures().getArray().indexOf(feature) >= 0;
return [
new ol.style.Style({
image: new ol.style.Circle({
radius: 30,
stroke: new ol.style.Stroke({
color: 'blue',
width: 1
}),
fill: new ol.style.Fill({
color: selected ? [255, 0, 0, 1] : [0, 0, 255, 1]
})
}),
text: new ol.style.Text({
text: 'Test',
fill: new ol.style.Fill({
color: 'black'
})
})
})];
}
縮放或平移后為什么我的“取消選擇”樣式錯誤? 據我在調試styleFunction
時看到的,選擇正確(僅包含1個項目),只是取消選擇的項目沒有正確設置樣式。
我使用styleFunction
是因為無論是否選擇,我都會根據附加到要素的對象來設置文本和半徑。 因此,我計算特征的樣式, styleFunction
選定和styleFunction
選定的特征使用相同的styleFunction
。
我更新了小提琴 。 顯然,您必須將所有取消選擇的特征的樣式設置為“ null”(請參閱Jonatas Walker的答案):
selectInteraction.on('select', function (evt) {
if (evt) {
$.each(evt.deselected, function (idx, feature) {
feature.setStyle(null);
});
}
});
更新-http : //jsfiddle.net/jonataswalker/4vu669Lq/
內部styleFunction
var fill_color = (this instanceof ol.Feature) ?
[0, 0, 255, 1] : [255, 255, 255, 1];
並在選擇偵聽器上設置所選樣式:
selectInteraction.on('select', function(evt){
var selected = evt.selected;
var deselected = evt.deselected;
if (selected.length) {
selected.forEach(function(feature){
feature.setStyle(styleFunction);
});
}
if (deselected.length) {
deselected.forEach(function(feature){
feature.setStyle(null);
});
}
});
這是因為你使用的是相同的styleFunction
兩個ol.layer.Vector
和ol.select.Interaction
。
因此,放大/縮小時,矢量層將從styleFunction
讀取樣式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.