This project has moved and is read-only. For the latest updates, please go here.

Restricted Extents

Topics: SharpMap v0.9 / v1.x
May 19, 2014 at 3:35 PM

Is there a way I can stop the user from:
  1. zooming out beyond a set extent
  2. zooming in beyond a certain scale
  3. pan outside of a set extent
I know there is an maximum extent property on the map, but its really the zooming in and panning that I'm looking at.

May 20, 2014 at 8:07 AM
If you set MaximumExtent you need to set map.EnforceMaximumExtent = true;

The MapViewportGuard class currently does not enforce a minimum scale but that should be fairly easy to handle.
May 20, 2014 at 9:27 AM
Hi Felix,

Would you recommend a modification to the Mapviewportguard, or just hook the map zoom event and if the zoom is beyond the threshold, set it to the minimum?
May 20, 2014 at 10:13 AM
Edited May 20, 2014 at 11:27 AM
Robert_Smart wrote:
Would you recommend a modification to the Mapviewportguard
yes, and provide a patch or check it in :)
May 20, 2014 at 2:58 PM
Also is there a way I can just lock the map? so I can just fix an extent and stop any panning, zooming etc
May 21, 2014 at 3:51 PM
I just noticed, everything is in place:
map.MaximumExtent = map.Envelope;
map.MinmumZoom = map.MaximumZoom = map.Zoom;
map.EnforceMaximumExtents = true;
May 21, 2014 at 4:22 PM
May 22, 2014 at 3:25 PM
I've tried to make so that the map can't zoom out beyond the extents of the background map and the behaviour is almost there. I'm using this method

private void FixMaxExtents()
        Envelope  env =null;
        foreach (var layer in Map.BackgroundLayer)
            if (env == null)
                env = layer.Envelope;

        Map.MaximumExtents = env;
        Map.EnforceMaximumExtents = true;
The behaviour is almost right, but it seems to fit the extent to the widest dimensions of the mapbox and the extent. For Example, I'm loading a standard OSM tilelayer into a rectangular window (wider rather than taller). The above method fixes the extents so that it cannot be panned left to right, but it can still be panned up and down. So it fits the extent to the widest dimension of the display. I really need it to fit the smallest dimension that the whole map is displayed with no panning in either direction. I can't help but think there must be some simple math to make this happen. Maybe expand the envelope vertically by the height/width ratio?
May 23, 2014 at 8:28 AM
The code you posted fixes the extent to the maximum extent of the background layer. It does not take into account any map envelope to map size ratio. If the extent you evaluate is square, but your MapBox is not, the MapViewPortguard will allow panning at MaximumExtent in the direction of the smaller side of the MapBox.

If you don't want that, you need to somewhat change the extent that it matches the width/height ratio of the mapbox control.
May 23, 2014 at 10:49 AM
I have investigated a bit more and the behaviour is still a bit odd.

I noticed that if load the data and call Map.ZoomToExtents. Then the extents are set how I would expect.

So I changed the code to this:
        var env = Map.GetExtents();
        Map.MaximumExtents = Map.GetExtents();
        Map.EnforceMaximumExtents = true;
And it still fits the map to largest dimension, rather than the smallest.

I went off the idea of calculating the ratio of height to width as this would have to be calculated on every resize.

My use case is that I'm displaying a png image (a schematic map with other polygons etc drawn over the top) and I need an option to lock the extent so it can't be zoomed in on. So I really need the whole image displayed no matter the shape of the window. As it is it only fits it to the largest dimension