一、前言
最近公司需要开发一个车辆在途轨迹追踪的软件,结合现有系统和技术体系,最终敲定使用WPF+Gmap.Net来实现,这里将一些坑踩一下,做个笔记记录一下。
二、项目搭建
本项目基于.Net6.0+Gmap.Net.Core+Gmap.Net.WinPresentation,前面是用到的框架版本,后面则是需要用到的地图包,可通过visual studio编辑器的包管理下载
三、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/
需要说明的是本笔记只讲述Gmap.Net在WPF中的一些粗浅运用,不涉及Winform相关知识。
四、WPF中使用Gmap.Net
第一步,创建地图控件,地图供应商(本文将使用高德地图来进行演示),正确的显示地图
创建地图控件类MapControl,继承Gmap提供的地图类
点击查看代码
using GMap.NET.MapProviders;
using GMap.NET.Projections;
using GMap.NET;
using GMap.NET.WindowsPresentation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
namespace WisdomTrack.Views
{
public class MapControl:GMapControl
{
}
}
在窗体的Xaml引用这个地图控件
点击查看代码
<Window x:Class="WisdomTrack.Views.RouteWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WisdomTrack.Views"
xmlns:system="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="RouteWindow" Height="600" Width="1200"
<Grid>
<local:MapControl x:Name="mapControl"> </local:MapControl>
</Grid>
</Window>
点击查看代码
public abstract class MapProviderBase : GMapProvider
{
public MapProviderBase()
{
}
public override PureProjection Projection
{
get { return MercatorProjection.Instance; }
}
GMapProvider[] overlays;
public override GMapProvider[] Overlays
{
get
{
if (overlays == null)
{
overlays = new GMapProvider[] { this };
}
return overlays;
}
}
}
点击查看代码
/// <summary>
/// 高德地图
/// </summary>
public class AMapProvider : MapProviderBase
{
public static readonly AMapProvider Instance;
static readonly string UrlFormat = "yourSDK";
readonly Guid id = new Guid("EF3DD303-3F74-4938-BF40-232D0595EE88");
public override Guid Id
{
get { return id; }
}
readonly string name = "AMap";
public override string Name
{
get
{
return name;
}
}
private AMapProvider()
{
}
static AMapProvider()
{
Instance = new AMapProvider();
}
//根据坐标和缩放,获取对应的图片。
public override PureImage GetTileImage(GPoint pos, int zoom)
{
string url = MakeTileImageUrl(pos, zoom, LanguageStr);
return GetTileImageUsingHttp(url);
}
string MakeTileImageUrl(GPoint pos, int zoom, string language)
{
string url = string.Format(UrlFormat, pos.X, pos.Y, zoom);
Console.WriteLine("url:" + url);
return url;
}
}
点击查看代码
/// <summary>
/// 地图初始化加载
/// </summary>
private void MapLoad()
{
mapControl.MapProvider = AMapProvider.Instance; //高德 china 地图
mapControl.Manager.Mode = AccessMode.ServerAndCache;
mapControl.MinZoom = 2; //最小缩放
mapControl.MaxZoom = 18; //最大缩放
mapControl.Zoom = 10; //当前缩放
mapControl.ShowCenter = false; //不显示中心十字点
mapControl.DragButton = MouseButton.Right; //右键拖拽地图
mapControl.Position = new PointLatLng(30.66, 104.07); //地图中心位置:南京(纬度,经度)
}