2016-07-22 75 views
0

我不知道如何創建add事件,或者在功能添加到源時將偵聽器附加到事件。在這一刻我有一大堆的其他活動,如:添加功能事件

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

我認爲drawend事件正是我需要的,但事實證明,當這個事件發生時,該功能尚未添加到源。

回答

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

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

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