Skip to main content

How to: Add a Map Control at Runtime

  • 2 minutes to read

This example demonstrates how to add a MapControl to a WPF application and connect the map to the BingMapDataProvider at runtime.

  1. Run Microsoft Visual Studio.

  2. Create a new WPF Application project or open an existing one.

  3. Open the Solution Explorer, right-click References and choose Add Reference….

    Add references to the following assemblies in the Extensions tab:

    • DevExpress.Xpf.Map.v23.2
    • DevExpress.Map.v23.2.Core
    • DevExpress.Data.v23.2

    how-to-add-map-control-xaml-2

    Click OK.

  4. Handle the Window’s Loaded event as follows:

    using System.Windows;
    using DevExpress.Xpf.Map;
    
    
    namespace DXMap_Runtime {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window {
            public MainWindow() {
                InitializeComponent();
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e) {
                // Create a map control and add it to the window.
                MapControl map = new MapControl();
                this.Content = map;
    
                // Create a layer.
                ImageLayer layer = new ImageLayer() {
                    DataProvider = new BingMapDataProvider() {
                        BingKey = "Insert your Bing Maps key here.",
                        Kind = BingMapKind.Road
                    }
                };
                map.Layers.Add(layer);
                map.ZoomLevel = 4;
                map.CenterPoint = new GeoPoint(40, -100);
            }
        }
    }
    

    The example above uses the following entities:

    Class Description
    MapControl A Map Control to display raster and vector maps. Supports both popular map services (Bing Maps and OpenStreetMap) and custom map data servers inside your corporate network.
    ImageLayer Displays map images obtained from the map image data provider.
    BingMapDataProvider The class that loads map images from the Bing Maps data provider.
  5. Run the project to see the result.

See Also