Search code examples
openlayers

Relative Max and Min Zoom - Openlayers


I'm currently working on a project where the goal is to use a static image layer to show realtime data. We've gotten pretty far and things are working very well. One of the issues i've noticed though is that the size of the ImageLayer shown on the map on minimum zoom for example, is relative to the browsers resolution. I've done some research in Openlayers documentation, but i can't find a exact way to work around this. Also set the resolution to 1 in the view when initalizing the map, but there are still some browser resolutions that do not show the whole map while fully zoomed out.

We're using a static ImageLayer where the projection is the custom coordinate-system we use on the map.

Is there any way around this? So that the minZoom and maxZoom are relative to the browsers resolution?


Solution

  • As said in the OpenLayers site:

    The zoom option is a convenient way to specify the map resolution. The available zoom levels are determined by maxZoom (default: 28), zoomFactor (default: 2) and max resolution (default is calculated in such a way that the projection's validity extent fits in a 256x256 pixel tile). Starting at zoom level 0 with a resolution of maxResolution units per pixel, subsequent zoom levels are calculated by dividing the previous zoom level's resolution by zoomFactor, until zoom level maxZoom is reached.

    So you can find min and max resolution or any zoom resolution with view.getResolutionForZoom(zoom) while the zoom is between 0-28. you just need to adjust zoom levels for your layers and calculate resolution at the loading of layer and set their properties.

    But this might not accurate enough like you said. There is also another way, first of all, get the layer's extent and fit your map to layer. there is a method for it view.fit(geometryOrExtent, opt_options) you can find more information here. then set the current resolution for maxResolution of map.
    After this you can make sure the whole layer will be visible.

    For better map boxing you can decrease the zoom number of layer extent by one or two. it makes the layer fits better in map.