Objective:
At any point of time there SHOULD only be zero/one polygon(s) on a vector layer with a polygon control. I need the previous polygon to be removed as soon as user starts drawing the first vertex of the next polygon.
Code:
var map = new OpenLayers.Map(mapDiv, options);
var tempLayer = new OpenLayers.Layer.Vector("Temp Layer");
map.addLayer(tempLayer);
var polyControl = new OpenLayers.Control.DrawFeature(tempLayer,OpenLayers.Handler.Polygon);
polyControl.events.register('featureadded', ' ', removeExistingPolygon);
function removeExistingPolygon(feature) //Assuming event listener gets a feature object
//Written mainly with featureadded in mind
{
if(feature.layer.features.length > 1){
feature.layer.removeFeatures(feature.layer.features[0]);
}
}
tempLayer.events.register(<event_name>, ' ', <prev_polygon_removal_function>);
Question:
What event_name and function should I use to attain the objective? I have tried 'sketchstarted' on tempLayer but it gets triggered even before user starts to draw a polygon. 'vertexmodified' doesn't seem to work at all.
Objective is partially satisfied when using featureadded with polyControl but in that case the first polygon remains visible till the second one is completed.
Any pointers on how to proceed are most appreciated. Thanks.