[OpenLayers-Users] How to override createGeometry

Tim Schaub tschaub at opengeo.org
Tue May 12 17:15:44 EDT 2009


Hey-

Stéphane Poirier wrote:
> Hi All,
> 
> In order to prevent creating rectangle that are too small, I would like to
> override createGeometry (that is, in my code, the function
> OpenLayers.Handler.RegularPolygon.prototype.createGeometry.apply(this,
> arguments);)
> 

Instead of overriding a non-API method, you could use the 
"sketchcomplete" event on your layer to stop the feature from being 
added if a specified condition is not met.

e.g.

rectangleLayer.events.on({
     sketchcomplete: function(event) {
         var feature = event.feature;
         return (feature.geometry.getArea() > 10); // false to not add
     }
});

If your "sketchcomplete" listener returns false, the feature will not be 
added to your layer.

http://dev.openlayers.org/apidocs/files/OpenLayers/Layer/Vector-js.html#OpenLayers.Layer.Vector.EVENT_TYPES

Tim


> How can I do this?
> 
> Here is my code:
> 
>  drawControls =
>     {
>         point: new OpenLayers.Control.DrawFeature(rectangleLayer,
>                    OpenLayers.Handler.Point, {featureAdded: point_feature_added,
>                     handlerOptions: {createFeature: function() {
>                             this.control.layer.destroyFeatures();
>                            
> OpenLayers.Handler.Point.prototype.createFeature.apply(this, arguments);
>                         }}}),
>         rectangle: new OpenLayers.Control.DrawFeature(rectangleLayer,
>                    OpenLayers.Handler.RegularPolygon, {featureAdded:
> rectangle_feature_added,
>                     handlerOptions: {sides: 4, irregular: true,'displayClass':
> 'olControlDrawFeatureRectangle',
>                       createGeometry: function() {
>                             this.control.layer.destroyFeatures();
>                            
> OpenLayers.Handler.RegularPolygon.prototype.createGeometry.apply(this,
> arguments);
>                       }}})
>     }
> 
> 
> Regards,
> 
> 
> Stéphane Poirier
> Professionel de recherche au CARTEL
> Centre d'Applications et de Recherches en Télédétection
> Département de géomatique appliquée
> Université de Sherbrooke
> Sherbrooke (Québec), CANADA J1K 2R1
> Tel.: (+1 819) 821 8000 #61904
> Fax.: (+1 819) 821 7944
> Stephane.Poirier at USherbrooke.ca
> http://www.usherbrooke.ca/cartel
> 
> Stéphane Poirier
> Professionel de recherche au CARTEL
> Centre d'Applications et de Recherches en Télédétection
> Département de géomatique appliquée
> Université de Sherbrooke
> Sherbrooke (Québec), CANADA J1K 2R1
> Tel.: (+1 819) 821 8000 #61904
> Fax.: (+1 819) 821 7944
> Stephane.Poirier at USherbrooke.ca
> http://www.usherbrooke.ca/cartel
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users


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



More information about the Users mailing list