Reputation: 6845
I have draw controls in my OpenLayers application like this.
var drawControls = {
polygon: new OpenLayers.Control.DrawFeature(polygonLayer, OpenLayers.Handler.Polygon),
box: new OpenLayers.Control.DrawFeature(boxLayer, OpenLayers.Handler.RegularPolygon,{
handlerOptions: {
sides: 4,
irregular: true
}
})
};
I am activating this controls with activate function of control.
var control = drawControls[selected.key];
control.activate();
This draws polygon on map. But I could not access the events of this control. For example, on draw end event should give me a polygon or box.
Upvotes: 1
Views: 3852
Reputation: 447
Here is an example using featureadded
event of the control.
// Add a polygon layer to which polygons will be drawn.
var polygonLayer = new OpenLayers.Layer.Vector("Polygon Layer", {
projection: "EPSG:4326"
});
map.addLayers([polygonLayer]);
// Initialize the polygon editor.
var polygonEditor = new OpenLayers.Control.DrawFeature(polygonLayer,
OpenLayers.Handler.Polygon);
// And its event listener when the feature is added.
polygonEditor.events.register('featureadded', polygonEditor, function(evt) {
// Here, you should see the geometry of the drawn feature in your console.
var geom = evt.feature.geometry;
console.log(geom);
});
And a sample fiddle.
Upvotes: 2