Gmap离线地图下载功能

行政区域下载地址

http://datav.aliyun.com/tools/atlas/#&lat=30.332329214580188&lng=106.72278672066881&zoom=3.5

 

2022/02/17添加

根据经纬度坐标获得省市区县行政区划城市名称,自建数据库 java python php c# .net 均适用 - xiangyuecn - 博客园 (cnblogs.com)

 

省市区边界数据在线预  下载:https://xiangyuecn.gitee.io/areacity-jsspider-statsgov/
GitHub地址:
https://github.com/xiangyuecn/AreaCity-JsSpider-StatsGov

 

自定义方法

using GMap.NET;
using GMap.NET.MapProviders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demo.WindowsPresentation.Windows
{
   class MapDownloadHelper
   {
      public static List<GPoint> GetPointToDownload(List<PointLatLng> pointsAll, GMapProvider provider, int zoom)
      {
         List<GPoint> listReturn = new List<GPoint>();
         List<GPoint> listGPoints = pointsAll.Select(t => provider.Projection.FromPixelToTileXY(provider.Projection.FromLatLngToPixel(t, zoom))).ToList();

         long minX = listGPoints.Min(t => t.X);
         long maxX = listGPoints.Max(t => t.X);

         long minY = listGPoints.Min(t => t.Y);
         long maxY = listGPoints.Max(t => t.Y);

         List<long> listY = listGPoints.Select(t => t.Y).ToList();
         List<long> listX = listGPoints.Select(t => t.X).ToList();

         for (long x = minX; x <= maxX; x++)
         {
            for (long y = minY; y <= maxY; y++)
            {
               if (PositionPnpoly(listY.Count, listY, listX, y, x))
               {
                  listReturn.Add(new GPoint(x, y));
               }
            }
         }
         return listReturn;
      }


      /// <summary>
      /// 判断当前位置是否在不规则形状里面
      /// </summary>
      /// <param name="nvert">不规则形状的定点数</param>
      /// <param name="vertx">当前x坐标</param>
      /// <param name="verty">当前y坐标</param>
      /// <param name="testx">不规则形状x坐标集合</param>
      /// <param name="testy">不规则形状y坐标集合</param>
      /// <returns></returns>
      private static bool PositionPnpoly(int nvert, List<long> vertx, List<long> verty, double testx, double testy)
      {
         int i, j, c = 0;
         for (i = 0, j = nvert - 1; i < nvert; j = i++)
         {
            if (((verty[i] > testy) != (verty[j] > testy)) && (testx < (vertx[j] - vertx[i]) * (testy - verty[i]) / (verty[j] - verty[i]) + vertx[i]))
            {
               c = 1 + c; ;
            }
         }
         if (c % 2 == 0)
         {
            return false;
         }
         else
         {
            return true;
         }
      }
   }
}

 

 

使用自定义方法下载

RectLatLng area = MainMap.SelectedArea;
        
            for (int i = (int)MainMap.Zoom; i <= MainMap.MaxZoom; i++)
            {
               MessageBoxResult res = MessageBox.Show("是否下载第 " + i + " 层地图?", "消息提示", MessageBoxButton.YesNoCancel);

               if (res == MessageBoxResult.Yes)
               {
                  TilePrefetcher obj = new TilePrefetcher();
                  obj.Owner = this;
                  obj.ShowCompleteMessage = true;
                  var listdownload = MapDownloadHelper.GetPointToDownload(pointLatLngs, MainMap.MapProvider, i);
                  obj.Start(area, i, MainMap.MapProvider, 100, true, listdownload);
               }
               else if (res == MessageBoxResult.No)
               {
                  continue;
               }
               else if (res == MessageBoxResult.Cancel)
               {
                  break;
               }
            }

 

测试离线地图

new Thread(() => GMaps.Instance.ImportFromGMDB(IMSSettings.Instance.GMapDataPath)).Start();
            mapControl.Manager.Mode = AccessMode.CacheOnly;
            mapControl.MapProvider = mapControl.MapProvider = m_mapOberationCollection.Where(x => x.Name == IMSConfigHelper.Instance.IMSConfiguration.GISParameters.GISMapName).ToList().FirstOrDefault().Value;
            mapControl.MinZoom = IMSConfigHelper.Instance.IMSConfiguration.GISParameters.GISMinZoom;  //最小缩放
            mapControl.MaxZoom = IMSConfigHelper.Instance.IMSConfiguration.GISParameters.GISMaxZoom; //最大缩放
            mapControl.Zoom = IMSConfigHelper.Instance.IMSConfiguration.GISParameters.GISZoom;     //当前缩放
            mapControl.ShowCenter = true; //显示中心十字点
            mapControl.DragButton = MouseButton.Right;
            mapControl.CacheLocation = IMSSettings.Instance.GMapDataFolder;

 

posted @ 2020-11-20 16:58  当年小清新  阅读(1435)  评论(0编辑  收藏  举报