简体   繁体   English

OpenLayers 3:缩放/平移后未为未选择的功能调用样式功能

[英]OpenLayers 3: stylefunction is not called for unselected features after zoom/pan

I've prepared a fiddle for my problem: Fiddle 我为我的问题准备了一个小提琴: 小提琴

  1. Click on a blue circle to select it -> it becomes red 单击一个蓝色圆圈以将其选中->它变为红色
  2. Click on the other blue circle -> the original becomes blue (deselected) and the new one becomes red (selected) 单击另一个蓝色圆圈->原来的变成蓝色(取消选中),而新的变成红色(选中)

so far so good. 到现在为止还挺好。 Now if you instead do the following: 现在,如果您改为执行以下操作:

  1. Click on a blue circle -> it becomes red 单击一个蓝色圆圈->它变为红色
  2. Zoom out and back in -> it stays red 缩小并返回->保持红色
  3. Click on the other blue circle 点击另一个蓝色圆圈

-> both circles are red instead of deselecting the first! -> 两个圆圈均为红色,而不是取消选择第一个!

All I have is a selectInteraction with a styleFunction like this (I do some more things like text and caching the styles but the effect is the same): 我所拥有的只是一个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'
            })
        })
    })];
}

Why is my "deselected"-Style wrong after zooming or panning? 缩放或平移后为什么我的“取消选择”样式错误? As far as I can see when debugging the styleFunction the selection is correct (only contains 1 item), just the deselected item doesn't get styled correctly. 据我在调试styleFunction时看到的,选择正确(仅包含1个项目),只是取消选择的项目没有正确设置样式。

I use a styleFunction because no matter if selected or not, I'm setting the text and radius according to an object that is attached to the feature. 我使用styleFunction是因为无论是否选择,我都会根据附加到要素的对象来设置文本和半径。 So I calculate the style of a feature and I use the same styleFunction for selected and for deselected features. 因此,我计算特征的样式, styleFunction选定和styleFunction选定的特征使用相同的styleFunction


Edit: Solution 编辑:解决方案

I updated the Fiddle . 我更新了小提琴 Apparently you have to set the style of all deselected features to "null" (see Jonatas Walker's answer): 显然,您必须将所有取消选择的特征的样式设置为“ null”(请参阅​​Jonatas Walker的答案):

selectInteraction.on('select', function (evt) {
    if (evt) {
        $.each(evt.deselected, function (idx, feature) {
            feature.setStyle(null);
        });
    }
});

UPDATE - http://jsfiddle.net/jonataswalker/4vu669Lq/ 更新-http : //jsfiddle.net/jonataswalker/4vu669Lq/

Inside styleFunction 内部styleFunction

var fill_color = (this instanceof ol.Feature) ?
    [0, 0, 255, 1] : [255, 255, 255, 1];

And set the selected style on select listener: 并在选择侦听器上设置所选样式:

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);
        });
    }
});

This is because you are using the same styleFunction for both ol.layer.Vector and ol.select.Interaction . 这是因为你使用的是相同的styleFunction两个ol.layer.Vectorol.select.Interaction

So when you zoom in/out the vector layer reads the style from your styleFunction . 因此,放大/缩小时,矢量层将从styleFunction读取样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM