简体   繁体   English

无法在 openlayer3 中两次选择相同的线层

[英]Not able to select the same line layer twice in openlayer3

I have drawn a line String feature and setting style for that and adding id for that line string it was showing.我已经为它绘制了一个线串特征和设置样式,并为它显示的那个线串添加了 id。 when it comes to the select it is now working as expected.当涉及到选择时,它现在按预期工作。

step-1: when i select for first time using map interaction i am able to select and get the data and apply the style to the line string.第 1 步:当我第一次使用地图交互进行选择时,我能够选择并获取数据并将样式应用于线串。

step-2: i am selecting again same line string which is not allowing to select and not responding at all if i select some that line string it is able select same selected is not selecting.第 2 步:我再次选择相同的行字符串,如果我选择一些它可以选择的行字符串,则它不允许选择并且根本不响应。

below is my map interaction code下面是我的地图交互代码

var selectionStyle=styleForSelection();
        var ifCondition="";         
        $.each(ispGeomResponse,function(key,value){
            ifCondition+="(layer.get('name')==\""+value.name+"\") || ";         
        });
        if(ifCondition)
            ifCondition=ifCondition.substring(0,ifCondition.lastIndexOf(" || "));
        selectInteraction= new ol.interaction.Select({
            hitTolerance:10,
            layers: function (layer) {
                if(eval(ifCondition)){
                    return layer;
                }
            },

        });

  map.addInteraction(selectInteraction);

selectInteraction.on('select', function(event){
            var selectFeature=event.selected[0];
            if(selectFeature!=undefined){
                   //some logic i am processing
            }   
        });

my expected output is i need to select the same line string twice.我的预期输出是我需要选择相同的行字符串两次。 if it is already selected no need apply style i will write some custom logic if it is already selected.如果它已经被选中,则不需要应用样式,如果它已经被选中,我将编写一些自定义逻辑。

You cannot select a feature which is already selected.您不能选择已选择的功能。 If you wish to change the style on each click you could use the map's click event如果您希望在每次点击时更改样式,您可以使用地图的点击事件

 var styles = [ new ol.style.Style({ fill: new ol.style.Fill({ color: 'rgba(255, 255, 255, 0.5)' }), stroke: new ol.style.Stroke({ color: '#319FD3', width: 1 }) }), new ol.style.Style({ fill: new ol.style.Fill({ color: 'rgba(255, 0, 0, 0.5)' }), stroke: new ol.style.Stroke({ color: '#319FD3', width: 1 }) }), new ol.style.Style({ fill: new ol.style.Fill({ color: 'rgba(0, 255, 0, 0.5)' }), stroke: new ol.style.Stroke({ color: '#319FD3', width: 1 }) }), new ol.style.Style({ fill: new ol.style.Fill({ color: 'rgba(0, 0, 255, 0.5)' }), stroke: new ol.style.Stroke({ color: '#319FD3', width: 1 }) }) ]; var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector({ url: 'https://openlayers.org/en/v4.6.5/examples/data/geojson/countries.geojson', format: new ol.format.GeoJSON() }), style: styles[0] }); var map = new ol.Map({ layers: [vectorLayer], target: 'map', view: new ol.View({ center: [0, 0], zoom: 1 }) }); var updateStyle = function(pixel) { var feature = map.forEachFeatureAtPixel(pixel, function(feature) { return feature; }); if (feature) { var styleIndex = 1; if (feature.getStyle() === styles[1]) { styleIndex = 2; } else if (feature.getStyle() === styles[2]) { styleIndex = 3; } feature.setStyle(styles[styleIndex]) } } map.on('click', function(evt) { updateStyle(evt.pixel); });
 html, body, .map { margin: 0; padding: 0; width: 100%; height: 100%; }
 <link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css"> <script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script> <div id="map" class="map"></div>

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

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