After spending some time, I managed to achieve the result, but still, I think the way I did it is quite "dirty".
Instead of initiating polygonControl
with default OpenLayers.Handler.Polygon
handler, I created a custom handler and responding to addPoint
var customHandler = new OpenLayers.Class(OpenLayers.Handler.Polygon, {
addPoint : function(pixel) {
console.log("A point has been added");
}
});
At this point, I believe the default addPoint
functionalities of drawing polygons are destroyed. So, what I did is just copying back all the codes under addPoint
method, which results in
var customHandler = OpenLayers.Class(OpenLayers.Handler.Polygon, {
addPoint: function(pixel) {
// Here is my magic
console.log("A point has been added");
if (!this.drawingHole && this.holeModifier && this.evt && this.evt[this.holeModifier]) {
var geometry = this.point.geometry;
var features = this.control.layer.features;
var candidate,
polygon;
for (var i = features.length - 1; i >= 0; --i) {
candidate = features[i].geometry;
if ((candidate instanceof OpenLayers.Geometry.Polygon || candidate instanceof OpenLayers.Geometry.MultiPolygon) && candidate.intersects(geometry)) {
polygon = features[i];
this.control.layer.removeFeatures([polygon], {
silent: true
});
this.control.layer.events.registerPriority("sketchcomplete", this, this.finalizeInteriorRing);
this.control.layer.events.registerPriority("sketchmodified", this, this.enforceTopology);
polygon.geometry.addComponent(this.line.geometry);
this.polygon = polygon;
this.drawingHole = true;
break;
}
}
}
OpenLayers.Handler.Path.prototype.addPoint.apply(this, arguments);
}
});
and use that handler in
var polygonControl = new OpenLayers.Control.DrawFeature(polygonLayer, customHandler);
It seems to give me what I want. Even though it probably isn't the right way of doing it. Perhaps, someone could enlighten me how can I properly "override" the addPoint
method. Thanks in advance.