[OpenLayers-Dev] Save drawn polygon

Tim Schaub tschaub at opengeo.org
Tue Feb 2 20:29:21 EST 2010


Alex G. wrote:
> Thanks for the link!
> 
> I am not sure how I can get the info I get on hover after the user finishes
> drawing a polygon. Another thought would be to "get" all the drawn shapes
> from the vector layer. After going through the layer functions I wasn't able
> to find any such function. Does one exist?
> 

Assuming you have a vector layer, you can create a listener to respond 
when features are added to the layer (by a draw control for example):

     layer.events.on({
         featuresadded: function(event) {
             console.log("newly added features: " + event.features);
         }
     });

At any time, you can get all the features on the layer:

     // all features on the layer
     console.log(layer.features);

And if you want to serialize the features, you'll use one of the 
available formats.  E.g.

     var format = new OpenLayers.Format.GeoJSON();
     var str = format.write(layer.features);

More information on vector layer events:
http://dev.openlayers.org/apidocs/files/OpenLayers/Layer/Vector-js.html#OpenLayers.Layer.Vector.EVENT_TYPES

Tim

> Thanks in advance for any help!
> 
> On Mon, Feb 1, 2010 at 5:14 PM, Bart van den Eijnden <bartvde at osgis.nl>wrote:
> 
>> See:
>>
>>
>> http://dev.openlayers.org/releases/OpenLayers-2.8/examples/vector-formats.html
>>
>> Best regards,
>> Bart
>>
>> On Feb 1, 2010, at 4:10 PM, Alex G. wrote:
>>
>> Hello,
>>
>> I am using the drawing example (
>> http://openlayers.org/dev/examples/editingtoolbar.html) on OpenLayers to
>> allow users to draw lines and polygons on my map.
>>
>> I was wondering how may I "save" the polygons that they have drawn. Is
>> there some way to get the coordinates they have clicked and then save them
>> to some database or external file? Is there some built-in function to return
>> the coordinates clicked or something similar?
>>
>> Do I have to "record" the clicks that the user makes and save them? If yes,
>> how could I go about doing that?
>>
>> Thanks in advance,
>>
>> Alex
>> _______________________________________________
>> Dev mailing list
>> Dev at openlayers.org
>> http://openlayers.org/mailman/listinfo/dev
>>
>>
>>
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> 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