Openlayers 和捕获拖动事件
我正在使用 OpenLayers,我需要能够区分地图是由我自己的脚本还是由用户移动的。是的,我知道我可以使用 moveend。但当同一脚本根据 ajax 调用传入的数据移动或重新定位地图时,它也会触发。因此 moveend 或其他地图事件将不起作用。
我做了一些谷歌搜索,发现了 OpenLayers.Hander.Drag。但我所做的只是阻止用户拖动地图。
我的脚本:
this.dragger = new OpenLayers.Handler.Drag('',{
'dragStart': function(evt){
this.userdragged = true;
console.log('drag');
}},{
'map':this.mymap
});
this.dragger.activate();
如您所见,我尝试将 userdragged 变量设置为 true,以便稍后在 moveend 事件中使用相同的变量。不幸的是,这一切只是阻止我的地图被拖动。
有人可以帮助我吗?
艾伦
Im using OpenLayers and i need to be able to tell difference between when map has been moved by my own scrip or by user. Yeah im aware that i can use moveend. But it also triggers when the same script is moving or repositioning map based on incoming data from ajax calls. So moveend or other map events wont work.
I did some googling and found OpenLayers.Hander.Drag. But all that i managed with it was to stop users from dragging map.
My script:
this.dragger = new OpenLayers.Handler.Drag('',{
'dragStart': function(evt){
this.userdragged = true;
console.log('drag');
}},{
'map':this.mymap
});
this.dragger.activate();
As you can see, i tried to set userdragged variable to true to use this same variable in moveend event later. Unfortunately all this did was to stop my map from beeing draggable.
Can someone assist me please?
Alan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
知道了!
让它发挥作用的是:
Booyaka!
编辑:
实际上 this.userdragged 在那里不起作用...... this 的范围在那里是不同的。你需要做类似 var that = this; 的事情在该对象初始化之前并使用 that.userdragged = true....
Edit2:
我后来发现,这个 panMapDone 函数覆盖了 DragPans 自己的同名方法。仅以我之前的示例为例,当用户拖动地图时,您最终可能会得到导致矢量要素与地图不同步的地图。为了阻止这种情况发生,您也应该将原始功能复制到该函数中......使其看起来像这样:
Alan
Got it!
What got it working was :
Booyaka!
Edit:
Actually this.userdragged wont work in there... the scope of this is different there. you would need to do something like var that = this; before that object initialization and use that.userdragged = true....
Edit2:
I later found, that this panMapDone function overwrites DragPans own method which has same name. With just my previous example, you can end up with map that results in vector features going out of sync with map, when user drags map. To stop that from happening, you should copy the original functionality into that function too... to make it look something like that:
Alan
查看关于拖动处理程序的文档,它指出它应该与 Control 对象一起使用。你就这样用吗?也许代码片段没有显示它?
“如果在没有控件的情况下使用处理程序,则必须重写处理程序 setMap 方法才能正确处理地图。”
我还没有尝试过,但似乎你应该这样做:
Looking at the documentation on Drag handlers, it states that it's supposed to be used with a Control object. Are you using it that way? Maybe the code snippet doesn't show it?
"If a handler is being used without a control, the handlers setMap method must be overridden to deal properly with the map."
I haven't tried it, but it seems as you should go for something like this:
只是在此处发布一个示例,说明当用户拖动地图时执行任意功能,而不会干扰用于平移地图的正常单击拖动,因为此页面是我搜索如何执行此操作期间最常见的结果。
然后在创建地图实例时将控件添加到地图的控件列表中,或者使用map.addControl(),使用
Just posting an example here of executing an arbitrary function when the user drag the map, without interfering with the normal click-drag used to pan the map, because this page was the most frequent result during my search to find how to do that.
then add the control to you map's controls list when creating the map instance, or with a map.addControl(), with
我在 OpenLayers 6 中使用了它:
hf gl!
i have use that in OpenLayers 6:
hf gl!