GMAP.NET
enjoyeclipse
深入理解最强桌面地图控件GMAP.NET --- 初用
上一篇介绍了GMAP.NET的基本概念和一些Demo的截图,这一章主要介绍我们的代码如何使用GMAP.NET。
1.下载
http://greatmaps.codeplex.com/releases/view/20235
2.编译GMAP.NET工程
3.在项目中引用
我的项目是用的WPF,因此需要引用GMAP.NET Core和GMap.NET.WindowsPresentation两个dll。
4. GMapControl
1)UserControl.xaml 创建一个UserControl,并在UserControl中引用 GMapControl ,我设置了MaxZoom和MinZoom, 也是GMAP.NET支持的最大缩放比例尺,如下所示:
< UserControl x:Class ="UICommon.View.Map.GMapTrack" xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:gmap ="clr-namespace:GMap.NET.WindowsPresentation;assembly=GMap.NET.WindowsPresentation" xmlns:Map ="clr-namespace:UICommon.View.Map" x:Name ="userControl" > < Grid > < GroupBox Name ="mapgroup" Margin ="0,0,50,0" VerticalContentAlignment ="Stretch" HorizontalContentAlignment ="Stretch" > < gmap:GMapControl x:Name ="MainMap" MaxZoom ="24" MinZoom ="1" > </ gmap:GMapControl > </ GroupBox > </ Grid > </ UserControl >
5. GMapControl事件
设置事件代码,包括鼠标进入事件MouseEnter, 鼠标右键事件MouseRightButtonDown,鼠标双击事件MouseDoubleClick。
OnTileLoadStart和OnTileLoadComplete这两个事件是这样的:所有地图下载的时候都是讲一张一张图片下载下来拼接而成的, 而这里的图片就叫做Tile 。所以
OnTileLoadStart就是指每次每张图片开始载入时触发的事件,OnTileLoadComplete就是每次每张图片载入完成后触发的事件。那么就可以在OnTileLoadStart的时候显示
正在加载或进度条之类的,不会让用户感觉死在那儿;而OnTileLoadComplete可以关闭进度条。
public GMapTrack() { InitializeComponent(); this .MainMap.MouseEnter += MainMap_MouseEnter;
this .MainMap.MouseRightButtonDown += new MouseButtonEventHandler(MainMap_MouseRightButtonDown); this .MainMap.MouseDoubleClick += new MouseButtonEventHandler(MainMap_MouseDoubleClick);
this .MainMap.OnTileLoadStart += MainMap_OnTileLoadStart;
this .MainMap.OnTileLoadComplete += MainMap_OnTileLoadComplete;
this .MainMap.Loaded += new RoutedEventHandler(MainMap_Loaded);
}
6. GMapControl Loaded初始化
Position 是地图默认启动的中心位置,我这里是从配置文件里面读取的。
Area 是指整个地图的区域,可以不填
Mode 有三种, CacheOnly(只从缓存中取),ServerAndCache(网络+缓存), ServerOnly(只从网络读)
MapProvider 是地图的来源,默认是OpenStreetMap,当然也可以使BingMap, GoogleMap,关于百度,SoSo等国内地图的支持会在后面的章节介绍。
DragButton 是指拖动地图时是用鼠标左键还是右键
Zoom 是当前地图显示的层级 (1~24)
MinZoom 是地图支持最小的层级, MaxZoom 是地图支持的最大层级。
this .MainMap.Position = new PointLatLng( double .Parse(ConfigurationManager.AppSettings[ " defaultLat " ]), double .Parse(ConfigurationManager.AppSettings[ " defaultLng " ])); this .MainMap.MapProvider.Area = new RectLatLng( 30.981178 , 105.351914 , 2.765142 , 4.120995 ); this .MainMap.BoundsOfMap = new RectLatLng( 30.981178 , 105.351914 , 2.765142 , 4.120995 ); this .MainMap.Manager.Mode = AccessMode.CacheOnly; this .MainMap.MapProvider = GMapProviders.OpenStreetMap; this .MainMap.DragButton = MouseButton.Left; this .MainMap.Zoom = 13 ; this .MainMap.MinZoom = 8 ; this .MainMap.MaxZoom = 24 ;
标签: GMAP.NET 地图
作者: Leo_wl
出处: http://www.cnblogs.com/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息