How to set scale in Openlayers 8

89 Views Asked by At

I am using openlayers 8 and I want to set different scales like 1:25000, 1: 50000, 1:100000 and 1:250000 based on the value selected from a dropdown.

I tried to calculate the resolution from the scale and then set the resolution of the map like this:

const dpi = 25.4 / 0.28;
const mpu = METERS_PER_UNIT[units];
const inchesPerMeter = 1000 / 25.4;
const resolution = (parseFloat(scale) / (mpu * inchesPerMeter * dpi));

map.getView().setResolution(resolution);

But this is not setting the correct scale.

1

There are 1 best solutions below

0
Mike On
  const resolution =
    parseFloat(scale) /
    getPointResolution(
      map.getView().getProjection(),
      1 / 0.28,
      map.getView().getCenter()
    );
  map.getView().setResolution(resolution);

will match the scale in an OpenLayers scale bar https://codesandbox.io/s/simple-forked-c5ngpy?file=/main.js