[OpenLayers-Users] Limitng the number of points that DrawFeature can create

Greg Allensworth gregor at greeninfo.org
Tue Oct 25 17:47:14 EDT 2011


On 10/25/2011 2:37 PM, Dave Potts wrote:
> Is there any way of controling drawfeature such that is only possible to
> insert one point and that a 2nd attempt insertation generates an error?

Sure. You're already handling the "featureadded" event to save your 
point or do something with it, right?

Make a similar handler, for the "beforefeatureadded" event. Like the 
"featureadded" it takes a feature as its argument. Check 
feature.layer.features.length and you'll know that a point has already 
been added. Return false to cancel the addition of the new feature.

layer.events.register('beforefeatureadded', layer, function (feature) {
     if (layer.features.length > 0) {
         alert('One at a time please');
         return false;
     }
     return true;
});


An additional thought:

* You can have your "featureadded" event handler deactivate the 
drawFeature control. This would prevent someone from drawing more than 
one feature per activation.

* You could then add a onactivate event handler to your drawFeature 
control, which calls layer.removeAllFeatures() to remove any previous 
drawing.

Between these two, you would be assured they could never insert a second 
feature accidentally: only one shape is drawn at a time, and opening the 
drawing tool again erases the previous one.

-- 
Greg Allensworth, Web GIS Developer
BS  A+  Network+  Security+  Linux+  Server+
GreenInfo Network - Information and Mapping in the Public Interest
564 Market Street, Suite 510  San Francisco CA 94104
PH: 415-979-0343 x302  FX: 415-979-0371    email: gregor at greeninfo.org
Web: www.GreenInfo.org     www.MapsPortal.org

Subscribe to MapLines, our e-newsletter, at www.GreenInfo.org


More information about the Users mailing list