Windows Mobile引路蜂地图开发示例:第一个地图应用
打开visual studio 创建一个新项目wm6gisenginetutorial。把form1改名为mainform,添加对引路蜂类库的引用,并把guidebee.lic做为content添加到项目中。
我们将使用mainform来显示地图,下面的例子显示南京地图,缩放级别为13级,地图类型为bing中国地图。
[csharp]
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;
}
}
}
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 支持地图平移。
后面的例子设置地图类型,地址编码,路径查询等将在这个例子基础上改动,通过菜单来解释各项功能。
作者:mapdigit
推荐阅读