How to Add a Map to a Windows Forms Application

Introduction

DotSpatial is an open-source project that contains controls which can be used to manipulate and display geographic information. This article explains how get DotSpatial mapping inside of your existing application.

Using NuGet

DotSpatial can be installed as a standard NuGet package. This means you can easily get mapping functionality in your existing application. For this tutorial, we assume you have already installed NuGet (and are using a version of Visual Studio that supports NuGet).

DotSpatial is broken out into a number of NuGet packages. The one with a map control for Windows Forms in DotSpatial.Controls. We’ll actually use the DotSpatial.Controls.Sample package, which has a dependency on DotSpatial.Controls and brings a sample form into our project. Because NuGet packages modify projects, and not Visual Studio, a package isn’t able to add controls to the Toolbox, but we can copy the map from the sample form into the correct place in our project or just use the form directly.

DotSpatial.Controls.Sample package is designed to work with C# Windows Forms applications.

Adding a reference to DotSpatial

Open your existing project in Visual Studio. Use the package manager (Project, Manage NuGet Packages…) to search for DotSpatial.Controls.  Select DotSpatial.Controls.Sample and install it.

This will install all of the needed dependencies and add MapForm.cs to our project. When we close the package manager we may be prompted to reload our project. As of NuGet 1.8 adding forms to a project is not directly supported, and the project is programmatically edited to add the form properly. Reload the project (as prompted).

image

Instantiating MapForm

You can examine MapForm in the designer, and will note that it displays a DotSpatial map control that is docked to fill the form. You can copy this control to an appropriate place in your application or display the form as demonstrated below. I show the form on a button click. This is rather trivial because the MapForm is added to your application’s root namespace.

private void showMapButton_Click(object sender, EventArgs e)
{
    var form = new MapForm();
    form.Show();
}

Build and run your application!

After running the application I loaded an image onto the map by dragging it onto the map control. Then, I zoomed in by using the spacebar and scroll wheel.

image

Points of Interest

Visit http://dotspatial.codeplex.com to learn how to get more out of your DotSpatial map control.

Advertisements

3 thoughts on “How to Add a Map to a Windows Forms Application

    • You’ll need VS2012 Express so that you can use the Nuget Package Manager. I’ve haven’t tested it, though, so you may have to experiment a bit.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s