.NET tools for developing web and desktop mapping applications
This page contains examples to help you get started. Full API reference is available here.

Windows Forms shape-file viewer example:

  • Create a new Windows Forms Application project
  • Add references to MapAround.Core and MapAround.WinFormsControl assemblies
  • Add MapControl to the Toolbox (click "–°hoose items" and select MapAround.WinFormsControl assembly)
  • Put MapControl and FileOpenDialog on the form
  • Add menu item for select shape-file

Write code like this for a form:

using System;
using System.Drawing;
using System.Windows.Forms;

using MapAround.Mapping;
using MapAround.DataProviders;
using System.Collections.Generic;
using MapAround.CoordinateSystems.Transformations;
using MapAround.Geometry;

namespace ShapeFileViewerDemo
{
    public partial class fMain : Form
    {
        public fMain()
        {
            InitializeComponent();

            // create a new map instance
            // and assign it to the mapControl
            mapControl.Map = new Map();

            // create a new feature layer
            FeatureLayer l = new FeatureLayer();
            
            // set layer display properties
            l.Visible = true;
            l.PolygonStyle.BorderColor = Color.Gray;
            l.PolygonStyle.FillForeColor = Color.LightGray;
            l.PolylineStyle.Color = Color.Gray;

            // add layer to the map
            mapControl.Map.AddLayer(l);
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // set open file dialog properties
            openFileDialog.Filter = "Shape-files (*.shp)|*.shp";
            openFileDialog.Title = "Open Shape-file";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                // remove layer contents
                ((FeatureLayer)mapControl.Map.Layers[0]).RemoveAllFeatures();

                // create shape-file provider instance and assign selected file name
                ShapeFileSpatialDataProvider dataProvider = new ShapeFileSpatialDataProvider();
                dataProvider.FileName = openFileDialog.FileName;

                // query features from shape-file
                dataProvider.QueryFeatures((FeatureLayer)mapControl.Map.Layers[0]);
                // and set zoom level
                doFitVertical();
            }
        }

        private void mapControl_MouseDown(object sender, MouseEventArgs e)
        {
            // process double clicks only
            if (e.Clicks == 2)
            {
                // get feature at click position
                Feature f = mapControl.GetFeatureAtPosition(new Point(e.X, e.Y));
                if (f != null)
                {
                    // toggle feature selection state
                    f.Selected = !f.Selected;
                    // and refresh map image
                    mapControl.RedrawMap();
                }
            }
        }

        private void fitVertical_Click(object sender, EventArgs e)
        {
            doFitVertical();
        }

        private void doFitVertical()
        {
            // set map viewbox to the layer extent
            // with forceRedraw and forceSizeFitting options
            mapControl.SetViewBox(((FeatureLayer)mapControl.Map.Layers[0]).GetBoundingRectangle(), truetruefalse);
        }
    }
}

Set custom rendering style for each polygon:

Add this code to previous example:

        private void thematicToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // subscribe to the BeforePolygonRender event and redraw the map
            ((FeatureLayer)mapControl.Map.Layers[0]).BeforePolygonRender += beforePolygonRender;
            mapControl.RedrawMap();
        }

        private void beforePolygonRender(object sender, FeatureRenderEventArgs e)
        {
            // clone layer settings for polygon rendering style
            PolygonStyle style = (PolygonStyle)e.Feature.Layer.PolygonStyle.Clone();

            // change this settings
            style.UseHatch = true;
            int f = e.Feature.Geometry.CoordinateCount % 3;
            switch (f)
            {
                case 0: style.HatchStyle = System.Drawing.Drawing2D.HatchStyle.Horizontal;
                    break;
                case 1: style.HatchStyle = System.Drawing.Drawing2D.HatchStyle.Vertical;
                    break;
                case 2: style.HatchStyle = System.Drawing.Drawing2D.HatchStyle.Cross;
                    break;
            }

            // style changes may be based on the feature attribute values
            // for example:
            // style.FillForeColor = Color.FromName(e.Feature["ColorName"].ToString());

            // assign changed style to the feature
            e.Feature.PolygonStyle = style;
        }

Add on-the-fly coordinate transformation:

Add using directive for MapAround.CoordinateSystems.Transformations namespace and write this code:

        private void transformToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // create an affine transformation
            Affine transformation = Affine.Rotation(Math.PI / 6);

            // assign it to the map on-the-fly transform and redraw the map
            mapControl.Map.OnTheFlyTransform  = transformation;
            mapControl.RedrawMap();
        }

Spatial query:

Add System.Collections.Generic and MapAround.Geometry namespaces. Write code like this:

        private void selectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // change mouse drag mode for the mapControl
            // this helps us to define a selection area by the zooming rectangle
            mapControl.DragMode = MapAround.UI.WinForms.MapControl.DraggingMode.Zoom;
        }

        private void mapControl_SelectionRectangleDefined(object sender, MapAround.UI.WinForms.ViewBoxEventArgs e)
        {
            // create a list instance for selected features
            List<Feature> selectedFeatures = new List<Feature>();

            // translate view box to map data coordinates
            BoundingRectangle br = mapControl.Map.MapViewBoxFromPresentationViewBox(e.ViewBox);
            FeatureLayer l = (FeatureLayer)mapControl.Map.Layers[0];

            // and do primary filter
            l.SelectObjects(br, selectedFeatures);

            // turn off selection
            foreach (Feature f in l.Features)
            {
                f.Selected = false;
            }

            Polygon p = null;

            // if the map has on-the-fly transformation, convert initial view box 
            // to polygon and transform its coordinates to map data coordinates
            if (mapControl.Map.OnTheFlyTransform  != null)
            {
                p = e.ViewBox.ToPolygon();
                IMathTransform inverseTransform = mapControl.Map.OnTheFlyTransform .Inverse();
                p = GeometryTransformer.TransformPolygon(p, inverseTransform);
            }

            foreach (Feature f in selectedFeatures)
            {
                if (mapControl.Map.OnTheFlyTransform  != null)
                {
                    // do more expensive secondary filter
                    if (p.Intersects((ISpatialRelative)f.Geometry))
                    {
                        f.Selected = true;
                    }
                }
                else
                {
                    // if our map hasn't on-the-fly transformation, primary filter is fine,
                    // so, set selection state
                    f.Selected = true;
                }
            }

            mapControl.DragMode = MapAround.UI.WinForms.MapControl.DraggingMode.Pan;
            mapControl.RedrawMap();
        }