[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 13:56:10 EST 2009


Ah, sorry about that, I misunderstood the problem you were having!

J. Cliff Dyer wrote:
> Thanks.  That's definitely a cleaner setup, but it didn't really solve
> the problem I was having.  Before the box would look like it was being
> drawn properly, but then the persistent copy would be vertically offset.
> Now it's offset from the time I start drawing.  Which is an improvement,
> because at least it will be easier for users to draw the box they want.
> Here's a screenshot to show what I mean: 
> 
> http://imgur.com/5cJ4H
> 
> The Box was drawn exactly from Cape Fear to Cape Hatteras, marked with
> little black dots on the screenshot.  However, the box itself starts
> drawing a bit above the location of the cursor.  It seems the further I
> get from the top of the display, the further displaced the box is.  It
> is impossible to get closer than ~2cm from the bottom of the map.  There
> also seems to be a very slight horizontal displacement.
> 
> Any thoughts on what might cause this displacement?  At this point, no
> transformation is being done--at least not in any of my code.
> 
> Cheers,
> Cliff
> 
> 
> 
> On Fri, 2009-11-13 at 09:58 -0700, Scott Lewis wrote:
>> 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
> 
> 
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users



More information about the Users mailing list