繁体   English   中英

在OpenLayers中消除反跳功能选择

[英]Debounce feature selection in OpenLayers

我正在使用使用大量矢量特征的地图。 在某些浏览器中,OpenLayers处理pointermove事件交互时会存在很大的滞后。 例如:

function selectOnHover(map, handler, styleFn) {
    var selectMove = new ol.interaction.Select({
        condition: ol.events.condition.pointerMove,
        style: styleFn
    });

    map.addInteraction(selectMove);
    selectMove.on('select', handler);
}

在处理连续输入(例如,处理滚动事件)并需要大量处理的其他情况下,我通常会对事件的处理程序进行反跳处理,以便仅在输入暂停时才进行大量工作(在这种情况下,确定相交特征) 。 有没有一种方法可以在浏览器事件分发和OpenLayers检查交叉点之间插入反跳,而又不会避开OpenLayers交互处理?

我试过直接处理pointermove / mousemove事件,将它们反跳(重新调度手动创建的合成事件),然后使用交互条件仅处理合成事件。 除OpenLayers不会接收Internet Explorer的综合事件外,此方法行之有效。

我正在考虑规避OpenLayers交互-例如通过使用forEachFeatureAtPixel并手动更新样式。

实际上,即使使用标准API,您也可以将选择交互包装到自定义交互中,然后对handleEvent函数进行反抖动处理:

var app = {};
app.DebounceSelect = function() {
  this.selectInteraction = new ol.interaction.Select({
    condition: ol.events.condition.pointerMove
  });

  var handleEventDebounce = debounce(function(evt) {
    return ol.interaction.Select.handleEvent.call(this.selectInteraction, evt);
  }.bind(this), 100);

  ol.interaction.Interaction.call(this, {
    handleEvent: function(evt) {
        handleEventDebounce(evt);
      // always return true so that other interactions can
      // also process the event
      return true;
    }
  });
};
ol.inherits(app.DebounceSelect, ol.interaction.Interaction);

app.DebounceSelect.prototype.setMap = function(map) {
  this.selectInteraction.setMap(map);
};

var select = new app.DebounceSelect();
map.addInteraction(select);

http://jsfiddle.net/n9nbrye8/3/

作为参考,有关如何编写自定义交互的示例: http : //openlayers.org/en/master/examples/custom-interactions.html

以及ol.interaction.Select.handleEvent的文档

暂无
暂无

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

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