1. 程式人生 > >GMap.Net解決方案之在WinForm和WPF中使用GMap.Net地圖插件的開發

GMap.Net解決方案之在WinForm和WPF中使用GMap.Net地圖插件的開發

aac dem play 設計實現 成本 這就是 sage 就是 pes

在做地理位置相關的開發時,總是面臨高額地圖引擎費用讓大部分用戶望而卻步,加之地圖數據又是天價,那麽GMap.NET就是首選了,它本身就是開源免費,服務器可以在本地緩存,以後訪問時就可以直接訪問。 可以廣泛用於保密單位的空間數據服務應用,如軍隊、武警、公安等保密單位。下面我們就開始我們的GMap.NET的之旅吧!

什麽是GMap.NET?

來看看它的官方說明:GMap.NET is great and Powerful, Free, cross platform, open source .NET control. Enable use routing, geocoding, directions and maps from Coogle, Yahoo!, Bing, OpenStreetMap, ArcGIS, Pergo, SigPac, Yendux, Mapy.cz, Maps.lt, iKarte.lv, NearMap, OviMap, CloudMade, WikiMapia, MapQuest in Windows Forms & Presentation, supports caching and runs on windows mobile!

自己翻譯一下,GMap.NET是一個強大、免費、跨平臺、開源的.NET控件,它在Windows Forms 和WPF環境中能夠使用來自Google, Yahoo!, Bing, OpenStreetMap, ArcGIS, Pergo, SigPac等地圖,並可以實現尋找路徑、地理編碼以及地圖展示功能,並支持緩存和運行在Mobile環境中。

項目主頁:https://greatmaps.codeplex.com/

如何在WinForm中使用GMap.Net,Hello World例子:

下載GMap.Net,我下載的版本:greatmaps_81b71bf30091,編譯三個核心項目:

GMap.Net.Core:核心DLL

GMap.Net.WindowsForms:WinForm中使用的DLL

GMap.NET.WindowsPresentation:WPF中使用的DLL

在WinForm項目中使用GMap:

1、新建一個Visual C# 的Windows窗口程序。添加對GMap.Net.Core.DLL和GMap.Net.WindowsForms.DLL的引用。

2、在項目中添加一個UserControl,這裏取名為MapControl,修改這個UserControl,使其繼承於GMapControl,這就是展示地圖的控件。修改如下:

技術分享圖片
 1 using System;
 2 using System.Collections.Generic;
3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Data; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using GMap.NET.WindowsForms; 10 11 namespace GMapWinFormDemo 12 { 13 public partial class MapControl : GMapControl 14 { 15 public MapControl() 16 { 17 InitializeComponent(); 18 } 19 } 20 }
View Code

3、編譯項目,在我們的Form設計窗口下,在工具箱中(tool box)裏就可以看到這個MapControl,將這個MapControl加到Form中。

4、在主Form中添加相關的代碼如下

技術分享圖片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 using GMap.NET;
10 using GMap.NET.WindowsForms;
11 using GMap.NET.MapProviders;
12 using GMap.NET.WindowsForms.Markers;
13 
14 namespace GMapDemo
15 {
16     public partial class MapForm : Form
17     {
18         private GMapOverlay markersOverlay = new GMapOverlay("markers"); //放置marker的圖層
19 
20         public MapForm()
21         {
22             InitializeComponent();
23 
24             try
25             {
26                 System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry("ditu.google.cn");
27             }
28             catch
29             {
30                 mapControl.Manager.Mode = AccessMode.CacheOnly;
31                 MessageBox.Show("No internet connection avaible, going to CacheOnly mode.", "GMap.NET Demo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
32             }
33 
34             mapControl.CacheLocation = Environment.CurrentDirectory + "\\GMapCache\\"; //緩存位置
35             mapControl.MapProvider = GMapProviders.GoogleChinaMap; //google china 地圖
36             mapControl.MinZoom = 2;  //最小比例
37             mapControl.MaxZoom = 24; //最大比例
38             mapControl.Zoom = 10;     //當前比例
39             mapControl.ShowCenter = false; //不顯示中心十字點
40             mapControl.DragButton = System.Windows.Forms.MouseButtons.Left; //左鍵拖拽地圖
41             mapControl.Position = new PointLatLng(32.064,118.704); //地圖中心位置:南京
42 
43             mapControl.Overlays.Add(markersOverlay);
44             
45             mapControl.MouseClick += new MouseEventHandler(mapControl_MouseClick);
46         }
47         
48         void mapControl_MouseClick(object sender, MouseEventArgs e)
49         {
50             if (e.Button == System.Windows.Forms.MouseButtons.Right)
51             {
52                 PointLatLng point = mapControl.FromLocalToLatLng(e.X, e.Y);
53                 GMapMarker marker = new GMarkerGoogle(point, GMarkerGoogleType.green);
54                 markersOverlay.Markers.Add(marker);
55             }
56         }
57     }
View Code

技術分享圖片

5、編譯、運行項目就可以看到地圖,這裏使用的是在線的Google中國的地圖,地圖控件的幾個主要屬性:

MapProvider:地圖服務的提供者。

MinZoom:最小縮放,最小可為1。

MaxZoom:最大縮放,最大為24.

Zoom:當前縮放。

ShowCenter:是否顯示中心點(最好為false,否則地圖中間會有一個紅色的十字)。

DragButton:哪個鍵拖動地圖。

Position:地圖中心點位置。

地圖顯示如下,支持左鍵拖動,放大縮小,可以顯示左鍵的點擊經緯度。

如何在WPF中使用GMap.Net

1、新建一個Visual C# 的WPF程序。添加對GMap.Net.Core.DLL和GMap.NET.WindowsPresentation.DLL的引用。

2、由於WPF的UserControl不能修改繼承的基類,所以添加一個新的類,為MapControl.cs,代碼如下:

技術分享圖片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using GMap.NET.WindowsPresentation;
 6 
 7 namespace GMapWPFDemo
 8 {
 9     class MapControl : GMapControl
10     {
11     }
12 }
View Code

技術分享圖片

只需要繼承GMapControl就行了,基本功能都可以由GMapControl提供。

3、在我們的MainWindow.xaml中,添加項目的namespace:xmlns:src="clr-namespace:GMapWPFDemo",在XML代碼中添加對MapControl.cs的使用:

技術分享圖片
 1 <Window x:Class="GMapWPFDemo.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:src="clr-namespace:GMapWPFDemo"
 5         Title="MainWindow" Height="410" Width="618">
 6     <Grid>
 7             <GroupBox Name="mapgroup" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch">
 8             <src:MapControl x:Name="mapControl" Zoom="13" MaxZoom="24" MinZoom="1" />
 9             </GroupBox>
10     </Grid>
11 </Window>
View Code

技術分享圖片

4、在MainWindow中添加相關的代碼如下:

技術分享圖片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Windows;
 6 using System.Windows.Controls;
 7 using System.Windows.Data;
 8 using System.Windows.Documents;
 9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Navigation;
13 using System.Windows.Shapes;
14 using GMap.NET;
15 using GMap.NET.MapProviders;
16 using GMap.NET.WindowsPresentation;
17 
18 namespace GMapWPFDemo
19 {
20     /// <summary>
21     /// MainWindow.xaml 的交互邏輯
22     /// </summary>
23     public partial class MainWindow : Window
24     {
25         public MainWindow()
26         {
27             InitializeComponent();
28 
29             try
30             {
31                 System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry("ditu.google.cn");
32             }
33             catch
34             {
35                 mapControl.Manager.Mode = AccessMode.CacheOnly;
36                 MessageBox.Show("No internet connection avaible, going to CacheOnly mode.", "GMap.NET Demo", MessageBoxButton.OK, MessageBoxImage.Warning);
37             }
38 
39             mapControl.MapProvider = GMapProviders.GoogleChinaMap; //google china 地圖
40             mapControl.MinZoom = 2;  //最小縮放
41             mapControl.MaxZoom = 17; //最大縮放
42             mapControl.Zoom = 5;     //當前縮放
43             mapControl.ShowCenter = false; //不顯示中心十字點
44             mapControl.DragButton = MouseButton.Left; //左鍵拖拽地圖
45             mapControl.Position = new PointLatLng(32.064, 118.704); //地圖中心位置:南京
46 
47             mapControl.MouseLeftButtonDown += new MouseButtonEventHandler(mapControl_MouseLeftButtonDown);
48         }
49 
50         void mapControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
51         {
52             Point clickPoint = e.GetPosition(mapControl);
53             PointLatLng point = mapControl.FromLocalToLatLng((int)clickPoint.X, (int)clickPoint.Y);
54             GMapMarker marker = new GMapMarker(point);
55             mapControl.Markers.Add(marker);
56         }
57     }
58 }
View Code

技術分享圖片

效果圖如下

技術分享圖片技術分享圖片?

和Winform代碼差不多,一些響應事件不同,WPF的GMap中沒有GMapOverlay這個“圖層”的概念,所以沒法加多個GMapOverlay,在GMapOverlay上再加GMapMarker(可以理解為圖標標註),GMapMarker只能直接加在mapControl上面。

WPF的GMapMarker可以直接實例化(WinForm中的不行),但是貌似沒有默認提供的效果,而要做出一些效果,需要自己設計實現,官方Demo中已經有了一些實現,WinForm中的GMapMarker可以用GMarkerGoogle去實例化(提供的有可選的效果,也可以自己傳入bitmap作為自定義的圖標)。

做過一段時間ArcGIS開發,由於ArcGIS的授權費用,為了節省成本,開始搞GMap.Net。。。剛開始研究,待續。。。

技術交流 省厓 QQ:2252224326 [email protected] 版權所有 http://blog.sina.com.cn/u/6029512413

GMap.Net解決方案之在WinForm和WPF中使用GMap.Net地圖插件的開發