[GIS] prevent osm to load tiles on zoom level > 19

openlayers-2openstreetmap

layer_osm = new OpenLayers.Layer.OSM( "Simple OSM Map","",{
        isBaseLayer : true,
        displayInLayerSwitcher : false,
        numZoomLevels: 26,
    });

I have several WFS and WMS Layers on Top of a OSM Base Layer, and I want to stop loading the OSM layer at zoom 19, which is the max zoom for OSM. When I increase the zoom level above 19, I get 404 when loading the OSM tiles. I simply want my OSM Layer to stop loading tiles when zoom level > 19. How can I do that?

Best Answer

You can use the setOpacity function to show/hide the layer_osm by detecting the zoom level on each zoom change. For example, use the below snippet

map.events.register("zoomend", map, function(){
     var zoom = map.getZoom();
     console.log(zoom);
     if(zoom>19){
        layer_osm.setOpacity(0);
     }
     else{
        layer_osm.setOpacity(1);
     }
});

Here is a working fiddle. Zoom in the map and when you'd exceed the zoom level 19, you see that OSM layer hides while the WMS layer stays there.