简体   繁体   English

Openlayers 4:更改所选要素的绘制顺序

[英]Openlayers 4: Changing the draw order of selected features

I have a single vector layer in OpenLayers 4 (4.4.1). 我在OpenLayers 4(4.4.1)中有一个矢量图层。 The layer has several features with LineString geometries. 该图层具有多个具有LineString几何的特征。 Some of the features overlap. 一些功能重叠。

If I click at a point where features overlap, I want only one of the features to be drawn as selected. 如果我在要素重叠的点处单击,我只希望将其中一个要素绘制为选中。 The others should still be available for selection later (by feature id in a separate UI selection list). 其他应该仍然可供以后选择(通过单独的UI选择列表中的功能ID)。

If I click on another feature id (in the separate UI selection List) that feature should be drawn as selected, and the previously selected should not be drawn as selected, but still available in the selection list. 如果我单击另一个要素ID(在单独的UI选择列表中),该要素应绘制为选中,并且先前选择的不应绘制为选中,但仍可在选择列表中使用。

This works, but it is only the first (default) selected feature that seems to be drawn at the top. 这是有效的,但它只是第一个(默认)选定的功能,似乎在顶部绘制。

Image below shows when feature id 10049 is marked as selected. 下图显示了功能ID 10049被标记为已选中的情况。 选择中的第一个元素是正确绘制的

Image below shows when feature id 10048 is marked as selected. 下图显示了功能ID 10048被标记为已选中的情况。 选择中的第二个元素绘制在第一个元素下面

If I click somewhere on the southmost feature where they do not overlap, it is drawn correctly as selected on top. 如果我点击它们不重叠的最南部特征上的某个位置,则会在顶部选中时正确绘制。

如果在没有重叠的情况下单击,则会正确绘制最南端的要素

To keep track of the feature that needs to be visually selected there is a variable: 要跟踪需要在视觉上选择的功能,有一个变量:

var multiselectPrimaryId = 0;

I use the following selectInteraction code: 我使用以下selectInteraction代码:

selectInteraction.on('select', function (e) {
    e.deselected.forEach(function (feature) {
        feature.setStyle(null);
    });
    if (e.selected.length <= 1) {
        $("#multipleHitWindow").hide();
        multiselectPrimaryId = 0;
        if (e.selected.length == 0) {
            return;
        }
    }
    var features = e.selected;
    if (multiselectPrimaryId == 0) {
        multiselectPrimaryId = features[0].getId();
    }
    if (features.length > 1) {
        var text = "Multiple hits: ";
        features.forEach(function (elem, index, array) {
            text += "<a href='javascript:changeSelectedFeature("
                 + elem.getId() + ")'>" + elem.getId() + "</a>  &nbsp;";
            if (elem.getId() == multiselectPrimaryId) {
                elem.setStyle(selectedStyleFunction);
            }
        });
        $('#multipleHit').html(text);
        $("#multipleHitWindow").show();
    }
    else {
        features[0].setStyle(selectedStyleFunction);
    }
});

And I call this function from a dynamically created list of link: 我从动态创建的链接列表中调用此函数:

function changeSelectedFeature(id) {
    multiselectPrimaryId = id;
    var featuresArray = selectInteraction.getFeatures().getArray().slice(0);
    var event = new ol.interaction.Select.Event()
    event.deselected = featuresArray;
    event.selected = featuresArray;
    event.type = 'select';
    event.target = selectInteraction;
    event.mapBrowserEvent = map.mapBrowserEventHandler_;
    selectInteraction.dispatchEvent(event);
}

How can I get the one with selectedStyle set to be drawn at the top? 如何将selectedStyle设置为在顶部绘制? I have tried to add a zIndex to the selectedStyle. 我试图将zIndex添加到selectedStyle。 But it does not seem to have any effect. 但它似乎没有任何影响。

Here is a JsFiddle: https://jsfiddle.net/5j6c6mgo/7/ . 这是一个JsFiddle: https ://jsfiddle.net/5j6c6mgo/7/。 There are some other minor issues with the selection, but hopefully you will able to see the behaviour that I described above. 选择还有一些其他小问题,但希望您能够看到我上面描述的行为。

I have a single vector layer ... The layer has several features with LineString geometries. 我有一个矢量图层...图层有几个带有LineString几何图形的特征​​。 Some of the features overlap. 一些功能重叠。

I think you would need the LineString geometries to be in separate Layers for you to be able to use 'zIndex' - you would do this by calling 'setZIndex' on the layer in question. 我认为您需要将LineString几何图形放在单独的图层中才能使用'zIndex' - 您可以通过在相关图层上调用'setZIndex'来完成此操作。 This will easily allow you to set the draw order at runtime . 这将允许您在运行时设置绘制顺序

Other than that the vectors are going to be displayed in their initial draw order and short of redrawing changing their draw order isn't possible. 除此之外,矢量将以其初始绘制顺序显示,并且缺少重绘以改变其绘制顺序是不可能的。

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

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