How to: Create a Map Control at Runtime
- 2 minutes to read
This example describes how to add a MapControl to a Windows Forms application and connect the control to the BingMapDataProvider data provider at runtime.
- 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.
Select the following assemblies in the Extensions tab:
- DevExpress.XtraMap
- DevExpress.Map.Core
- DevExpress.Data
- DevExpress.Utils
Click OK.
Handle the Form.Load event as follows:
using System; using System.Windows.Forms; using DevExpress.XtraMap; namespace MapControlRuntimeSample { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void OnFormLoad(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. BingMapDataProvider provider = new BingMapDataProvider(); provider.BingKey = "Your Bing Maps key"; layer.DataProvider = provider; // Specify the map zoom level and center point. map.ZoomLevel = 3; map.CenterPoint = new GeoPoint(40, -100); // Add the map control to the window. this.Controls.Add(map); } } }
The example above uses the following entities:
Class Description MapControl Displays raster and vector maps. Supports both popular map services (Bing Maps and OpenStreetMap) and custom map data servers inside your corporate network. See Map Control. ImageLayer Displays map images obtained from map image data providers. BingMapDataProvider The class that loads map images from the Bing Maps data provider.
Run the application to see the result.