7

I have a vector layer in OL3 which I want to auto clear when drawing a Point feature (which means there is always max 1 feature in the layer).

Problem is that when I listen to drawstart (ol.interaction.draw) or to featureadd (ol.layer.vector) they are fired after feature is added and thus I can't clear the layer without them be deleted.

how can I workaround this ?

* Edit * I've noticed the drawstart event work when the draw type isn't Point , but I want to add points and thus my problem.

1 Answer 1

12

Points are a bit tricky because the drawstart and drawend events are driven from the same mouse click. To support all geometry types, have a look at the sample code below.

If you only ever need to support the ol.geom.Point type, then just use the drawend event and don't worry about the rest.

Sample Code

var geomType, lastFeature;

var source = new ol.source.Vector({
    wrapX: false
});

var draw = new ol.interaction.Draw({
    source: source,
    type: geomType
});

// removes the last feature from the vector source.
var removeLastFeature = function() {
    if (lastFeature)
      source.removeFeature(lastFeature);
};

// On draw start for polygon, line and circle types we need to 
// remove the previous feature so that it doesn't show.
draw.on('drawstart', function (e) {

    // Filter on all BUT points
    if (geomType !== 'Point')) {
       source.clear();  // implicit remove of last feature.
    }

});

// When a drawing ends for a point feature, remove the last feature
draw.on('drawend', function (e) {

    // Filter on points ONLY
    if (geomType === 'Point') {
        removeLastFeature();
    }
    lastFeature = e.feature;

});

map.addInteraction(draw);

jsFiddle

See a complete example here that handles all geometry types: https://jsfiddle.net/abb54n79/1/

5
  • Hi , I just figured that when type is Point (which is what I want to add) than the solution isn't working. it works only for LineString , Circle , Polygon.... Any idea how to apply same solution to point ?
    – Alophind
    Commented May 24, 2015 at 7:25
  • I didn't consider points in the previous example. My answer has been updated to support all geometry types.
    – nagytech
    Commented May 24, 2015 at 8:11
  • That solution is excellent , may I ask you something on email ? (your site url don't have contact us)
    – Alophind
    Commented May 24, 2015 at 8:28
  • Updated the code again, apparently the event feature isn't always the same geometry type as the drawing feature. Especially the case for circles where the initial draw is a point. Check my site again - there is a contact form, but was disabled.
    – nagytech
    Commented May 24, 2015 at 9:43
  • Th , that solved the problem , And I've emailed you :)
    – Alophind
    Commented May 24, 2015 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.