上一篇介紹了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;
掛個外鏈地址,練習下seo呢。