GMap.NET是什么?windows
来看看它的官方说明: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!api
GMap.NET是一个强大、免费、跨平台、开源的.NET控件,它在Windows Forms 和WPF环境中可以使用来自Google, Yahoo!, Bing, OpenStreetMap, ArcGIS, Pergo, SigPac等地图,并能够实现寻找路径、地理编码以及地图展现功能,并支持缓存和运行在Mobile环境中。缓存
项目主页:https://greatmaps.codeplex.com/ide
如何在WinForm中使用GMap.Net工具
下载GMap.Net,我下载的版本:greatmaps_81b71bf30091,编译三个核心项目:google
GMap.Net.Core:核心DLL编码
GMap.Net.WindowsForms:WinForm中使用的DLLspa
GMap.NET.WindowsPresentation:WPF中使用的DLL设计
在WinForm项目中使用GMap:code
一、新建一个Visual C# 的Windows窗口程序。添加对GMap.Net.Core.DLL和GMap.Net.WindowsForms.DLL的引用。
二、在项目中添加一个UserControl,这里取名为MapControl,修改这个UserControl,使其继承于GMapControl,这就是展现地图的控件。修改以下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using GMap.NET.WindowsForms; namespace GMapWinFormDemo { public partial class MapControl : GMapControl { public MapControl() { InitializeComponent(); } } }
三、编译项目,在咱们的Form设计窗口下,在工具箱中(tool box)里就能够看到这个MapControl,将这个MapControl加到Form中。
四、在主Form中添加相关的代码以下
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using GMap.NET; using GMap.NET.WindowsForms; using GMap.NET.MapProviders;
using GMap.NET.WindowsForms.Markers; namespace GMapDemo { public partial class MapForm : Form { private GMapOverlay markersOverlay = new GMapOverlay("markers"); //放置marker的图层 public MapForm() { InitializeComponent(); try { System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry("ditu.google.cn"); } catch { mapControl.Manager.Mode = AccessMode.CacheOnly; MessageBox.Show("No internet connection avaible, going to CacheOnly mode.", "GMap.NET Demo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } mapControl.CacheLocation = Environment.CurrentDirectory + "\\GMapCache\\"; //缓存位置 mapControl.MapProvider = GMapProviders.GoogleChinaMap; //google china 地图 mapControl.MinZoom = 2; //最小比例 mapControl.MaxZoom = 24; //最大比例 mapControl.Zoom = 10; //当前比例 mapControl.ShowCenter = false; //不显示中心十字点 mapControl.DragButton = System.Windows.Forms.MouseButtons.Left; //左键拖拽地图 mapControl.Position = new PointLatLng(32.064,118.704); //地图中心位置:南京 mapControl.Overlays.Add(markersOverlay); mapControl.MouseClick += new MouseEventHandler(mapControl_MouseClick); } void mapControl_MouseClick(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Right) { PointLatLng point = mapControl.FromLocalToLatLng(e.X, e.Y); GMapMarker marker = new GMarkerGoogle(point, GMarkerGoogleType.green); markersOverlay.Markers.Add(marker); } } }
五、编译、运行项目就能够看到地图,这里使用的是在线的Google中国的地图,地图控件的几个主要属性:
MapProvider:地图服务的提供者。
MinZoom:最小缩放,最小可为1。
MaxZoom:最大缩放,最大为24.
Zoom:当前缩放。
ShowCenter:是否显示中心点(最好为false,不然地图中间会有一个红色的十字)。
DragButton:哪一个键拖动地图。
Position:地图中心点位置。
地图显示以下,支持左键拖动,放大缩小,能够显示左键的点击经纬度。
如何在WPF中使用GMap.Net
一、新建一个Visual C# 的WPF程序。添加对GMap.Net.Core.DLL和GMap.NET.WindowsPresentation.DLL的引用。
二、因为WPF的UserControl不能修改继承的基类,因此添加一个新的类,为MapControl.cs,代码以下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using GMap.NET.WindowsPresentation; namespace GMapWPFDemo { class MapControl : GMapControl { } }
只须要继承GMapControl就好了,基本功能均可以由GMapControl提供。
三、在咱们的MainWindow.xaml中,添加项目的namespace:xmlns:src="clr-namespace:GMapWPFDemo",在XML代码中添加对MapControl.cs的使用:
<Window x:Class="GMapWPFDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src="clr-namespace:GMapWPFDemo" Title="MainWindow" Height="410" Width="618"> <Grid> <GroupBox Name="mapgroup" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch"> <src:MapControl x:Name="mapControl" Zoom="13" MaxZoom="24" MinZoom="1" /> </GroupBox> </Grid> </Window>
四、在MainWindow中添加相关的代码以下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using GMap.NET; using GMap.NET.MapProviders; using GMap.NET.WindowsPresentation; namespace GMapWPFDemo { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); try { System.Net.IPHostEntry e = System.Net.Dns.GetHostEntry("ditu.google.cn"); } catch { mapControl.Manager.Mode = AccessMode.CacheOnly; MessageBox.Show("No internet connection avaible, going to CacheOnly mode.", "GMap.NET Demo", MessageBoxButton.OK, MessageBoxImage.Warning); } mapControl.MapProvider = GMapProviders.GoogleChinaMap; //google china 地图 mapControl.MinZoom = 2; //最小缩放 mapControl.MaxZoom = 17; //最大缩放 mapControl.Zoom = 5; //当前缩放 mapControl.ShowCenter = false; //不显示中心十字点 mapControl.DragButton = MouseButton.Left; //左键拖拽地图 mapControl.Position = new PointLatLng(32.064, 118.704); //地图中心位置:南京 mapControl.MouseLeftButtonDown += new MouseButtonEventHandler(mapControl_MouseLeftButtonDown); } void mapControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Point clickPoint = e.GetPosition(mapControl); PointLatLng point = mapControl.FromLocalToLatLng((int)clickPoint.X, (int)clickPoint.Y); GMapMarker marker = new GMapMarker(point); mapControl.Markers.Add(marker); } } }
效果图以下:
和Winform代码差很少,一些响应事件不一样,WPF的GMap中没有GMapOverlay这个“图层”的概念,因此无法加多个GMapOverlay,在GMapOverlay上再加GMapMarker(能够理解为图标标注),GMapMarker只能直接加在mapControl上面。
WPF的GMapMarker能够直接实例化(WinForm中的不行),可是貌似没有默认提供的效果,而要作出一些效果,须要本身设计实现,官方Demo中已经有了一些实现,WinForm中的GMapMarker能够用GMarkerGoogle去实例化(提供的有可选的效果,也能够本身传入bitmap做为自定义的图标)。
作过一段时间ArcGIS开发,因为ArcGIS的受权费用,为了节省成本,开始搞GMap.Net。。。刚开始研究,待续。。。