Search code examples
reactjsopenlayers-6

How to get event pixel when I have coordinates in database


I have clicked on a coordinates and remove with the help of MouseEvent clientY and ClientX value. I have some coordinates in database and I want to hit them same as it clicked as event. I have used bellow code.

const pixel = map.getPixelFromCoordinate(coordinates);
event = {}
event.clientX = pixel[0];
event.clientY = pixel[1];
simulateSingleClick(map, event);

But not able to click because event's clientY and above clientY value has lot of different.


Solution

  • The map viewport is a viewport within the client viewport https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect so unless you have a full page map you will need to add the left and top offsets of that

    const vpClient = map.getViewport().getBoundingClientRect();
    const pixel = map.getPixelFromCoordinate(coordinates);
    event = {}
    event.clientX = vpClient.left + pixel[0];
    event.clientY = vpClient.top + pixel[1];
    simulateSingleClick(map, event);