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

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

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

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

using System;
using System.Windows.Forms;
using Mapdigit.Gis;
using Mapdigit.Gis.Drawing;
using Mapdigit.Gis.Geometry;
using Mapdigit.Gis.Raster;
using Mapdigit.Gis.Service;
using Mapdigit.Licence;
using WM6GISEngineTutorial.Drawing;
using System.Drawing;

namespace WM6GISEngineTutorial
{
    public partial class Mainform : Form, IMapDrawingListener, IReaderListener,
         IRoutingListener, IGeocodingListener
    {
        /// <summary>
        /// map tile downloader manager
        /// </summary>
        private readonly MapTileDownloadManager _mapTileDownloadManager;
        /// <summary>
        /// raster map.
        /// </summary>
        private readonly RasterMap _rasterMap;
        /// <summary>
        /// map image.
        /// </summary>
        private readonly IImage _mapImage;
        /// <summary>
        /// map graphics object.
        /// </summary>
        private IGraphics _mapGraphics;
        /// <summary>
        /// map type.
        /// </summary>
        private int _mapType = MapType.MicrosoftChina;
        private int _oldX;
        private int _oldY;
        private bool _isPanMode;
        private delegate void UpdateInfo(string message);
        public Mainform()
        {
            InitializeComponent();
            //set the licence info.
            LicenceManager licenceManager = LicenceManager.GetInstance();
            long[] keys = { -0x150dc6a05f379456L, -0x703237078e243c4fL,
      -0x104afc92926c5418L, -0x4af1782b11010f5dL,
      -0x27d1f7ce354a3419L, 0x17ded67edd3a5281L, };
            licenceManager.AddLicence("GuidebeeMap_DotNet", keys);
            //optional, get the tile url from server.
            MapType.UpdateMapTileUrl();
            MapLayer.SetAbstractGraphicsFactory(NETGraphicsFactory.GetInstance());
            MapConfiguration.SetParameter(MapConfiguration.WorkerThreadNumber, 4);
            _mapImage = MapLayer.GetAbstractGraphicsFactory()
      .CreateImage(Width, Height);
            _mapTileDownloadManager = new MapTileDownloadManager(this);
            _rasterMap = new RasterMap(1024, 1024, _mapType, _mapTileDownloadManager);
            _rasterMap.SetScreenSize(Width, Height);
            _mapTileDownloadManager.Start();
            _rasterMap.SetMapDrawingListener(this);
            _rasterMap.SetRoutingListener(this);
            _rasterMap.SetGeocodingListener(this);
             GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);
            _rasterMap.SetCenter(center, 13, _mapType);
        }

        private void Mainform_Paint(object sender, PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;
            Bitmap image = (Bitmap)_mapImage.GetNativeImage();
            graphics.DrawImage(image, 0, 0);
        }

        private void PanMap(int x, int y)
        {
            int dx = x - _oldX;
            int dy = y - _oldY;
           _rasterMap.PanDirection(dx, dy);
        }

        private void UpdateStatus(string messsage)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new UpdateInfo(UpdateStatus), messsage);

            }
            else
            {
                _mapGraphics = _mapImage.GetGraphics();
                _rasterMap.Paint(_mapGraphics);
                Invalidate();
                Console.WriteLine(messsage);
            }
        }

        public void Done()
        {
            UpdateStatus("");
        }

        public void Done(string query, MapDirection result)
        {
            if (result != null)
            {
                _rasterMap.SetMapDirection(result);
                _rasterMap.Resize(result.Bound);
            }
        }

        public void ReadProgress(int bytes, int total)
        {
            if (total != 0)
            {
                UpdateStatus("Reading ...");
            }
        }

        public void Done(string query, MapPoint[] result)
        {
            if (result != null)
            {
                _rasterMap.PanTo(result[0].Point);

            }
        }
        void IGeocodingListener.ReadProgress(int bytes, int total)
        {
            if (total != 0)
            {
                UpdateStatus("Reading ...");
            }
        }

        void IReaderListener.ReadProgress(int bytes, int total)
        {
            if (total != 0)
            {
                UpdateStatus("Reading ...");
            }
        }

        private void Mainform_MouseDown(object sender, MouseEventArgs e)
        {
            _oldX = e.X;
            _oldY = e.Y;
            _isPanMode = true;
        }

        private void Mainform_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isPanMode)
            {
                PanMap(e.X, e.Y);
                _oldX = e.X;
                _oldY = e.Y;
            }
        }

        private void Mainform_MouseUp(object sender, MouseEventArgs e)
        {
            _oldX = e.X;
            _oldY = e.Y;

            _isPanMode = false;
        }

    }
}

行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  支持地图平移。

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