ThinkGeo.com    |     Blog    |     Wiki    |     Support

Dynamically restrict zoom?

Is it possible to dynamically change the allowable zooms? I’m trying to adjust the zoom levels depending on if a particular layer is displayed. When I exceed the set zoom, the display changes to all pink, and still lets me zoom in further.

Is there something I need to do on the client side to enable this? I’ve attached a sample project so you can see what I’m trying to do. Click on Show U.S. States then zoom in twice to get the pink display.ZoomBug.zip (1.8 MB)

Hi Jay,

Thanks for your sample.

Your code have some problem.

If you want to make this layer disappear for zoomlevel 6, please just set the style like this:

ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(Server.MapPath("~/App_Data/USStates.shp"));
            worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.State1;                
            worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level05;

Which only make this layer visible between level 1 to 5.

The code as below shows how to make the layer render for different zoomlevel:

ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(Server.MapPath("~/App_Data/USStates.shp"));
            worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.State1;
            worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level05;

            // We don't set style for 6 and 7, only show level 8

            worldLayer.ZoomLevelSet.ZoomLevel08.DefaultAreaStyle = AreaStyles.State1;

            // Ignore 9 and render 10 to 20

            worldLayer.ZoomLevelSet.ZoomLevel10.DefaultAreaStyle = AreaStyles.Grass1;
            worldLayer.ZoomLevelSet.ZoomLevel10.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

If you want to control this totally in client side, please find the target overlay in JavaScript, and directly make it hide by the OpenLayer’s API.

Wish that’s helpful.

Regards,

Don

Hi Don, thanks for the reply. It’s not that I want the layer to disappear for zoom level 6, it’s that I’d like it to not zoom past zoom level 6. For instance, if I change the Index() method to be

    public ActionResult Index()
    {
        Map map1 = new Map("Map1", 600, 500);
        map1.MapBackground = new BackgroundLayer(new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF")));
        map1.CurrentExtent = new RectangleShape(-138, 48, -55, 28);
        map1.MapUnit = GeographyUnit.DecimalDegree;

        WorldMapKitWmsWebOverlay worldMapKitOverlay = new WorldMapKitWmsWebOverlay();
        map1.CustomOverlays.Add(worldMapKitOverlay);

        LayerOverlay overlay = new LayerOverlay("ShapeOverlay", false, TileType.MultipleTile);
        map1.CustomOverlays.Add(overlay);

        ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(Server.MapPath("~/App_Data/USStates.shp"));
        worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.State1;
        worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level05;
        worldLayer.ZoomLevelSet.ZoomLevel06.Scale = 0;
        worldLayer.ZoomLevelSet.ZoomLevel06.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

        overlay.Layers.Add(worldLayer);

        map1.ZoomLevelSet = worldLayer.ZoomLevelSet;

        return View(map1);
    }

then the states display initially, the pan zoom bar shrinks, and I can’t zoom past level 6. Is there a way to do this same thing dynamically?

Hi Jay,

Please try following code:
Client:

function UpdateDynamicOverlay(layerName) {
    Map1.ajaxCallAction("Home", "UpdateOverlay", { layer: layerName }, function (result) {
        Map1.baseLayer.numZoomLevels = (layerName == 'US' ? 5 : 20);

        var zoom = Map1.getZoom();
        if (Map1.getZoom() > Map1.baseLayer.numZoomLevels) {
            zoom = Map1.baseLayer.numZoomLevels - 1;
        }
        Map1.zoomTo(zoom);
        // Refresh overlay after callback
        Map1.redrawLayer("ShapeOverlay");
        
     
    })
}

Server:

[MapActionFilter]
    public void UpdateOverlay(Map map, GeoCollection<object> args)
    {

        var layerName = args["layer"].ToString();

        LayerOverlay overlay = map.CustomOverlays["ShapeOverlay"] as LayerOverlay;
        overlay.Layers.Clear();
        if (layerName.ToLowerInvariant() == "us")
        {
            // States layer
            ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(Server.MapPath("~/App_Data/USStates.shp"));
            worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.State1;
            // worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
            worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level05;
           // worldLayer.ZoomLevelSet.ZoomLevel06.Scale = 0;
            //worldLayer.ZoomLevelSet.ZoomLevel06.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

            overlay.Layers.Add(worldLayer);

            map.ZoomLevelSet = worldLayer.ZoomLevelSet;
        }
        else
        {
            // Cities layer
            ShapeFileFeatureLayer countyLayer = new ShapeFileFeatureLayer(Server.MapPath("~/App_Data/cities_a.shp"));
            countyLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.Capital3;
            countyLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.Capital3("AREANAME");
            countyLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

            overlay.Layers.Add(countyLayer);

            map.ZoomLevelSet = countyLayer.ZoomLevelSet;

        }
    }

Thanks,
Peter

Thanks, Peter. That’s just what I needed. I tweaked the client side slightly to adjust the slider position and redraw it:

function UpdateDynamicOverlay(layerName) {
    Map1.ajaxCallAction("Home", "UpdateOverlay", { layer: layerName }, function (result) {
        Map1.baseLayer.numZoomLevels = (layerName == 'US' ? 5 : 20);

        var zoom = Map1.getZoom();
        if (Map1.getZoom() > Map1.baseLayer.numZoomLevels) {
            zoom = Map1.baseLayer.numZoomLevels - 1;
        }
        Map1.zoomTo(zoom);

        var zoomBar = Map1.getMapTools().panZoomBar;
        zoomBar.moveZoomBar();
        zoomBar.redraw();

        // Refresh overlay after callback
        Map1.redrawLayer("ShapeOverlay");
    })
}

Hi Jay,

I am glad to hear that’s helpful.

Any question please let us know.

Regards,

Don