[OpenLayers-Users] How to access polygons or lines as WKT after drawing them?

Pierre Knobel knobel.pierre at gmail.com
Sun Oct 30 05:43:58 EDT 2011


Hi,

I'm trying to write a program starting from this example:
http://openlayers.org/dev/examples/draw-feature.html
The user should be able to draw any number of polygons and lines, and
then when hitting a button on the webpage the javascript code would do
something with them. The problem is that I can't figure out how to
access these geometries.

Basically, what I have is a vector layer defined by:
    drawLayer = new OpenLayers.Layer.Vector("blabla")

>From what I understand from
http://dev.openlayers.org/docs/files/OpenLayers/Layer/Vector-js.html,
http://dev.openlayers.org/docs/files/OpenLayers/Feature/Vector-js.html
and http://dev.openlayers.org/docs/files/OpenLayers/Geometry-js.html,
I should be able to retrieve the WKT of the geometries drawn by the
user the following way:
     drawLayer.features[i].geometry.toString();   //i would be an
integer lower than the number of geometries on my layer

The problem is that it doesn't work, and javascript doesn't make
debugging very easy. I tried to run the following function:
     function getDrawings()  {
            var feats = drawLayer.features;
            alert(typeof feats);
            alert(feats);
            var spam = 0;
            for(var f in feats) {
                   alert(typeof f);
                   alert(f);
                   spam = spam + 1;
            }
            document.getElementById("drawing_list").innerHTML = String(spam);
        }

The counter 'spam' returns the correct number of geometries (let's say
4 for this example), when I print 'feats' I get '[object
Object],[object Object],[object Object],[object Object]', but 'typeof
f' prints a string which ends up being an incrementing number (0, 1,
2, 3) when I would expect to get a geometry object.

What am I doing wrong?

Thanks,

Pierre


More information about the Users mailing list