Example E3680
Visible to All Users

Map for WPF - How to Implement a Custom Map Data Provider

This example implements a custom map data provider class inherited from the MapDataProviderBase type. In the example, a custom MapTileSourceBase class is implemented to supply tile URLs from the OpenStreetMap source.

Map

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

MainWindow.xaml
XAML
<Window x:Class="CustomMapDataProviderApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/map" Title="MainWindow" Height="350" Width="525"> <Grid> <dxc:MapControl> <dxc:ImageLayer x:Name="imageLayer"/> </dxc:MapControl> </Grid> </Window>
MainWindow.xaml.cs(vb)
C#
using System; using System.Globalization; using System.Net; using System.Windows; using DevExpress.Xpf.Map; namespace CustomMapDataProviderApp { public partial class MainWindow : Window { public MainWindow () { InitializeComponent(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; CustomMapDataProvider provider = new CustomMapDataProvider(); imageLayer.DataProvider = provider; provider.WebRequest += Provider_WebRequest; } private void Provider_WebRequest(object sender, MapWebRequestEventArgs e) { e.UserAgent = "Test Map App"; } } public class CustomMapDataProvider : MapDataProviderBase { readonly SphericalMercatorProjection projection = new SphericalMercatorProjection(); public override ProjectionBase Projection { get { return projection; } } public CustomMapDataProvider () { SetTileSource(new CustomTileSource()); } protected override MapDependencyObject CreateObject () { return new CustomMapDataProvider(); } public override Size GetMapSizeInPixels (double zoomLevel) { return new Size(Math.Pow(2.0, zoomLevel) * OpenStreetMapTileSource.tileSize, Math.Pow(2.0, zoomLevel) * OpenStreetMapTileSource.tileSize); } } public class CustomTileSource : MapTileSourceBase { const string roadUrlTemplate = @"http://{subdomain}.tile.openstreetmap.org/{tileLevel}/{tileX}/{tileY}.png"; public const int maxZoomLevel = 20; public const int tileSize = 256; static int imageWidth = (int)Math.Pow(2.0, maxZoomLevel) * tileSize; static int imageHeight = (int)Math.Pow(2.0, maxZoomLevel) * tileSize; static string[] subdomains = new string[] { "a", "b", "c" }; public CustomTileSource () : base(imageWidth, imageHeight, tileSize, tileSize) { } public override Uri GetTileByZoomLevel (int zoomLevel, long tilePositionX, long tilePositionY) { string url = roadUrlTemplate; url = url.Replace("{tileX}", tilePositionX.ToString(CultureInfo.InvariantCulture)); url = url.Replace("{tileY}", tilePositionY.ToString(CultureInfo.InvariantCulture)); url = url.Replace("{tileLevel}", zoomLevel.ToString(CultureInfo.InvariantCulture)); url = url.Replace("{subdomain}", subdomains[GetSubdomainIndex(subdomains.Length)]); return new Uri(url); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.