I am currently trying to build a website using leaflet that allows the dragging of image overlays. I feel so close to getting it right, but for some reason, whenever I zoom in or out the image returns to its original position. I have tried a lot, but whatever I do seems to make it worse at this point lol. Here is the JS code I currently have:
// Function to convert kilometers to latitude/longitude changes
function kilometersToLatLon(kilometers, latitude) {
var latChange = kilometers / 111.0;
var lonChange = kilometers / (111.0 * Math.cos(latitude * Math.PI / 180));
return { latChange: latChange, lonChange: lonChange };
}
// Define the center coordinates (Charleston, SC)
var centerLat = 32.7765;
var centerLon = -79.9311;
// Define image width and height in kilometers
var widthInKilometers = 9.1070;
var heightInKilometers = 8.6625;
// Convert kilometers to latitude and longitude changes
var latLonWidth = kilometersToLatLon(widthInKilometers, centerLat);
var latLonHeight = kilometersToLatLon(heightInKilometers, centerLat);
// Calculate top-left and bottom-right coordinates based on width and height
var topLeftLat = centerLat - latLonHeight.latChange / 2;
var topLeftLon = centerLon - latLonWidth.lonChange / 2;
var bottomRightLat = centerLat + latLonHeight.latChange / 2;
var bottomRightLon = centerLon + latLonWidth.lonChange / 2;
// Path to the image hosted on your local server (Live Server)
var imageUrl = 'http://127.0.0.1:5500/Images/overlay.png';
// Add the image overlay
var imageOverlay = L.imageOverlay(imageUrl, [[topLeftLat, topLeftLon], [bottomRightLat, bottomRightLon]], {
opacity: 0.8,
interactive: true
}).addTo(map);
// Make the ImageOverlay draggable
let imgElement = imageOverlay.getElement();
let draggable = new L.Draggable(imgElement);
// Enable the draggable functionality
draggable.enable();
// Initialize draggable and set the image overlay position relative to the map
draggable.on('dragstart', () => {
map.scrollWheelZoom.disable(); // Disable zoom on the map
});
// Re-enable zoom on the map when dragging ends
draggable.on('dragend', () => {
map.scrollWheelZoom.enable(); // Re-enable zoom on the map
});
// Update the overlay bounds as it is dragged
draggable.on('drag', () => {
const imgPos = L.DomUtil.getPosition(imgElement);
const topLeft = map.containerPointToLatLng(imgPos);
const bottomRight = map.containerPointToLatLng({
x: imgPos.x + imgElement.offsetWidth,
y: imgPos.y + imgElement.offsetHeight,
});
imageOverlay.setBounds([topLeft, bottomRight]);
});
// Remove the draggable instance on map move and re-create it
map.on('moveend', () => {
// Disable the current draggable instance
draggable.disable();
// Recreate the draggable instance after map move ends
draggable = new L.Draggable(imgElement);
draggable.enable(); // Re-enable dragging functionality
});
I have tried a lot of stuff for hours that awlays makes the code messy. Often times, I manage to prevent the issue, but by creating a bigger issue where, if I click on the image to drag it, it gets offset from the mouse corresponding to the offset between my original and current camera position.

