Windows Mobile引路蜂地图开发示例:第一个地图应用

jerry 地图开发 2015年11月26日 收藏

打开Visual studio 创建一个新项目WM6GISEngineTutorial。把Form1改名为MainForm,添加对引路蜂类库的引用,并把guidebee.lic做为Content添加到项目中。

我们将使用MainForm来显示地图,下面的例子显示南京地图,缩放级别为13级,地图类型为Bing中国地图。

  1. using System;
  2. using System.Windows.Forms;
  3. using Mapdigit.Gis;
  4. using Mapdigit.Gis.Drawing;
  5. using Mapdigit.Gis.Geometry;
  6. using Mapdigit.Gis.Raster;
  7. using Mapdigit.Gis.Service;
  8. using Mapdigit.Licence;
  9. using WM6GISEngineTutorial.Drawing;
  10. using System.Drawing;
  11.  
  12. namespace WM6GISEngineTutorial
  13. {
  14.     public partial class Mainform : Form, IMapDrawingListener, IReaderListener,
  15.          IRoutingListener, IGeocodingListener
  16.     {
  17.         /// <summary>
  18.         /// map tile downloader manager
  19.         /// </summary>
  20.         private readonly MapTileDownloadManager _mapTileDownloadManager;
  21.         /// <summary>
  22.         /// raster map.
  23.         /// </summary>
  24.         private readonly RasterMap _rasterMap;
  25.         /// <summary>
  26.         /// map image.
  27.         /// </summary>
  28.         private readonly IImage _mapImage;
  29.         /// <summary>
  30.         /// map graphics object.
  31.         /// </summary>
  32.         private IGraphics _mapGraphics;
  33.         /// <summary>
  34.         /// map type.
  35.         /// </summary>
  36.         private int _mapType = MapType.MicrosoftChina;
  37.         private int _oldX;
  38.         private int _oldY;
  39.         private bool _isPanMode;
  40.         private delegate void UpdateInfo(string message);
  41.         public Mainform()
  42.         {
  43.             InitializeComponent();
  44.             //set the licence info.
  45.             LicenceManager licenceManager = LicenceManager.GetInstance();
  46.             long[] keys = { -0x150dc6a05f379456L, -0x703237078e243c4fL,
  47.       -0x104afc92926c5418L, -0x4af1782b11010f5dL,
  48.       -0x27d1f7ce354a3419L, 0x17ded67edd3a5281L, };
  49.             licenceManager.AddLicence("GuidebeeMap_DotNet", keys);
  50.             //optional, get the tile url from server.
  51.             MapType.UpdateMapTileUrl();
  52.             MapLayer.SetAbstractGraphicsFactory(NETGraphicsFactory.GetInstance());
  53.             MapConfiguration.SetParameter(MapConfiguration.WorkerThreadNumber, 4);
  54.             _mapImage = MapLayer.GetAbstractGraphicsFactory()
  55.       .CreateImage(Width, Height);
  56.             _mapTileDownloadManager = new MapTileDownloadManager(this);
  57.             _rasterMap = new RasterMap(1024, 1024, _mapType, _mapTileDownloadManager);
  58.             _rasterMap.SetScreenSize(Width, Height);
  59.             _mapTileDownloadManager.Start();
  60.             _rasterMap.SetMapDrawingListener(this);
  61.             _rasterMap.SetRoutingListener(this);
  62.             _rasterMap.SetGeocodingListener(this);
  63.              GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);
  64.             _rasterMap.SetCenter(center, 13, _mapType);
  65.         }
  66.  
  67.         private void Mainform_Paint(object sender, PaintEventArgs e)
  68.         {
  69.             Graphics graphics = e.Graphics;
  70.             Bitmap image = (Bitmap)_mapImage.GetNativeImage();
  71.             graphics.DrawImage(image, 0, 0);
  72.         }
  73.  
  74.         private void PanMap(int x, int y)
  75.         {
  76.             int dx = x - _oldX;
  77.             int dy = y - _oldY;
  78.            _rasterMap.PanDirection(dx, dy);
  79.         }
  80.  
  81.         private void UpdateStatus(string messsage)
  82.         {
  83.             if (InvokeRequired)
  84.             {
  85.                 BeginInvoke(new UpdateInfo(UpdateStatus), messsage);
  86.  
  87.             }
  88.             else
  89.             {
  90.                 _mapGraphics = _mapImage.GetGraphics();
  91.                 _rasterMap.Paint(_mapGraphics);
  92.                 Invalidate();
  93.                 Console.WriteLine(messsage);
  94.             }
  95.         }
  96.  
  97.         public void Done()
  98.         {
  99.             UpdateStatus("");
  100.         }
  101.  
  102.         public void Done(string query, MapDirection result)
  103.         {
  104.             if (result != null)
  105.             {
  106.                 _rasterMap.SetMapDirection(result);
  107.                 _rasterMap.Resize(result.Bound);
  108.             }
  109.         }
  110.  
  111.         public void ReadProgress(int bytes, int total)
  112.         {
  113.             if (total != 0)
  114.             {
  115.                 UpdateStatus("Reading ...");
  116.             }
  117.         }
  118.  
  119.         public void Done(string query, MapPoint[] result)
  120.         {
  121.             if (result != null)
  122.             {
  123.                 _rasterMap.PanTo(result[0].Point);
  124.  
  125.             }
  126.         }
  127.         void IGeocodingListener.ReadProgress(int bytes, int total)
  128.         {
  129.             if (total != 0)
  130.             {
  131.                 UpdateStatus("Reading ...");
  132.             }
  133.         }
  134.  
  135.         void IReaderListener.ReadProgress(int bytes, int total)
  136.         {
  137.             if (total != 0)
  138.             {
  139.                 UpdateStatus("Reading ...");
  140.             }
  141.         }
  142.  
  143.         private void Mainform_MouseDown(object sender, MouseEventArgs e)
  144.         {
  145.             _oldX = e.X;
  146.             _oldY = e.Y;
  147.             _isPanMode = true;
  148.         }
  149.  
  150.         private void Mainform_MouseMove(object sender, MouseEventArgs e)
  151.         {
  152.             if (_isPanMode)
  153.             {
  154.                 PanMap(e.X, e.Y);
  155.                 _oldX = e.X;
  156.                 _oldY = e.Y;
  157.             }
  158.         }
  159.  
  160.         private void Mainform_MouseUp(object sender, MouseEventArgs e)
  161.         {
  162.             _oldX = e.X;
  163.             _oldY = e.Y;
  164.  
  165.             _isPanMode = false;
  166.         }
  167.  
  168.     }
  169. }

行45-49    设置Licence信息。
行51       从服务器更新地图类型配置,可选。
行52       将Windows Mobile平台上Gis.Drawing实现与开发包连接起来。
行53       设置工作线程数。
行54-59    创建地图实例,并启动工作线程。一般在程序结束时需调用_mapTileDownloadManager.Stop()停止工作线程。
行60-62    设置回调函数,其实第一个例子60,61行不是必须的。
行63-64    设置地图中心,地图类型和缩放级别。
行67-72    显示地图,当一个地图图片完成下载时,会通过回调函数 Done() 通知应用,一般在此更新地图。
行143-166  支持地图平移。

后面的例子设置地图类型,地址编码,路径查询等将在这个例子基础上改动,通过菜单来解释各项功能。