[OpenLayers-Users] Trying to draw a box on a map, and get the coordinates back.

Scott Lewis scott.lewis at nsidc.org
Fri Nov 13 11:58:45 EST 2009


I did something like this using one of the examples on the OpenLayers 
site, modified slightly.  Here's what I used:

boxControl = new OpenLayers.Control();
OpenLayers.Util.extend(boxControl, {
   draw: function() {
     this.box = new OpenLayers.Handler.RegularPolygon(boxControl, 
{"done": this.notice}, {sides:4, irregular:true, persist:true});
     this.box.activate();
   },

   notice: function(geom) {
     // whatever you want it to do after the box has been drawn
   }
});

// then add the boxControl to the map

This works pretty nicely.  Note that "geom" will be in the same 
projection as the map, so if you want the output to be in another 
projection (like a latitude/longitude display) you will have to go 
through the geometry and transform the points.  Also, you may have to go 
through the geometry to determine which point is which (it doesn't 
always start with the upper-left corner, and it's not always clockwise 
or counter-clockwise).  If interested, I can post some of the functions 
I do for that.

I hope this helps,

Scott Lewis
NSIDC

J. Cliff Dyer wrote:
> I'm trying to draw a box on a map, have it persist until another box
> gets drawn, and submit the coordinates of the box to a form.  I'm doing
> this to allow the user to search for objects within a given area.  
> 
> I'm doing this by using an OpenLayers.Handler.Box tool in a custom
> OpenLayers.Control, which then uses to the "done" callback of the draw
> method to create (or recreate) an OpenLayers.Feature.Vector and populate
> (or repopulate) a search form.
> 
> My immediate problem is that the persistent box is not in quite the same
> location as the box the user just drew.  I suspect this has something to
> do with projections, but since I never explicitly set projections, I'm
> not sure where the error sneaked in.  
> 
> More generally, I'm not sure I'm taking the optimal route from the
> bounds passed to the callback to the Vector feature.  Currently, I do
> the following:
> 
>   1) Take the incoming OpenLayers.Bounds object, 
>   2) Get an OpenLayers.Pixel from the the corners
>   3) Get the coordinates of each pixel using map.getLonLatFromPixel
>   4) Create a new OpenLayers.Bounds 
>   5) Extend it to my new coordinates
>   6) Convert it to a geometry
>   7) Convert it to an OpenLayers.Feature.Vector
> 
> Then I take the coordinates from step 3) and populate a form with them,
> which I then submit to my application server side.
> 
> The code for this is pasted here: http://pastebin.com/m300c24a5
> 
> Any pointers on how to do this more cleanly would be much appreciated.
> 
> Cheers,
> Cliff
> 
> 
> 
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users



More information about the Users mailing list