[OpenLayers-Users] Accessing a callback | Getting a polygon's vertices

Paul Spencer pagameba at gmail.com
Wed Oct 14 17:31:21 EDT 2009


Hi, not too trivial at all - features and geometries are a little  
confusing at first.  I'll try to explain ...

First, the feature object passed to your 'polygonAdded' function is  
the thing you want.

If you look in Control/DrawFeature.js you will see that the  
DrawFeature control hooks the 'done' event of the handler and creates  
a new vector feature from the supplied geometry and calls the  
feautreAdded function which points to your polygonAdded function (and  
which you report as working).

The structure of the feature object is in Feature/Vector.js, it has a  
geometry attribute that is what you are interested in.

The geometry object (in this case Geometry/Polygon.js) has (via  
inheritance from Geometry/Collection.js) a getVertices() method that  
returns an array of Geometry/Point.js instances, each of which has an  
x and y attribute which is really the geographic x and y in whatever  
the coordinate system units are that you are using (lon and lat if you  
are using the defaults).

So ...

polygonAdded = function(feature) {
   var nodes = feature.geometry.getVertices();
   for (var i=0; i<nodes.length; i++) {
     var lon = nodes[i].x;
     var lat = nodes[i].y;
   }
}

Cheers

Paul

On 2009-10-14, at 5:10 PM, Harry Lam wrote:

> Hello community!  This is my first post; I hope I'm not asking too  
> trivial a question.  :)  I'll get right into it:
>
> Based on this example -- http://www.openlayers.org/dev/examples/drag-feature.html 
>  -- I have put a similar control on my website where I can draw a  
> polygon on my map.  So what I want to do is get the coordinates of  
> my polygon’s vertices after I finish drawing it.
>
> According to
> http://dev.openlayers.org/releases/OpenLayers-2.8/doc/apidocs/files/OpenLayers/Handler/Polygon-js.html
> there are callback functions called ‘point’ and ‘done’ that should  
> be able to give me what I want.
>
> My question is how do I get control of those functions?  I think I  
> am a little lacking in syntax knowledge as to how to set it up.
>
> For example I have:
> var drawpoly = new OpenLayers.Control.DrawFeature(vectorLayer,  
> OpenLayers.Handler.Polygon);
> map.addControl(drawpoly);
> which takes care of enabling the drawing feature.  But I don’t know  
> how to get to the done() callback function from here.
>
> On a separate note, I found this example online:
> var drawpoly = new OpenLayers.Control.DrawFeature(vectorLayer,  
> OpenLayers.Handler.Polygon, {'featureAdded': polygonAdded});
> ...
> polygonAdded = function (feature) {
>  //Yay, I can do stuff here!
> }
> which is pretty close to what I want, but I think the object passed  
> to it is not the polygon object I want to manipulate.  (Or is it?)
>
> If anyone has a better way to get the coordinates of a polygon (I  
> assume I'd get an x-y pixel value first and would have to convert to  
> lat/lng), I'd love to hear that, too.  But knowing how to get to the  
> callbacks would also be useful in future applications.  :)
>
> Thanks for reading and considering!
>
> --Harry
> mr.harry.lam at gmail.com
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users




More information about the Users mailing list