How to initialize correctly the zoom level in a SharpMap object?

1.1k Views Asked by At

I'm working with a MapBox object from SharpMap in a Windows Forms C# app and I can't seem to find how to make the zoom level work. The MapBox object is in a TabControl object.

This class is called after the main InitializeComponent() call in my app.

public void InitializeMap(SharpMap.Forms.MapBox mapBox)
        {
            mapBox.Map.BackgroundLayer.Add(new SharpMap.Layers.TileAsyncLayer(
                new BruTile.Web.OsmTileSource(), "OSM"));

            mapBox.Map.Zoom = 10; //
            mapBox.Refresh();
        }

Whatever value written for the mapBox.Map.Zoom attribute, it's always the same high zoom level.

On the other hand, if I use mapBox.Map.ZoomToExtents() call, it zooms to the extents of the OSM layer correctly.

I suppose I'm doing wrong a simple thing, but I can't seem to find what is the problem.

1

There are 1 best solutions below

0
On

mapBox.Map.Zoom is always the same as mapBox.Map.Envelope.Width.

Instead of just setting the zoom like that, try using mapBox.Map.ZoomToBox(new GeoAPI.Geometries.Envelope(-5.0, 5.0, -5.0, 5.0)). The map will be positioned and zoomed so that the entirety of the envelope is within the map.

The envelope in my example has a width of 10, but if your map is wider than it is high mapBox.Map.Zoom will be greater than that, since (as I stated above) its value is the same as the width of the visible part of the map.