[OpenLayers-Dev] Prevent adding a point to a sketch

geographika geographika at gmail.com
Fri Oct 1 04:13:33 EDT 2010


  Hi,

I want to be able to prevent a user from adding a new vertex to a line 
sketch tool if I determine the location is invalid.
So for example in http://openlayers.org/dev/examples/editingtoolbar.html 
I may want to prevent a user from clicking on the sea (the code to work 
out if a point is valid or not is fine).

My draw feature tool calls a pointHandler function correctly, but there 
seems no way to cancel the addition of a new point.

var myControl = OpenLayers.Control.DrawFeature(myLayer, 
OpenLayers.Handler.Path, {
                     callbacks: {
                         point: pointHandler
                     });

     function pointHandler = function(newPoint){
         if(!_isValidPoint){
             //none of the following work...
             //myControl.handler.destroyFeature();
             //myControl.removePoint();
             //newPoint= null;
         }
     }


Looking at the source code for at 
http://trac.osgeo.org/openlayers/browser/trunk/openlayers/lib/OpenLayers/Handler/Path.js, 
the callback occurs after the new geometry is added but before it is drawn.

127 
<http://trac.osgeo.org/openlayers/browser/trunk/openlayers/lib/OpenLayers/Handler/Path.js#L127> 
	        this.line.geometry.addComponent(
128 
<http://trac.osgeo.org/openlayers/browser/trunk/openlayers/lib/OpenLayers/Handler/Path.js#L128> 
	            this.point.geometry, this.line.geometry.components.length
129 
<http://trac.osgeo.org/openlayers/browser/trunk/openlayers/lib/OpenLayers/Handler/Path.js#L129> 
	        );
130 
<http://trac.osgeo.org/openlayers/browser/trunk/openlayers/lib/OpenLayers/Handler/Path.js#L130> 
	        this.callback("point", [this.point.geometry, this.getGeometry()]);
131 
<http://trac.osgeo.org/openlayers/browser/trunk/openlayers/lib/OpenLayers/Handler/Path.js#L131> 
	        this.callback("modify", [this.point.geometry, this.getSketch()]);
132 
<http://trac.osgeo.org/openlayers/browser/trunk/openlayers/lib/OpenLayers/Handler/Path.js#L132> 
	        this.drawFeature();



I also tried a custom OpenLayers.Control.Click with stopSingle set to 
true, but new points are added on the "mousedown" event, and so the 
click control is called afterwards.

Are there any ways to achieve this, or will it involve overriding 
browser events?
Apologies if this is in the wrong mailing list.
Thanks for any help,

Seth G.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-dev/attachments/20101001/75efc01c/attachment.html


More information about the Dev mailing list