[OpenLayers-Users] How to draw a BoundingBox Vector feature?

Christopher Schmidt crschmidt at metacarta.com
Mon Nov 17 11:44:04 EST 2008


On Mon, Nov 17, 2008 at 11:37:49AM -0500, Heidt, Christopher M. wrote:
> I need a way to allow the user to draw a bounding box on the map but
> there doesnt seem to be a builtin way of doing this.
> polygon and regularPolygon are no good because you cant force them to be
> straight.
>  
> I'm attempting to use the Box Handler to allow the user to draw a
> bounding box on the map. (code below)
> The problem im having is that the feature that ends up being created is
> suppose to be a polygon with the same bounds produced by the box, 
> but what i end up with is an extreamly tiny polygon at 0,0. 
> I'm using a google base so i thought maybe the projection was the
> culprit, 
> but i've tried transforming the bounds and nothing good seems to come
> from it. 
> Any ideas? 

Handler.Box returns a Bounds which is in *pixel* space -- you have to
convert the corners with getLonLatFromPixel:
http://openlayers.org/dev/examples/custom-control.html demonstrates how.

> var control = new OpenLayers.Control({
>     draw: function () {
>         this.box = new OpenLayers.Handler.Box( control,
>             {done: this.notice},
>             {keyMask: OpenLayers.Handler.MOD_SHIFT});
>         this.box.activate();
>     },
>     notice: function (bounds){
>         var geom = bounds.toGeometry();
>         var feature = new OpenLayers.Feature.Vector(geom);
>         _bboxLayer.addFeatures(feature);
>         _map.zoomToExtent(bounds,true);
>     }
> });

> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users


-- 
Christopher Schmidt
MetaCarta



More information about the Users mailing list