Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers 3

You kind of have catch-22 or at minimum not detailed enough requirements. Specifically - do you want to remove the 1st feature as SOON as the user starts drawing a new feature? Or when they're done? Because you could end up blowing out your 1st feature and user cancels out of drawing 2nd feature and then you have nothing.

You have 2 additional things you can register:

1) Register to listen for the Control's activate event. When the drawing tool is activated, just blow out any features in the layer.
1a) And by the way, it's better to just say layer.removeAllFeatures() instead of the way you have it.

2) Register to listen for the Layer's beforefeatureadded event and work your logic in there. http://dev.openlayers.org/docs/files/OpenLayers/Layer/Vector-js.html#OpenLayers.Layer.Vector.events

share|improve this answer
    
I want to remove the 1st feature as SOON as the user starts drawing a new feature. –  Siva Feb 12 '13 at 10:41
    
And one thing I missed, the draw control is always active. So can't use first solution. Regarding 1a) my existing code removes the feature AFTER the second one is done., don't you think removeAllFeatures() would remove them both in that case? As for 2) beforefeatureadded kicks in only just before featureadded and not when I place/draw the first vertex of the second polygon. –  Siva Feb 12 '13 at 10:47
1  
Ok, then you have to go lower at the Handler events. Look at the Point and Polygon events dev.openlayers.org/releases/OpenLayers-2.12/doc/apidocs/files/… and perhaps create in Point and point in Polygon are what you're looking for. There you can check "ok, are there other shapes? Delete them" and in Polygon you'll have to check "is this 1st point? -delete old on. has done fired yet? no, then user is just drawing stuff" –  Vadim Feb 12 '13 at 14:21
    
Thanks a lot for this. point in Polygon Handler did the trick. And I used create for RegularPolygon Handler. –  Siva Feb 13 '13 at 10:57
add comment
up vote 0 down vote accepted

This answer is built with the excellent help from @Vadim. Read through his answer and ensuing comments for a detailed explanation

Another helpful link about handler callbacks: http://gis.stackexchange.com/a/37741/10620

Solution:

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.handler.callbacks.point = function(data) {
    if(tempLayer.features.length > 0)
    {
        tempLayer.removeAllFeatures();
    }
}

var regPolyControl = new OpenLayers.Control.DrawFeature(tempLayer,OpenLayers.Handler.RegularPolygon);

regPolyControl.handler.callbacks.create = function(data) {
    if(tempLayer.features.length > 0)
    {
        tempLayer.removeAllFeatures();
    }
}
share|improve this answer
    
I'm glad you got to the bottom of this, but I'm not really sure why you gave yourself the answer? hmmm. –  Vadim Feb 13 '13 at 18:09
    
The direct answer to my question is the one above. Myself being a lazy bum, usually look for the direct answers to questions before going into the details. Likewise this will help people who are searching for a quick answer. If they do want to know in detail, they can always refer to your helpful answer. Thanks again. –  Siva Feb 14 '13 at 5:56
    
Yes, agree with @Vadim: not very gentleman-like to just regurgitate the answer Vadim gave you, and then award yourself the answer. At least give the man some credit for helping you. –  nathanvda Sep 30 '13 at 10:12
1  
Good that you shared the final code, but awarding yourself the correct answer is bad form. –  ach Oct 3 '13 at 15:25
add comment

"polyControl.handler.callbacks.point = function(data) { if(tempLayer.features.length > 0) { tempLayer.removeAllFeatures(); } }" No 'callbacks' properties

share|improve this answer
    
Please consider adding more details to the answer which provide context to this code. As it stands, it is not not clear what this code does, or how it should be used. –  Devdatta Tengshe Apr 24 at 4:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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