Venturing around in the OpenLayers source code, it seems like everything related to zoom-levels is done using resolutions. Fair enough since this related directly to screen space.

I would like though, to use scales instead, and further more explicitly define the allowed scales.
- Is this possible?

So far I have created a set of scales which I transform into resolutions:

var scales = [500, 1000, 2000, 4000, 10000, 25000, 50000];
var resolutions = [];
for(var i = 0; i < scales.length; i++) {
    resolutions.push(OpenLayers.Util.getResolutionFromScale(scales[i], units));

map = new OpenLayers.Map('', {                        
    minScale: scales[scales.length - 1],
    maxScale: scales[0],        
    resolution: resolutions,
    numZoomLevels: scales.length,

My problem is that the first and the last scale are respected, but in between the current resolution converted into scale is not among the ones I specified.
- What have I missed?


In order to use scales on your map:

  • Set only the 'scales' option. Do not set maxResolution, minResolution, maxScale, minScale, numZoomLevels, or any other scale related property. Set it to an array of scale denominators (or scales).
  • Configure a unit: note that for meters, this should be 'm', for degrees, 'degrees', etc.
  • You can set a maxExtent. Do not set a minExtent.

Once you do this, the scales should be used on your map. (Note that they will be ordered from most zoomed out -- highest scale denominator -- to most zoomed in -- smallest scale denominator.)

In your case, this should look exactly like:

map = new OpenLayers.Map('map', {                        
  scales: scales,
  units: 'm' // or whatever       

Then add your layer.

Note that layer-based resolution options will 'control' the map; you should typically only specify these things on the map or the layers, not both.

Note that the base layer controls the overall scales of the map; overlays will be entirely controlled by base layers.

Note that OpenLayers uses a DPI assumption of 72. To control this DPI setting, you can set OpenLayers.DOTS_PER_INCH before creating your map. A typical value (used by the SLD spec) will be different, so if this assumption matters to you, you should change this configuration parameter to match your server.

 var options = { scales: [50000000, 30000000, 10000000, 5000000],
                resolutions: [1.40625,0.703125,0.3515625,0.17578125,0.087890625,0.0439453125],
                minScale: 50000000,
                maxResolution: "auto",
                maxExtent: new OpenLayers.Bounds(-180, -90, 180, 90),
                maxResolution: 0.17578125,
                maxScale: 10000000,
                minResolution: "auto",
                minExtent: new OpenLayers.Bounds(-1, -1, 1, 1),
                minResolution: 0.0439453125,
                numZoomLevels: 5,
                units: "degrees"
map = new OpenLayers.Map( $('map') , options);

Units needs to be set: units: "degrees"

String - The units in which to display the layer.

This affects the Scale-Resolution calculation.

Official source: http://trac.openlayers.org/wiki/SettingZoomLevels

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.