2017-06-30 16 views
0

嗨有一种方法来在某个图层上添加一个多边形后有一个回调?该场景是我在我的地图上添加了一个自定义控件,并且此控件将调用绘制多边形交互。现在我想要有一个回调,帮助我发现多边形的绘图已经添加。现在我的代码如下自定义控件开放层多边形featureadded回调

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调用将被触发。但我不知道如何在自定义控件上添加功能添加的回调。我为此使用了Openlayers 3。

回答

0

您可以将源代码传递给opt_options中的控件,然后让控件监听“addfeature”。

source.on('addfeature', function(feature) { 
     // do something with the feature 
    }, this);