Reputation: 3772
Hi is there a way to have a callback after a polygon is added on a certain layer? The scenario is I added a custom control on my map and this control will call a draw polygon interaction. Now I would like to have a callback that helps me find out that the drawing of polygon is already added. Right now my code is as below
var polysource = new ol.source.Vector({wrapX: false});
var map = new ol.Map({
target: id,
controls: ol.control.defaults()
.extend([
new ol.control.FullScreen(),
new windowpoly.DrawPolygon()
]),
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
new ol.layer.Vector({
source: polysource
})
],
loadTilesWhileAnimating: true,
view: new ol.View({
center: [0,0],
zoom: 11
})
});
// Create a custom control for the drawpolygon
var windowpoly = {};
windowpoly.DrawPolygon = function(opt_options) {
var options = opt_options || {};
var button = document.createElement('button');
button.innerHTML = '/';
var this_ = this;
var drawPolygon = function(e) {
addInteraction();
};
button.addEventListener('click', drawPolygon, false);
button.addEventListener('touchstart', drawPolygon, false);
var element = document.createElement('div');
element.className = 'draw-polygon ol-unselectable ol-control';
element.appendChild(button);
ol.control.Control.call(this, {
element: element,
target: options.target
});
};
ol.inherits(windowpoly.DrawPolygon, ol.control.Control);
// Function to initialize draw polygon
function addInteraction()
{
draw = new ol.interaction.Draw({
source: polysource,
type: /** @type {ol.geom.GeometryType} */ ('Polygon')
});
map.addInteraction(draw);
}
Now what I want is after I draw the polygon an ajax call will be triggered. But I don't know how to add the featureadded callback on a custom control. I'm using Openlayers 3 for this.
Upvotes: 0
Views: 415
Reputation: 626
You can pass the source to the control in opt_options and then have the control listen to "addfeature".
source.on('addfeature', function(feature) {
// do something with the feature
}, this);
Upvotes: 0