2

Is there a way to trigger openlayers event from console. In this case: ol.interaction.Select event which listens 'select' (ol.SelectEvent.select). It triggers when feature is selected or deselected by mouse click.

1

1 Answer 1

1

In the mean time I have found the answer. There is OpenLayers built in function for dispatchingEvents :

ol.events.EventTarget.prototype.dispatchEvent.call(this,event);

this references the object on which we add event listener

In my case:

var fakeOnSelectEvent = new ol.interaction.Select.Event(ol.interaction.Select.EventType.SELECT, [], [], false);  
var selectInteraction = new ol.interaction.Select({...});

ol.events.EventTarget.prototype.dispatchEvent.call(selectInteraction,fakeOnSelectEvent); 
2
  • It may fire an event but does select a feature?
    – Mike
    Nov 12, 2019 at 16:46
  • @Mike in my case I only wanted my event listener select.on('select',... to be triggered because my logic is implemented that way. To select the feature this way I think you can manualy add features in first empty array parm of fakeOnSelectEvent and then when listening you can add them from that array like select.getFeatures().push(e.selected). That is the way I would do it.
    – Goran
    Nov 12, 2019 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.