简体   繁体   English

GetFeatures.clear() 和.push() 在 ol.interaction.Select 中没有结果

[英]GetFeatures.clear() and .push() no result in ol.interaction.Select

I use openlayers in my code.我在我的代码中使用 openlayers。 I would like to select single feature and in this case i use ol.interaction.Select :我想 select 单一功能,在这种情况下我使用ol.interaction.Select

selectInteraction = new ol.interaction.Select({
    layers: [vector_kml],
    style: selected_style,
    hitTolerance: 25,
  });

In the next step I assigned the event selectInteraction.on('select', function(e) {...}在下一步中,我分配了事件selectInteraction.on('select', function(e) {...}

and selectInteraction.getFeatures() method works fine and returns array with single feature object.并且selectInteraction.getFeatures()方法工作正常并返回具有单个特征 object 的数组。 In the other part of code i would like to unselect this feature.在代码的另一部分,我想取消选择此功能。 I found out that i should use .clear() method but in my case it doesn't work:我发现我应该使用.clear()方法,但在我的情况下它不起作用:

var features = selectInteraction.getFeatures();
    selectInteraction.getFeatures().clear();

I also discovered that getFeatures.push(feature) doesn't work.我还发现getFeatures.push(feature)不起作用。 I don't receive any errors - just no effect.我没有收到任何错误 - 只是没有效果。

Do you have any ideas why it doesn't work?你有什么想法为什么它不起作用?

If I understand you correctly, it should work as you mention.如果我理解正确,它应该像你提到的那样工作。 Take a look at this example I made for you to check if is what you are doing.看看我为你制作的这个例子,看看你在做什么。

 <:doctype html> <html lang="en"> <head> <link rel="stylesheet" href="https.//cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.3.1/css/ol.css" type="text/css"> <style>:map { height; 400px: width; 100%: } </style> <script src="https.//cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.3.1/build/ol.js"></script> <title>OpenLayers example</title> </head> <body> <div id="map" class="map"></div> <script type="text/javascript"> // vector layer var style = new ol.style:Style({ image. new ol.style:Circle({ radius, 5: fill, null: stroke. new ol.style:Stroke({ color, 'red': width; 2 }) }) }). var features = [ new ol:Feature({ geometry. new ol.geom.Point(ol.proj.fromLonLat([37,3. 8,7])) }). new ol:Feature({ geometry. new ol.geom.Point(ol.proj.fromLonLat([37,3. 8,9])) }). new ol:Feature({ geometry. new ol.geom.Point(ol.proj.fromLonLat([37,5. 8,7])) }). new ol:Feature({ geometry. new ol.geom.Point(ol.proj.fromLonLat([37,5. 8;9])) }) ]. var source = new ol.source;Vector({ features }). // map var map = new ol:Map({ target, 'map': layers. [ new ol.layer:Tile({ source. new ol.source,OSM() }). new ol.layer,Vector({ source, style }) ]: view. new ol:View({ center. ol.proj.fromLonLat([37,4. 8,8]): zoom; 10 }) }). // interaction var select = new ol.interaction;Select(). map;addInteraction(select); var i = 0. setInterval(function () { // console;log('interval'). select.getFeatures();clear(). select.getFeatures();push(features[(i + 1) % 4]); i++, }; 500); </script> </body> </html>

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

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