如何在WinForm中使用GMap.Net
項(xiàng)目主頁(yè):https://greatmaps.codeplex.com/
下載GMap.Net,我下載的版本:greatmaps_81b71bf30091,編譯三個(gè)核心項(xiàng)目:
GMap.Net.Core:核心DLL
GMap.Net.WindowsForms:WinForm中使用的DLL
GMap.NET.WindowsPresentation:WPF中使用的DLL
在WinForm項(xiàng)目中使用GMap:
1、新建一個(gè)Visual C# 的Windows窗口程序。添加對(duì)GMap.Net.Core.DLL和GMap.Net.WindowsForms.DLL的引用。
2、在項(xiàng)目中添加一個(gè)UserControl,這里取名為MapControl,修改這個(gè)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();
}
}
}
3、編譯項(xiàng)目,在我們的Form設(shè)計(jì)窗口下,在工具箱中(tool box)里就可以看到這個(gè)MapControl,將這個(gè)MapControl加到Form中。
4、在主Form中添加相關(guān)的代碼如下:
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("www.google.com.hk");
}
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; //當(dāng)前縮放
mapControl.ShowCenter = false; //不顯示中心十字點(diǎn)
mapControl.DragButton = MouseButton.Left; //左鍵拖拽地圖
mapControl.Position = new PointLatLng(32.064, 118.704); //地圖中心位置:南京
mapControl.OnMapZoomChanged += new MapZoomChanged(mapControl_OnMapZoomChanged);
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);
}
void mapControl_OnMapZoomChanged()
{
}
}
}
5、編譯、運(yùn)行項(xiàng)目就可以看到地圖,這里使用的是在線的Google中國(guó)的地圖,地圖控件的幾個(gè)主要屬性:
MapProvider:地圖服務(wù)的提供者。
MinZoom:最小縮放,最小可為1。
MaxZoom:最大縮放,最大為24.
Zoom:當(dāng)前縮放。
ShowCenter:是否顯示中心點(diǎn)(最好為false,否則地圖中間會(huì)有一個(gè)紅色的十字)。
DragButton:那個(gè)鍵拖動(dòng)地圖。
Position:地圖中心點(diǎn)位置。
地圖顯示如下,支持左鍵拖動(dòng),放大縮小,可以顯示左鍵的點(diǎn)擊經(jīng)緯度。
如何在WPF中使用GMap.Net
1、新建一個(gè)Visual C# 的WPF程序。添加對(duì)GMap.Net.Core.DLL和GMap.NET.WindowsPresentation.DLL的引用。
2、由于WPF的UserControl不能修改繼承的基類,所以添加一個(gè)新的類,為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提供。
3、在我們的MainWindow.xaml中,添加項(xiàng)目的namespace:xmlns:src="clr-namespace:GMapWPFDemo",在XML代碼中添加對(duì)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"
style="margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; background: rgb(242, 246, 251); width: 640px; clear: both; border-top: 1px solid rgb(0, 153, 204); border-right: 1px solid rgb(0, 153, 204); border-left: 1px solid rgb(0, 153, 204); border-image: initial; border-bottom: none; font-family: tahoma, arial, "Microsoft YaHei";"> 復(fù)制代碼代碼如下:
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("www.google.com.hk");
}
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; //當(dāng)前縮放
mapControl.ShowCenter = false; //不顯示中心十字點(diǎn)
mapControl.DragButton = MouseButton.Left; //左鍵拖拽地圖
mapControl.Position = new PointLatLng(32.064, 118.704); //地圖中心位置:南京
mapControl.OnMapZoomChanged += new MapZoomChanged(mapControl_OnMapZoomChanged);
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);
}
void mapControl_OnMapZoomChanged()
{
}
}
}
效果圖如下:
和Winform代碼差不多,一些響應(yīng)事件不同,WPF的GMap中沒有GMapOverlay這個(gè)“圖層”的概念,所以沒法加多個(gè)GMapOverlay,在GMapOverlay上再加GMapMarker(可以理解為圖標(biāo)標(biāo)注),GMapMarker只能直接加在mapControl上面。
WPF的GMapMarker可以直接實(shí)例化(WinForm中的不行),但是貌似沒有默認(rèn)提供的效果,而要做出一些效果,需要自己設(shè)計(jì)實(shí)現(xiàn),官方Demo中已經(jīng)有了一些實(shí)現(xiàn),WinForm中的GMapMarker可以用GMarkerGoogle去實(shí)例化(提供的有可選的效果,也可以自己傳入bitmap作為自定義的圖標(biāo))。