maxZoom and User Input

19,892

Solution 1

For setting maximum zoom level :

map._layersMaxZoom=15 (which sets the maximum zoomlevel as 15)

Similarly we can set minimum zoom level:

map._layersMinZoom=2, (which sets minimum zoomlevel as 2).

Another way of doing the same-

map.options.maxZoom = 15;

map.options.minZoom = 10;

Solution 2

You can set the map's maximum zoom level dynamically with map.options.maxZoom in those special conditions and set it back when you are done.

Solution 3

This works for me:

L.tileLayer('...', { minZoom: 5, maxZoom: 15 }).addTo(map)

Solution 4

If you're displaying the controls and you change the maxZoom or minZoom options, the controls won't be updated. For example, if your min zoom level is 10 and the current zoom level is 10, the zoom out control will appear disabled even if you change the zoom level.

One workaround for this solution is to trigger a 'zoomend' event on the map:

map.options.minZoom = 9;
map.fire('zoomend');

You might also want to consider calling the setZoom() method if you change the max/min zoom levels in a way that invalidates the current zoom level.

Share:
19,892

Related videos on Youtube

Mia
Author by

Mia

Updated on June 04, 2022

Comments

  • Mia
    Mia almost 2 years

    I want to limit the users max zoom level with inputs (muse scroll etc.) and buttons. In other words, I limit the "maxZoom" to -lets say- level9. But I still want to be able to zoom to level10 in special conditions. When I set the maxZoom, it obviously does not let me zoom more in any way, so wht is my solution in this case?

  • flup
    flup about 11 years
    That's the internal interface, the public way is through setting the options.
  • Fedir RYKHTIK
    Fedir RYKHTIK almost 11 years
    map.options.maxZoom = 15;map.options.minZoom = 10;
  • subhaze
    subhaze about 10 years
    As @flup stated, the public way of doing this is via the options leafletjs.com/reference.html#map-options
  • Andy E
    Andy E about 9 years
    Note that if you're displaying controls, this won't update disabled/enabled controls to reflect the new values.
  • SteveGSD
    SteveGSD almost 9 years
    It appears that setting these options via the internal method doesn't persist correctly. I'd only use the public options method.
  • Inserve
    Inserve over 7 years
    Thanks, my controls did not update indeed. using the fire('zoomed') function they do.
  • Muthu
    Muthu about 6 years
    Any limit is there in max and min zoom level