简体   繁体   English

OL3-如何知道是否开始绘图或交互

[英]OL3 - How to know if drawing or interaction is started

In OpenLayer 3 I create a Draw interaction using the 'draw-feature' sample code they have on their website . 在OpenLayer 3中,我使用他们网站上具有“ draw-feature”示例代码创建Draw交互。

The only difference is that I supply my own condition function to the Draw constructor. 唯一的区别是,我向Draw构造函数提供了自己的条件函数。

I would like to know if there is a way to determine within the condition function if the interaction/drawing has started? 我想知道是否有一种方法可以确定条件函数中的交互/绘图是否已经开始?

Basically my goal is to change the behavior slightly so drawing a box is initiated with a CTRL-click rather than a click. 基本上,我的目标是稍微改变行为,以便通过CTRL单击而不是单击来启动绘制框。 But ending the drawing can be done with a simple click. 但是,只需单击一下即可结束图形。 So my approach would be something like this (in TypeScript) 所以我的方法是这样的(在TypeScript中)

var condition = (e: ol.MapBrowserEvent): boolean => {
    return (myDraw.isStarted() ? true : e.originalEvent['ctrlKey']);
}

As far as I can see there's nothing like an isStarted() method in OL Draw class. 据我所知,在OL Draw类中没有像isStarted()方法那样的东西。 If I had access to internal members I would resolve it by checking the length of myDraw.sketchCoords_ (haven't checked this but if 0 the drawing is not started yet). 如果我可以访问内部成员,则可以通过检查myDraw.sketchCoords_的长度来解决此问题(尚未检查,但是如果为0,则绘图尚未开始)。 But I don't want to rely on private members, furthermore I'm using the minified version of OL where members names are transformed. 但是我不想依赖私有成员,而且我使用的是OL的缩小版本,其中成员名称已转换。

Try something like this: 尝试这样的事情:

var start_drawing = false;
function drawCondition(evt){
    var ctrl = ol.events.condition.platformModifierKeyOnly(evt);
    // this should be ol.events.condition.click
    // but for some reason always returns false
    var click = evt.type == 'pointerdown';

    // to finish draw with click
    if(start_drawing) return click;
    // start drawing only with Ctrl + click    
    return ctrl && click;
}
// draw is a reference to ol.interaction.Draw
draw.on('drawstart', function(evt){
    start_drawing = true;
});

draw.on('drawend', function(evt){
    start_drawing = false;
});

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

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