Search code examples
javascripttypescriptzoomingopenlayers

Openlayers map scale line same zoom rate repeated twice in metric even-though the zoom differs


Openlayers Map scale line in Metric unit has a particular zoom rate repeated twice on zoom eventhough the actually zoom-in resolution differs in map.

https://openlayers.org/en/latest/examples/scale-line.html As you can see in the link the zoom rates 5km and 5m are repeated twice for two different zoom levels

How i declared my zoom rates in view.

var viewLayer = new ol.View({
            center: [0, 0],
            zoom: 10,
            minZoom: 15,
            maxZoom: 24,
            resolution: 0.2,
            minResolution: 0.0125,
            maxResolution: 6.4
        });

Scale line control declaration as

var scaleLineControl = new ol.control.ScaleLine({
            minWidth: 100,
            units: 'metric'
        });

With the above declaration ranging from 1000m to 1m - 5m is repeated twice in metric unit


Solution

  • To avoid duplicates you must use a resolutions array to match the scale line intervals

    The scale line intervals are fixed like this

    5000, 2000, 1000, 500, 200, 100, 50, 20, 10, 5, 2, 1
    

    If you don't use a resolutions array OpenLayers fixes the rsolutions at even intervals between the min and max, for example

    4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1
    

    so (in that case) you would have two resolutions which fit between 2000 and 5000 on the scale line