簡體   English   中英

GetFeatures.clear() 和.push() 在 ol.interaction.Select 中沒有結果

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

我在我的代碼中使用 openlayers。 我想 select 單一功能,在這種情況下我使用ol.interaction.Select

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

在下一步中,我分配了事件selectInteraction.on('select', function(e) {...}

並且selectInteraction.getFeatures()方法工作正常並返回具有單個特征 object 的數組。 在代碼的另一部分,我想取消選擇此功能。 我發現我應該使用.clear()方法,但在我的情況下它不起作用:

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

我還發現getFeatures.push(feature)不起作用。 我沒有收到任何錯誤 - 只是沒有效果。

你有什么想法為什么它不起作用?

如果我理解正確,它應該像你提到的那樣工作。 看看我為你制作的這個例子,看看你在做什么。

 <: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