自定义控件openlayers上的多边形featureadded回调

时间:2017-06-30 09:32:49

标签: openlayers openlayers-3

您是否有办法在某个图层上添加多边形后进行回调?场景是我在地图上添加了一个自定义控件,此控件将调用绘制多边形交互。现在我想要一个回调,帮助我发现已经添加了多边形的绘图。现在我的代码如下

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);
}

现在我想要的是在绘制多边形之后将触发ajax调用。但我不知道如何在自定义控件上添加featureadded回调。我正在使用Openlayers 3。

1 个答案:

答案 0 :(得分:0)

您可以在opt_options中将源传递给控件,​​然后让控件听取“addfeature”。

{{1}}