Skip to content

Adding a layer to the Map

Felix Obermaier edited this page Jan 10, 2018 · 3 revisions

Creating a Form with a MapControl

Adding a layer to the Map

In the step you will add a layer to the mapcontrol created in the first step of the tutorial.

  1. Add a reference to SharpMap.dll if it's not already added
  2. Open the code for the form by right-clicking the form and choose "View Code"
  3. Add the following code to the constructor of the form
    (The data: states_ugl.shp is located in this zip file on the tutorial overview page)
 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("States");
        vlay.DataSource = new SharpMap.Data.Providers.ShapeFile("path_to_data\\states_ugl.shp", true);
        mapBox1.Map.Layers.Add(vlay);
        mapBox1.Map.ZoomToExtents();
        mapBox1.Refresh();
    }
}
  1. Run the application and you should now see the US states in the map
  2. Set the Active tool to Pan
mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;
  1. Run the application again and you should now be able to pan and zoom (by scrolling the mouse-wheel) in the map

Styling the layer with UniqueValueRenderer

Clone this wiki locally