====== Map Suite Services Edition Quick Start Guide ====== {{section>upgrade_map_suite_to_10.0}}
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).
{{servicesedition: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:
- The borders of every country in the world ("Countries02.shp")
- 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):
{{servicesedition: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.
{{servicesedition: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):
{{servicesedition: 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)
{
//
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.
{{servicesedition: QSG_ServicesEdition2_Img09.jpg}}
\\
//Figure 6. European cities with two ZoomLevels, before zooming in.//
{{servicesedition: 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:
- 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.
- Shapefiles (and other data sources like Oracle, Postgre, SQL 2008, etc.) provide the data used by Map Suite to render a map.
- A MapEngine is the basic class that contains all of the other objects that are used to define how the map will be rendered.
- A MapEngine has one-to-many Layers. A Layer contains the data (from shapefile or other data sources) for drawing.
- A layer can have one-to-many ZoomLevels. ZoomLevels help to define ranges of when a layer should be shown or hidden.
{{filehistory:HelloWorld Services.zip|Download Sample Code From This Exercise}} //(1.5 MB)//