How to: Load Image Tiles from OpenStreetMap

  • 2 minutes to read

This example demonstrates how to connect to the OpenStreetMap using the OpenStreetMapDataProvider object.

using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraMap;

namespace ConnectToOpenStreet {
    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;

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

            // Create an image tiles layer and add it to the map.
            ImageLayer tileLayer = new ImageLayer();
            map.Layers.Add(tileLayer);            

            // Create an Open Street data provider.
            OpenStreetMapDataProvider provider = new OpenStreetMapDataProvider();
            tileLayer.DataProvider = provider;

            // Specify a template that is used to obtain image tiles. 
            provider.TileUriTemplate = "http://{subdomain}.tile.MyCustomOSMProvider.org/{tileLevel}/{tileX}/{tileY}.png";

            provider.WebRequest += OnWebRequest;
        }

        private void OnWebRequest(object sender, MapWebRequestEventArgs e) {
            e.UserAgent = "XtraMap Getting Started - Connect to OpenStreetMap";
        }
    }
}