[OpenLayers-Users] limiting a Vector layer to the last feature added

John Cartwright john.c.cartwright at comcast.net
Fri Aug 14 17:26:42 EDT 2009


Hello All,

I'm using OL 2.8 and trying to implement the behavior where the user can
digitize points, lines, or polygons, but only the last created feature
will be kept and displayed.  I've attempted this via a handler on the
Layer's "beforefeatureadded" event like:

function clearPreviousFeatures(evt) {
    dojo.forEach(vectorLayer.features, function(f) {
       if (f.id != evt.feature.id) {
          vectorLayer.removeFeatures([f], {silent:true});
       }
    });
    vectorLayer.redraw();
}


However,  I find that the previous drawn features remain until the
DrawFeature control is changed.  Also as soon as the second feature is
added, the code above complains that "f is undefined".  Finally, any
features added subsequent to the first do not display in the style
defined for the layer.

I feel like this should be a simple task and that I'm missing something
fundamental here.  Can someone show me what's wrong or suggest a better
strategy for accomplishing this goal?

Thanks!

--john




More information about the Users mailing list