0
votes

How can i set the scale of a map in openlayers 4.4 with an input value like "1:1000" or "1:25000" ? I try this without succes from the exemple http://mikenunn.16mb.com/demo/ol5-osm-map-print.htm

    var scale = document.getElementById('echelleVal').value;
    var size = map.getSize(); 
    var viewResolution = map.getView().getResolution();
    var width = Math.round(size[0] * viewResolution / 25.4);
    var height = Math.round(size[1] * viewResolution / 25.4);
    var scaleResolution = scale/ol.proj.getPointResolution(map.getView().getProjection(), viewResolution / 25.4, map.getView().getCenter());
    // Set size
    var newsize = [width, height];
    map.setSize(newsize);
    map.getView().setResolution(scaleResolution);
1

1 Answers

1
votes

Scale only has a precise meaning on a printed map where the size of the output is known and fixed. For on-screen maps the browser and operating system do not know the physical size of the monitor, only the number of pixels it uses, so the nominal scale for on-screen maps usually based on a presumed pixel size of 0.28mm (90.7142857143 dpi), while operating systems typically assume 72dpi or 96dpi. In this example https://openlayers.org/en/latest/examples/print-to-scale.html the on-screen scalebar presumes a 0.28mm pixel size regardless of the monitor but is updated when the map is prepared for printing to a device or pdf of known physical size.