【问题标题】:openlayers 3 set sensitivity on drag mapopenlayers 3 在拖动地图上设置灵敏度
【发布时间】:2017-04-29 08:01:57
【问题描述】:

我正在尝试在 ol3 考虑单击事件以移动事件之前设置一个限制像素,代码见下文。我做错了,因为我的代码无法再拖动地图了。

  window.app   = {};
  app.Drag = function() {
    ol.interaction.Pointer.call(this, {
      handleDownEvent: app.Drag.prototype.handleDownEvent,
      handleUpEvent: app.Drag.prototype.handleUpEvent
    });
  }

  ol.inherits(app.Drag, ol.interaction.Pointer);
  app.Drag.prototype.handleDownEvent = function(evt) {
    app.toD=evt.pixel;
    console.log('Down');
    return true;
  };

  app.Drag.prototype.handleUpEvent = function(evt) {
    app.toU=evt.pixel;
    console.log('up');
    delta=Math.sqrt(Math.pow(Math.abs(app.toU[0]-app.toD[0]),2)+Math.pow(Math.abs(app.toU[1]-app.toD[1]),2))
    if (delta<10) {
      console.log('Click event');
      var event = document.createEvent("MouseEvents");
      event.initMouseEvent("click", true, true, window,0, app.toD[0], app.toD[1], app.toD[0], app.toD[1], false, false, false, false, 0, null);
      map.dispatchEvent(event.initMouseEvent);
    }
    console.log(delta);
  };

  map.getInteractions().extend([new app.Drag()])

提前感谢您的帮助。

【问题讨论】:

    标签: openlayers-3 drag threshold


    【解决方案1】:

    我采取另一种方式,对我有好处:

    if(typeof limit=='undefined')limit=10;
    map.on('pointerdown', function(evt){
      console.log('down')
      app.toD=evt.pixel;
    })
    map.on('pointerup', function(evt){
      app.toU=evt.pixel;
      console.log('up');
      delta=Math.sqrt(Math.pow(Math.abs(app.toU[0]-app.toD[0]),2)+Math.pow(Math.abs(app.toU[1]-app.toD[1]),2))
      if (delta<limit) {
        console.log('Click event');
        var event = document.createEvent("MouseEvents");
        var x= app.toD[0]+((app.toD[0]-app.toU[0])/2).toFixed(0);
        var y= app.toD[1]+((app.toD[1]-app.toU[1])/2).toFixed(0);
        event.initMouseEvent("click", true, true, window,0, x, y, x, y, false, false, false, false, 0, null);
        event.pixel = [x, y];
        map.dispatchEvent(event);
      }
      console.log(delta);
    })
    

    【讨论】:

    • 感谢@Slayes 的代码。通过两个小的修改,它完全符合我的要求(对于一个古老的项目:))。我编辑了您的代码 sn-p 以反映这一点,并希望它们是一般正确的修改(添加像素属性 + 调度事件而不是 event.initMouseEvent 函数)。
    【解决方案2】:

    从 OpenLayers 4.2.0 开始,您可以将 moveTolerance 设置为 ol.Map 的选项,这正是您所要求的。

    ol.Mapdocumentation.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多