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

OpenStreetMapDataProvider.GetMapSizeInPixels(Double) Method

Returns the map size (in pixels) for the specified zoom level.

Namespace: DevExpress.XtraMap

Assembly: DevExpress.XtraMap.v18.2.dll

Declaration

public override MapSize GetMapSizeInPixels(
    double zoomLevel
)

Parameters

Name Type Description
zoomLevel Double

A double value that specifies the zoom level.

Returns

Type Description
MapSize

A MapSize object that is the map size.

Example

This example illustrates how to implement a custom provider for image tiles. To do this, it is necessary to create a MapDataProviderBase class descendant and assign it to the ImageLayer.DataProvider property.

The sample below shows how to load image tiles from a local cache. This is possible only if you previously downloaded a group of image tiles from one of the available image tile providers. For example, a list of alternative OpenStreetMap image tile providers is available here: OpenStreetMap: Tile usage policy.

Note

If you don’t need to create a custom data provider and only require changing the Uri to the location of image tiles, use the OpenStreetMapDataProvider.TileUriTemplate property.


using System;
using System.Windows.Forms;
using DevExpress.XtraMap;
using System.IO;
using System.Drawing;
using System.Diagnostics;

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

        private void Form1_Load(object sender, EventArgs e) {
            // Create a map control, set its dock style and add it to the form.
            MapControl map = new MapControl();
            map.Dock = DockStyle.Fill;
            this.Controls.Add(map);

            // Create a layer to load image tiles from a local map data provider.
            ImageLayer imageTilesLayer = new ImageLayer();
            map.Layers.Add(imageTilesLayer);
            imageTilesLayer.DataProvider = new LocalProvider();

        }

        public class LocalProvider : MapDataProviderBase {

            readonly SphericalMercatorProjection projection = new SphericalMercatorProjection();

            public LocalProvider() {
                TileSource = new LocalTileSource(this);
            }

            public override ProjectionBase Projection {
                get {
                    return projection;
                }
            }

            public override MapSize GetMapSizeInPixels(double zoomLevel) {
                double imageSize;
                imageSize = LocalTileSource.CalculateTotalImageSize(zoomLevel);
                return new MapSize(imageSize, imageSize);
            }
            protected override Size BaseSizeInPixels {
                get { return new Size(Convert.ToInt32(LocalTileSource.tileSize * 2), Convert.ToInt32(LocalTileSource.tileSize * 2)); }
            }
        }

        public class LocalTileSource : MapTileSourceBase {
            public const int tileSize = 256;
            public const int maxZoomLevel = 2;
            string directoryPath;

            internal static double CalculateTotalImageSize(double zoomLevel) {
                if (zoomLevel < 1.0)
                    return zoomLevel * tileSize * 2;
                return Math.Pow(2.0, zoomLevel) * tileSize;
            }

            public LocalTileSource(ICacheOptionsProvider cacheOptionsProvider) :
                base((int)CalculateTotalImageSize(maxZoomLevel), (int)CalculateTotalImageSize(maxZoomLevel), tileSize, tileSize, cacheOptionsProvider) {
                DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
                directoryPath = dir.Parent.Parent.FullName;
            }

            public override Uri GetTileByZoomLevel(int zoomLevel, int tilePositionX, int tilePositionY) {
                if (zoomLevel <= maxZoomLevel) {
                    Uri u = new Uri(string.Format("file://" + directoryPath + "\\openstreetmap.org\\Hybrid_{0}_{1}_{2}.png", zoomLevel, tilePositionX, tilePositionY));
                    return u;
                }
                return null;
            }
        }
    }
}
See Also