简体   繁体   中英

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 .

The only difference is that I supply my own condition function to the Draw constructor.

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. But ending the drawing can be done with a simple click. So my approach would be something like this (in 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. 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). But I don't want to rely on private members, furthermore I'm using the minified version of OL where members names are transformed.

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;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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