如果有人仍然对这个问题的答案感兴趣,那么下面的代码应该可以达到要求的要求(因为我曾为这类问题苦苦挣扎了一段时间):
// Create the icon feature
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point([lng, lat])
});
// Set the style for the icon feature
iconFeature.setStyle(new ol.style.Style({
image: new ol.style.Icon(({
anchor: [0.5, 35],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
opacity: 1,
src: markerGraphics
}))
}));
// Create the vector layer with it's source
var vectorLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: [iconFeature]
})
});
// Drag and drop feature
var dragInteraction = new ol.interaction.Modify({
features: new ol.Collection([iconFeature]),
style: null,
pixelTolerance: 20
});
// Add the event to the drag and drop feature
dragInteraction.on('modifyend',function(){
callYourFunction();
},iconFeature);
// Add the vector layer and the refering drag and drop interaction
map.addLayer(vectorLayer);
map.addInteraction(dragInteraction);
基于修改事件,可以将侦听器附加到拖放交互而不是图层/图标功能。使用此解决方案,可以在图层/图标的拖动停止后执行功能(类似于 OpenLayers 2 中已知的“dragend”)。