C# Devexpress Mapcontrol地图离线加载本地切片地图数据

C# Devexpress Mapcontrol 地图离线加载本地切片地图数据

C #Devexpress的Mapcontrol是一个强大的地图控件,可以连接Bing等网络地图,也可以连接搭建的WMS服务器地图,并且可以在地图上进行各种图形的绘制,但是我们会遇到某些客户环境是没有网络的,但是需要有离线的地图展示,因此可以参照本文方法进行离线地图加载。

未经作者许可,禁止转载!

1、首先我们需要下载地图资源

网上有很多的下载器可以下载,但是精度,放大级别比较高的地图大多需要收费,大家可以自己找下,一般来说下载好的切片地图,文件夹结果应该是如下的(截图为谷歌地图):在这里插入图片描述
切片结构如下,地图会因为下载的范围和级别不同,生成不同的文件夹和名称
在这里插入图片描述

2、在WinForm界面加入Mapcontrol

在这里插入图片描述
窗体代码 C#

public Form1() {
            InitializeComponent();
        }
        ImageLayer imageTilesLayer = new ImageLayer();
        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.
            map.Layers.Add(imageTilesLayer);//添加图层
            imageTilesLayer.DataProvider = new LocalProvider();//图层数据源设置为本地资源

        }
        /// <summary>
        /// 本地资源类
        /// </summary>
        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 = 18;
            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 + "\\"+Mapstyle+ "\\{0}\\{1}\\{2}."+ MapsFormat, zoomLevel, tilePositionX, tilePositionY));
                    return u;
                }
                return null;
            }
        } 

运行效果

在这里插入图片描述
不同所缩放级别,控件会自动读取对应的层级的切片文件夹,如图:
在这里插入图片描述

结束

  • 7
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不知名君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值