Search code examples
javascriptopenlayers

Openlayer, how to trigger translate without click


I want to implement something like this sample https://openlayers.org/en/latest/examples/translate-features.html

but instead of clicking on feature then dragging, I already have target feature and I want to trigger translate event on it. When the event triggered, the feature is moved under mouse cursor and when left-clicking on map, the feature is transferred to that position.

Can openlayers do something like that or I have to custom the translate?


Solution

  • The interaction relies on pointerdown and pointerdrag events, so you will probably need to simulate those as in the OpenLayers tests https://github.com/openlayers/openlayers/blob/main/test/browser/spec/ol/interaction/translate.test.js#L68 A real click will be followed by a pointerup event which will end the interaction https://codesandbox.io/s/translate-features-forked-b0f0m