[GIS] How to simulate an event in OpenLayers 3

openlayers

I have the following OpenLayers 3 code for this:

map.on('dblclick', function(evt) {
    // Do Something.
});

The idea is catch the event principally. I've tried to find something but without success.

Any idea?

Best Answer

You need to do two things:

  1. Decouple the event handler so that it can be triggered by another event.
  2. Impersonate the ol.MapBrowserEvent members that your event handler requires

The following code should give you an idea on how to do what you need.

var doSomething = function(evt) {
    console.log('I've done it');
    // do something with evt.coordinate, evt.pixel or evt.map
};

_map.on('dblclick', doSomething);

$('#simulator').on('click', function() {
    doSomething({
        coordinate: [0,0],
        map: _map,
        pixel: [100,10],
        wasVirtual: true
    });
});

You may also want to add a member to the event arguments that signifies the source was virtual, or truly from the map dblclick event.