添加功能事件

时间:2016-07-22 07:43:37

标签: javascript openlayers-3

我不知道如何在向源添加功能时创建add事件或将事件附加到事件。此刻我还有很多其他事件,比如:

draw.on("drawend", function (e) {
//....
});

我认为drawend事件是我需要的,但事实证明,当此事件发生时,该功能尚未添加到源中。

2 个答案:

答案 0 :(得分:2)

或者您可以使用'addfeature'事件:

source.on( 'addfeature', function (ft) {
    // ft - feature being added
});

答案 1 :(得分:1)

尝试手动将其添加到源: 您将绘制交互添加到叠加层

 var features = new ol.Collection();
 var featureOverlay = new ol.layer.Vector({
     source: new ol.source.Vector({features: features}),
     style: new ol.style.Style({
        fill: new ol.style.Fill({
            color: 'rgba(255, 255, 255, 0.2)'
        }),
        stroke: new ol.style.Stroke({
            color: '#ffcc33',
            width: 2
        }),
        image: new ol.style.Circle({
            radius: 7,
            fill: new ol.style.Fill({
                color: '#ffcc33'
            })
        })
    })
 });
 featureOverlay.setMap(map);

 var draw = new ol.interaction.Draw({
    features: features, // we set the newly drawn feature on the overlay declared previously
    type: /** @type {ol.geom.GeometryType} */ ('Polygon') // for example polygon
    });

在drawend事件中,您将该功能推送到所需的源图层

draw.on('drawend', function(event) {
     yourSource.addFeature(event.feature);
}