[OpenLayers-Dev] DrawFeature.featureAdded() - called in wrong place?

Tim Schaub noreply at geocartic.com
Fri Apr 6 12:04:26 EDT 2007

Jeff Dege wrote:
> I'm trying to create two controls on a tool bar, both of which draw
> polygons, but where what happens after the polygon is drawn would be
> different.  I had noticed that in EditingToolBar.js, each control is
> assigned a featureAdded() function that sets the state of the feature to
> OpenLayers.State.INSERT.  I'd thought that I could use this function to
> set a different state, or to add my own status attribute to the feature,
> and then check on the value of that state in
> Layer.Vector.onFeatureInsert().
> But in Control\DrawFeature.js, the drawFeature() function calls
> this.featureAdded() after it calls this.layer.addFeatures(), so there's
> no way in onFeatureInsert() to distinguish which control created the
> feature.

You have 3 or 4 dozen options here.

First, the layer calls onFeatureInsert - so you're right, you have no 
way of knowing what did the feature adding by the time that gets called.

The DrawFeature control calls featureAdded (past tense) after the 
feature has been added to the layer - so here also, you're too late if 
you want to do something with the feature before it gets added to the 
layer.  It wouldn't make any sense to call featureAdded before the 
feature gets added to the layer.

If you look at the DrawFeature control, you'll see that it sets its own 
drawFeature method as the "done" callback for the handler that actually 
does the drawing.  So, you're drawing polygons.  The DrawFeature control 
is using the PolygonHandler in this case.  The DrawFeature control is 
constructing the PolygonHandler with callbacks that look like {done: 
this.drawFeature} (where this is the DrawFeature control).

So, while the DrawFeature control is not really meant to be used to do 
anything custom with features *before* they are added to a layer (more 
on this below) - you could customize this control to do what you want.

If you construct your DrawFeature control with something like:

     var options = {drawFeature: myDrawFeature};
     var control = new OpenLayers.Control.DrawFeature(
                           layer, OpenLayers.Handler.Polygon, options);

and myDrawFeature is something like:

     function myDrawFeature(geometry) {
         var feature = new OpenLayers.Feature.Vector(geometry);
         // do something with feature before it is added to the layer
         // at this point you know which control did the drawing

All that said, I wonder if you really need to be using the DrawFeature 
control at all.  If you want users to digitize polygons, and then you 
want to do a lot of custom stuff *before* adding the features to a 
layer, then it sounds to me like you should make a custom control that 
uses the polygon handler.

I recognize that this is too much for one email already.  If you want to 
create your own control that uses the polygon handler (i.e. if the above 
solution doesn't look good to you), write back and I can show a quick 
example of an alternative.

Hope that helps,

More information about the Dev mailing list