mobility-toolbox-js
Version:
Toolbox for JavaScript applications in the domains of mobility and logistics.
18 lines (17 loc) • 620 B
JavaScript
import { getWidth, getHeight } from 'ol/extent';
import { fromLonLat } from 'ol/proj';
/**
* Get the current resolution of a Maplibre map.
* @param {maplibregl.Map} map A map object.
*/
const getMercatorResolution = (map) => {
const bounds = map.getBounds().toArray();
const a = fromLonLat(bounds[0]);
const b = fromLonLat(bounds[1]);
const extent = [...a, ...b];
const { width, height } = map.getCanvas();
const xResolution = getWidth(extent) / width;
const yResolution = getHeight(extent) / height;
return Math.max(xResolution, yResolution);
};
export default getMercatorResolution;