2017-06-30 25 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);