Skip to main content
A newer version of this page is available. .

How to: Create a Map Control via Code

  • 2 minutes to read

This example describes how to programmatically add a MapControl in a Windows Forms application and connect this control to the OpenStreetMap data provider.

  • Run MS Visual Studio.
  • Create a new Windows Forms Application project or open an existing one.
  • Open the Solution Explorer, right-click References and choose Add Reference… to add the Map Control Library.

    Add Reference

  • In the Extensions tab, select the DevExpress.XtraMap, DevExpress.Map.Core, DevExpress.Data and DevExpress.Utils assemblies and click OK.

    Add Assemblies

  • Write the following (or similar) code:
using System;
using System.Windows.Forms;
using DevExpress.XtraMap;

namespace CreateMap {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            // Create a map control.
            MapControl map = new MapControl();

            // Specify the map position on the form.           
            map.Dock = DockStyle.Fill;

            // Create a layer.
            ImageLayer layer = new ImageLayer();
            map.Layers.Add(layer);

            // Create a data provider.
            OpenStreetMapDataProvider provider = new OpenStreetMapDataProvider();
            layer.DataProvider = provider;

            // Specify the map zoom level and center point. 
            map.ZoomLevel = 2;
            map.CenterPoint = new GeoPoint(38, -100);

            // Add the map control to the window.
            this.Controls.Add(map);
        }
    }
}

The MapControl has now been created and connected to the OpenStreetMap provider.

Run the application to see the result.

CreateMapRuntimeResult