[OpenLayers-Dev] lines with only two vertexs

Tim Schaub tschaub at opengeo.org
Wed Mar 25 16:16:13 EDT 2009


Hey-

toni hernández wrote:
> Hi all,
> 
> I need to draw lines with only two vertexs so when the second vertex is 
> added the draing must end.
> 

If you want to use the existing code, your application could do 
something like this (assuming you had a vector layer named "layer" and a 
drawing control named "draw" with a path handler configured to draw on 
that layer):

// using API properties and methods only

layer.events.on({
     sketchmodified: function(event) {
         var feature = event.feature;
         if(feature.geometry.components.length > 2) {
             feature = feature.clone();
             var geom = feature.geometry;
             geom.removeComponent(geom.component[2]);
             draw.deactivate();
             layer.addFeatures([feature]);
         }
     }
});

Untested, but something like that should work.

Tim

> 
> I've done it so far creating a new handler.path where I control the 
> length of the geometry components. When is bigger than 2 I run the 
> finalize function so it ends.
> That works fine but I think there must be a better way of doing it?
> 
> I have also tried registering the sketchmodified event of the control 
> from the main html page.
> 
> The issue here is that I do not know how to deactivate the drawing 
> control without throwing and error. (feature is null, on Vector.js (line 
> 617, in drawFeature function)
> 
> any ideas?
> 
> the code.
> 
> //DRAWING CONTROLS
> drawControls = {
>     point: new OpenLayers.Control.DrawFeature(pointLayer,
>         OpenLayers.Handler.Point),
>     line: new OpenLayers.Control.DrawFeature(lineLayer,
>         OpenLayers.Handler.Path, options),
>     polygon: new OpenLayers.Control.DrawFeature(polygonLayer,
>         OpenLayers.Handler.Polygon, options)
> };
> 
> for(var key in drawControls) {
>     map.addControl(drawControls[key]);
> }
> 
> //REGISTERING
> drawControls.line.layer.events.on({
>     'sketchmodified': report
> });
> 
> 
> function report(param)
> {
>     if (param.feature.components.length>2)
>     {
>         drawControls.line.handler.finalize(); 
>     }
> }
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Dev mailing list
> Dev at openlayers.org
> http://openlayers.org/mailman/listinfo/dev


-- 
Tim Schaub
OpenGeo - http://opengeo.org
Expert service straight from the developers.



More information about the Dev mailing list