User Tools

Site Tools


map_suite_services_edition_quick_start_guide

Map Suite Services Edition Quick Start Guide

Note: The page was created before Map Suite 10. Map Suite 10.0 organized many classes into new namespaces and assemblies as well as had a few minor breaks in compatibility. The majority of previously built code should work without modification assuming the new namespaces are added. For guidance on upgrading your existing code, please check out MapSuite 10 Upgrade Guide.

The Map Suite Services Edition illustrated QuickStart Guide will guide you through the process of creating a sample application and will help you become familiar with Map Suite. This edition of the QuickStart Guide supports Services Edition 9.0.0.0 and higher.

Welcome to Map Suite™ from ThinkGeo, a full-featured mapping control that makes it easy for any Microsoft .NET developer to add mapping functionality to a Microsoft .NET application quickly and efficiently. Using the intuitive object model, even developers inexperienced in Geographic Information Systems (GIS) can have fully functional maps working in minutes.

The purpose of this guide is to help you quickly get started building your own spatially aware applications. Like any new software, there is some learning to be done along the way.

How do we start to learn how to take advantage of the power of Map Suite? The best way is to make a sample application with it.

Before we get started, make sure that you have installed the ThinkGeo Product Center and that you have either started an evaluation or activated a full license of Map Suite Services Edition. By default, this will install the Map Suite Services Edition 9.0 assemblies to C:\Program Files (x86)\ThinkGeo\Map Suite 9.0\Map Suite Services.

Download the Sample

Setting up the Environment

Let's start with a new Windows Forms project in Microsoft Visual Studio (2010 or newer) and call it HelloWorld (see Figure 1). We can create the project with .NET Framework 4.0 or 4.5.

qsg_servicesedition2_img01.jpg
Figure 1. Creating a new project in the Visual Studio.NET 2010 or 2012 IDE.

The project HelloWorld is created in a new solution called HelloWorld. The wizard creates a single Windows Form.

Next, we need to add “MapSuiteCore.dll” to the reference. Right-click the project in solution explorer and select “Add Reference…”, navigate to the C:\Program Files (x86)\ThinkGeo\Map Suite 9.0\Map Suite Services\Current Version\Managed Assemblies folder and select “MapSuiteCore.dll”.

Note: If you are using Map Suite Services Edition version 9.0.0.0 or later, you also need to add “WindowsBase.dll” to the references. WindowsBase can be found on the .NET tab of the Add Reference dialog. If you don't do this, you will get the following error when you compile the project: “The type 'System.Collections.Specialized.INotifyCollectionChanged' is defined in an assembly that is not referenced. You must add a reference to assembly 'WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.”

qsg_servicesedition2_img02.jpg

To make it simple, we will display the map using Map Suite Services Edition in a PictureBox. Draw a PictureBox on the form by clicking on the PictureBox object in the Toolbox and then dragging and dropping (using the left mouse button) to the size you desire. You can leave the name set to pictureBox1. Our map will display in the PictureBox.

qsg_servicesedition2_img03.jpg

Now that we have our “MapSuiteCore.dll” referenced and a pictureBox added, we are ready to add the code.

Map Suite Services Edition "Hello World" Sample

After this section, you will be able to draw a map with Map Suite Services Edition using your own data. At the very beginning, let's have a look at the data and the important objects we will use.

Shapefiles

Map Suite Services Edition 9.0 supports multiple kinds of data sources, such as SQL Server 2008, Postgre, Oracle, etc. Here we will provide more introductions on Shapefiles which we will use in this QuickStart Guide.

Simply put, Shapefiles are used by Map Suite to provide data that we will use to draw our map. Shapefiles store binary vector coordinates to be used by the component. They have an .shp extension. Shapefiles may also come with two supplementary files that help Map Suite to work with the data.

The first supplementary file is called the .shx file. Its purpose is to provide a simple index of the main Shapefile. It tells the Map Suite component when to start reading binary data and when to stop. It is much like a directory for reading the binary data, sort of a lookup mechanism.

The second supplementary file is the .dbf file. This file holds tabular data associated with the main Shapefile. For example, the Shapefile may have the coordinates for a line to be drawn that represents a road. The .dbf file may have information to tell you what the name of the road is or what type of road it is, such as county road, state road, interstate highway, etc.

All three files need to reside in the same directory as the main Shapefile (.shp), but the Map Suite component only expects you to designate the name and file path of the main Shapefile. Next, when we discuss layers, you will start understanding a little more about how maps are constructed in Map Suite using the shape data.

ShapeFileFeatureLayers

A ShapeFileFeaturelayer in a map correlates to a single Shapefile, such as networks of roads. You can think of layers much like actual terrain in the real world. The bare earth might be a layer and have either physically defined boundaries, such as a fence around a military installation, or legal boundaries, such as the border of a country. Another layer on top of that might be roads that are built upon the bare earth. It is important to understand this when working with layers, as they need to be added in the logical order you might expect so that they can be visualized correctly from above. In other words, you would not want to lay down roads and then cover them with earth, because they could not be seen or used by vehicles.

How do we create and add layers? First, you should know that there are three types of styles that layers represent. As mentioned above, it follows logically that you would create and add layers based on how they should be viewed, so naturally you might start with some polygons, such as the outline of a country and all of the regions within it. You might then lay down lines that represent rivers and roads and then finally you might lay down points like cities or places of interest. Again, keep in mind that logic will dictate what works best. For instance, laying down roads and then rivers would put rivers on top of roads when it should more than likely be the other way around (the exception here might be a tunnel!).

MapEngine

A MapEngine object is the highest level object that encompasses layers and some other objects. For now, you can think of a MapEngine as a set of layers, which can render each layer and present you with a map based on the extent you want to display.

Styles

Shapefiles provide the data, but Styles are the way you color and draw them. You can specify the color of the country, the width of a road, the shape (triangle, circle, cross etc) of a point, and so on.

Map Suite has many preset Styles built in, including predefined Styles for roads, rivers, cities, countries, and more. This makes it easier to create great looking maps without a lot of hassle.

PresetZoomLevels

Styles define the way we visually represent the data, while ZoomLevels define the situation in which we want to display them. The reason why we need ZoomLevels is because we may want to display a small town when we are zoomed into a state, but we definitely don't want to display that town when we are zoomed out and looking at the entire country.

We have provided the 20 most common scales, from ZoomLevel01 to ZoomLevel20, at which you may want to change the way your data looks. What is scale? Scale indicates how much the given area has been reduced. If a road is 10,000 inches long in the real world and a map depicts this length as 1 inch on the screen, then we say the scale of this map is 1:10,000. Now let's say ZoomLevel02 uses a scale of 1:500 and ZoomLevel03 uses a scale of 1:1200. This means the map with a current scale of 1:1000 matches ZoomLevel03, the ZoomLevel whose scale is the closest to that.

PresetZoomLevels has a very useful property called ZoomLevel.ApplyUntilZoomLevel, which you can you can very easily use to extend your ZoomLevels. Let's say you want a particular Style to be visible at ZoomLevel03 through ZoomLevel10. To make that work, we can simply code as follows:

worldLayer.ZoomLevelSet.ZoomLevel03.DefaultAreaStyle = AreaStyles.Country1;
worldLayer.ZoomLevelSet.ZoomLevel03.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level10;

Map Suite Services Edition "Hello World"

In creating our “Hello World” sample application, our first step is to set a reference to the Map Suite workspace at the very top of our code, before any other code. We do this so that we do not have to use the fully qualified name of the Map Suite classes throughout our code. Setting a reference to the Map Suite workspace can be done in the “code-behind” of the Form by selecting the Form and hitting the F7 function key. Set the reference like this:

using ThinkGeo.MapSuite.Core;

Now let's look at a code sample to bring this concept to fruition. We'll look at Shapefiles relating to the entire world. In our example, we have one such Shapefile:

  • The borders of every country in the world (“Countries02.shp”)

(NOTE: The data used in this sample can be found in the attached sample above at “\AppData” folder)

Our next step is to define and add our layers. Here is the code to use for our example, which includes the Form1_Load event of the form as well as one module member.

private MapEngine mMapEngine = new MapEngine();
    private void Form1_Load(object sender, EventArgs e)
    {
        // We create a new Layer and pass the path to a Shapefile into its constructor.
        ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(@"../../AppData/Countries02.shp");
 
        // Set the worldLayer with a preset Style, as AreaStyles.Country1 has YellowGreen background and black border, our worldLayer will have the same render style. 
        worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
 
        // This setting will apply from ZoonLevel01 to ZoomLevel20, that means we can see the world the same style with ZoomLevel01 all the time no matter how far we zoom out/in.
        worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
 
        // We need to add the world layer to the mapEngine's Static Layer collection.
        mMapEngine.StaticLayers.Add(worldLayer);
        // Set a proper extent for the Map. ExtentHelper.GetDrawingExtent will modify the extent based on the width/height of the canvas.
        mMapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(0, 78, 30, 26), pictureBox1.Width, pictureBox1.Height);
 
        // Create a bitmap to prepare for drawing on.
        Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
 
        // Open the layer and draw it on the bitmap.
        mMapEngine.OpenAllLayers();
        // We set the unit of measurement to DecimalDegree because this is the inherent unit in the "Countries02.shp" file.
        mMapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree);
        mMapEngine.CloseAllLayers();
 
        // Set the bitmap to pictureBox1.
        pictureBox1.Image = bitmap;
    }

If you compile and run what you have now, your map should look like the one below. (see Figure 3).

qsg_servicesedition2_img05.jpg
Figure 3. A simple map of Europe.

So what has occurred here? We have created a layer and added it to the MapEngine, and the MapEngine has rendered it according to its default style parameters. Also, we have used ZoomLevel to display the map the way we want.

NOTE: As you can see in the code, MapEngine has a very important method called DrawStaticLayers, which you can use to get the map you want. It is crucial that you correctly set the map's unit of length measurement in that method, since Shapefiles only store binary vector coordinates (that can be in DecimalDegrees, feet, etc.) and our MapEngine has no idea what the unit is until we set explicitly. Information on what unit of measurement to use is normally found somewhere in the Shapefile documentation or within its supplemental data file.

That was easy, wasn't it? Let's add another Shapefile to the sample so that we will have a total of two layers:

  1. The borders of every country in the world (“Countries02.shp”)
  2. The capitals of the world countries (“WorldCapitals.shp”)
    private void Form1_Load(object sender, EventArgs e)
    {
        ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(@"../../AppData/Countries02.shp");
        worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
        worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
 
        ShapeFileFeatureLayer capitalLayer = new ShapeFileFeatureLayer(@"../../AppData/WorldCapitals.shp");
        // Similarly, we use the presetPointStyle for capitals.
        capitalLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.Capital3;
        // This setting also applies from ZoomLevel01 to ZoomLevel20, which means we can see capital symbols the same style with ZoomLevel01 all the time.
        capitalLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
 
        mMapEngine.StaticLayers.Add(worldLayer);
        mMapEngine.StaticLayers.Add(capitalLayer);
 
        mMapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-11, 70, 34, 30), pictureBox1.Width, pictureBox1.Height);
 
 
        // Display the map.
        Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        mMapEngine.OpenAllLayers();
        mMapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree);
        mMapEngine.CloseAllLayers();
        pictureBox1.Image = bitmap;
    }

The result is as follows (Figure 4):

qsg_servicesedition2_img06.jpg
Figure 4. Europe map with 2 layers.

How to Use TextStyle

TextStyle

TextStyle are used to label items on map. As every Shapefile has a relative .dbf file, which includes descriptions for every record, the most common way to use TextStyles is for labeling. For example, WorldCapital Shapefile's corresponding .dbf file contains the field “CITY_NAME”. We can use this field to label the cities on our map.

qsg_servicesedition2_img07.jpg

Map Suite has many TextStyles built in, which will help us quickly design attractive labels for the cities on our map. We can just pick the TextStyle we like and use it.

    private void Form1_Load(object sender, EventArgs e)
    {
        ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(@"../../AppData/Countries02.shp");
        worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
        worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
 
        ShapeFileFeatureLayer capitalLayer = new ShapeFileFeatureLayer(@"../../AppData/WorldCapitals.shp");
        capitalLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.Capital3;
        capitalLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
 
        // We create a new Layer for labeling the capitals.
        ShapeFileFeatureLayer capitalLabelLayer = new ShapeFileFeatureLayer(@"../../AppData/WorldCapitals.shp");
        // We use the preset TextStyle. Here we passed in the “CITY_NAME”, which is the name of the field we want to label on map.
        capitalLabelLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.Capital3("CITY_NAME");
        capitalLabelLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
 
        mMapEngine.StaticLayers.Add(worldLayer);
        mMapEngine.StaticLayers.Add(capitalLayer);
        // Add the label layer to MapEngine.
        mMapEngine.StaticLayers.Add(capitalLabelLayer);
 
        mMapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-11, 70, 34, 30), pictureBox1.Width, pictureBox1.Height);
        // Set the background brush to color the sea.
        mMapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);
 
        // Display the map.
        Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        mMapEngine.OpenAllLayers();
        mMapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree);
        mMapEngine.CloseAllLayers();
        pictureBox1.Image = bitmap;
    }

The result is as follows (Figure 5):

qsg_servicesedition2_img08.jpg
Figure 5. Europe map with TextStyle.

Before we go to the next step, let's first add some navigation capabilities, such as zooming in and out, to our map.

Zooming In and Zooming Out

Add two buttons to the form, one with the text “Zoom In” and the other with “Zoom Out” (let's name the buttons btnZoomIn and btnZoomOut).

Now that we have our buttons, let's put the appropriate code in the Click events of each button. Below is the code for the Zoom In button. Double-click on the button to add the code.

    private void btnZoomIn_Click(object sender, EventArgs e)
    {
        // Current Extent Zoom in 30%.
        mMapEngine.CurrentExtent = ExtentHelper.ZoomIn(mMapEngine.CurrentExtent, 50);
        Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
 
        // Draw the layer again with new current extent.
        mMapEngine.OpenAllLayers();
        mMapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree);
        mMapEngine.CloseAllLayers();
 
        pictureBox1.Image = bitmap;
    }

The above code simply scales down the current extent and redraws the map. This has the effect of making the map seem like it is zooming in. Now add the code for the Zoom Out button:

    private void btnZoomOut_Click(object sender, EventArgs e)
    {
        //<Current Extent Zoom out 30%.
        mMapEngine.CurrentExtent = ExtentHelper.ZoomOut(mMapEngine.CurrentExtent, 50);
        Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
 
        // Draw the layer again with new current extent.
        mMapEngine.OpenAllLayers();
        mMapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree);
        mMapEngine.CloseAllLayers();
 
        pictureBox1.Image = bitmap;
    }

Now that we know how to render text and render symbols, let's define two different ZoomLevels in one single layer, and create our own custom Style and TextStyle.

    private void Form1_Load(object sender, EventArgs e)
    {
        ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(@"../../AppData/Countries02.shp");
        worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
        worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
 
        ShapeFileFeatureLayer capitalLayer = new ShapeFileFeatureLayer(@"../../AppData/WorldCapitals.shp");
        // We can customize our own Style. Here we passed in a color and a size.
        capitalLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.CreateSimpleCircleStyle(GeoColor.StandardColors.White, 7, GeoColor.StandardColors.Brown);
        // The Style we set here is available from ZoomLevel01 to ZoomLevel05. That means if we zoom in a bit more, the appearance we set here will not be visible anymore.
        capitalLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level05;
 
        capitalLayer.ZoomLevelSet.ZoomLevel06.DefaultPointStyle = PointStyles.Capital3;
        // The Style we set here is available from ZoomLevel06 to ZoomLevel20. That means if we zoom out a bit more, the appearance we set here will not be visible any more.
        capitalLayer.ZoomLevelSet.ZoomLevel06.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
 
        ShapeFileFeatureLayer capitalLabelLayer = new ShapeFileFeatureLayer(@"../../AppData/WorldCapitals.shp");
        // We can customize our own TextStyle. Here we passed in the font, the size, the style and the color.
        capitalLabelLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.CreateSimpleTextStyle("CITY_NAME", "Arial", 8, DrawingFontStyles.Italic, GeoColor.StandardColors.Black, 3, 3);
        // The TextStyle we set here is available from ZoomLevel01 to ZoomLevel05. 
        capitalLabelLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level05;
 
        capitalLabelLayer.ZoomLevelSet.ZoomLevel06.DefaultTextStyle = TextStyles.Capital3("CITY_NAME");
        // The TextStyle we set here is available from ZoomLevel06 to ZoomLevel20. 
        capitalLabelLayer.ZoomLevelSet.ZoomLevel06.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
 
        mMapEngine.StaticLayers.Add(worldLayer);
        mMapEngine.StaticLayers.Add(capitalLayer);
        mMapEngine.StaticLayers.Add(capitalLabelLayer);
 
        mMapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-11, 70, 34, 30), pictureBox1.Width, pictureBox1.Height);
        mMapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);
 
        // Display the map.
        Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        mMapEngine.OpenAllLayers();
        mMapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree);
        mMapEngine.CloseAllLayers();
        pictureBox1.Image = bitmap;
    }

Can you imagine what the map will look like now? Below is the result. At first it looks the same as it did in Figure 6. Now click the ZoomIn button, and watch the map change to resemble Figure 7 as you zoom in.

qsg_servicesedition2_img09.jpg
Figure 6. European cities with two ZoomLevels, before zooming in.

qsg_servicesedition2_img10.jpg
Figure 7. European cities with two ZoomLevels, after zooming in.

Summary

You now know the basics of using Map Suite Services Edition and are able to start adding this functionality into your own applications. Let's recap what we have learned about the object relationships and how the pieces of Map Suite work together:

  1. It is of the utmost importance that the units of measurement (feet, meters, decimal degrees, etc.) be set properly for the MapEngine, based on the requirements of your data.
  2. Shapefiles (and other data sources like Oracle, Postgre, SQL 2008, etc.) provide the data used by Map Suite to render a map.
  3. A MapEngine is the basic class that contains all of the other objects that are used to define how the map will be rendered.
  4. A MapEngine has one-to-many Layers. A Layer contains the data (from shapefile or other data sources) for drawing.
  5. A layer can have one-to-many ZoomLevels. ZoomLevels help to define ranges of when a layer should be shown or hidden.

Download Sample Code From This Exercise (1.5 MB)

map_suite_services_edition_quick_start_guide.txt · Last modified: 2017/03/17 02:33 by tgwikiupdate