How can I prevent the user to draw more than one polygon on the map? Can you send me code examples for doing this. thank you...
Prevent the user to draw multiple polygons
Ayhan,
Here is a method for you to prevent adding polygon on the map. Please paste the following JavaScript to our installed sample at "Samples\MapShapes\DrawEditShapes.aspx".var tgMap = null;
var flagPolygonDrew = false;
var OnMapCreated = function(map) {
tgMap = map;
OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer.Vector, {
preFeatureInsert: function(feature) {
if (flagPolygonDrew) {
// if false, it'll prevent the following actions.
this.drawn = false;
}
if (feature.geometry.CLASS_NAME == 'OpenLayers.Geometry.Polygon') {
flagPolygonDrew = true;
}
}
});
}
If you have any questions please let me know.
Thanks,
Howard
Howard
thank you, it works , but at this time for example when I try to draw 3 polygons on it, I can draw one polygon but I get feature count = 3 but it should be 1. What should I do?
Ayhan,
Yeah, you're right, please change the preFeatureInsert function to the following as following code, it'll be fine.preFeatureInsert: function(feature) {
if (flagPolygonDrew) {
// if false, it'll prevent the following actions.
this.drawn = false;
this.features.pop();
}
if (feature.geometry.CLASS_NAME == 'OpenLayers.Geometry.Polygon') {
flagPolygonDrew = true;
}
}
If you have more questions please let me know.
Thanks,
Howard
Now it works correctly. Thank you very much…
Ayhan,
You are welcome. Please feel free to ask if you have more questions.
Thanks,
Howard