OpenDiachronicMaps/scaleGesture.js
2023-10-28 17:35:06 +02:00

26 lines
No EOL
1 KiB
JavaScript

L.control.scale().addTo(map);
function pixelsInOneMeterWidth() {
var div = document.createElement('div');
div.id="mesure";
div.style.cssText = 'position: absolute; left: -100%; top: -100%; width: 100cm';
document.body.appendChild(div);
var px = div.offsetWidth;
document.body.removeChild(div);
return px;
}
function scaleManage(){
var pos = map.getCenter();
var screenWidth = screen.width;
var wFrameInPx = document.getElementById("map").clientWidth;
var mapSpan = map.containerPointToLatLng([0, 0]).distanceTo(map.containerPointToLatLng([0, wFrameInPx]));
var frameInMeter = wFrameInPx/pixelsInOneMeterWidth();
return [pos,screenWidth,wFrameInPx,mapSpan, pixelsInOneMeterWidth(), frameInMeter, mapSpan/frameInMeter];
}
const widthInMeter = 6378137 * 2 * Math.PI * Math.cos(map.getCenter().lat * Math.PI / 180);
var scaleOutput = document.getElementById("scaleOutput");
scaleOutput.innerHTML = widthInMeter;
map.on('moveend', function(){
scaleOutput.innerHTML = widthInMeter;
});