Getting pixel coordinates of an image overlay using leaflet map library on click (right click)

后端 未结 4 1496
后悔当初
后悔当初 2020-12-17 17:18

I\'m trying to get the pixel coordinates of an image overlay on click (right click/contextmenu) using the leaflet map library. Essentially when a user clicks on the image, I

4条回答
  •  有刺的猬
    2020-12-17 17:22

    The leaflet is giving you the x,y coordinates along the "image-map" div which resizes with the zoom in and out. The event coordinates do not scale to your image size.

    In order to get x,y relative to actual picture size, you need to multiply the coordinates against the ratio of current div dimensions and full sized image dimensions.

    Check my Fiddle

    I calculated your x,y by taking the events coordinates, multiplying them by your var w and var h and dividing them by the maps height and width:

    function onMapClick(e) {
    
        var mapWidth=map._container.offsetWidth;
        var mapHeight=map._container.offsetHeight;
        console.log(e.containerPoint.x * w / mapWidth);
        console.log(e.containerPoint.y * h / mapHeight);
        console.log(e);
    
    
    }
    

提交回复
热议问题