Using openlayers, I want to be able to draw one polygon on a map. After one polygon is drawn, I want an event to fire that stops any more from being drawn until a button is pressed. My current code allows the user to draw more than one polygon and I'm trying to figure out how to limit it. This is my current code:
// Add drawing layer
analysisLayer = new OpenLayers.Layer.Vector("analysisLayer");
map.addLayer(analysisLayer);
// add polygon drawing control
polygon = new OpenLayers.Control.DrawFeature(analysisLayer, OpenLayers.Handler.Polygon);
map.addControl(polygon);
What I tried doing was adding a callback to the Control.DrawFeature
function as follows:
// add polygon drawing control
polygon = new OpenLayers.Control.DrawFeature(
analysisLayer,
OpenLayers.Handler.Polygon, {callbacks: { "finalize" : function(){alert('poly made')} } } );
map.addControl(polygon);
This does not work, but it does not give me an error. I'm not sure how exactly to structure this function. I did find the function finalize in the documentation which is what I thought I needed, but I'm also not sure how to structure the callback
.
EDIT: I'm starting to understand how these callbacks work. For example this detects that a feature has been added (using @Vadim's advice):
// detect polygon events
function newPolygonAdded (evt) {
alert('Polygon completed');
polygon.deactivate(); //stops the drawing
}
// add polygon drawing control
polygon = new OpenLayers.Control.DrawFeature(analysisLayer, OpenLayers.Handler.Polygon,
{eventListeners:{"featureadded": newPolygonAdded}});
map.addControl(polygon);
What I still don't understand is how to limit the drawing layer from allowing more polygons to be drawn.
Answer
yourControl.events.register("featureadded", ' ' , controlFeatureHandler);
...
function controlFeatureHandler(data)
{
// do regular stuff
// DISABLE control to draw
yourControl.deactivate();
}
No comments:
Post a Comment