c# wpf使用GMap.NET類庫(kù),實(shí)現(xiàn)地圖軌跡回放
前言
實(shí)現(xiàn)軌跡回放,GMap.NET有對(duì)應(yīng)的類GMapRoute。這個(gè)類函數(shù)很少,功能有限,只能實(shí)現(xiàn)簡(jiǎn)單的軌跡回放。要實(shí)現(xiàn)更復(fù)雜的軌跡回放,就需要自己動(dòng)手了。
本文介紹一種方法,可以實(shí)現(xiàn)復(fù)雜的軌跡回放。有句話“功夫在詩(shī)外”,GMap.NET給你提供了基本地圖處理功能;但是不要讓CMap.NET束縛了手腳。你需要有深刻理解地圖實(shí)現(xiàn)原理,深入理解WPF動(dòng)畫的原理,才能到達(dá)隨心所欲。最終的效果如下:
GMap.NET 顯示原理
地圖就是由許多方格“瓦片”組合而來(lái)。當(dāng)你移動(dòng)或縮放時(shí),GMap.NET會(huì)根據(jù)當(dāng)前位置、顯示窗口、縮放級(jí)別,到地圖服務(wù)器獲取圖片。所以地圖控件本質(zhì)上就是顯示圖片的控件,只是這些圖片包含了坐標(biāo)信息。
地圖上加軌跡,就是在圖片上畫線。這些線要與gps坐標(biāo)點(diǎn)吻合。通過(guò)GMapMarker不僅可以加標(biāo)注,也可以實(shí)現(xiàn)軌跡。需要將gps坐標(biāo)點(diǎn)轉(zhuǎn)換成控件的坐標(biāo)點(diǎn),再連成線就可以了。本文就是通過(guò)GMapMarker實(shí)現(xiàn)了軌跡回放。
1 實(shí)現(xiàn)軌跡顯示
通過(guò)自定義控件UserControlMapRoute實(shí)現(xiàn)了軌跡顯示功能。需要將此控件加入到GMapMarker。
GMapMarker _routeMaker = new GMapMarker(point); UserControlMapRoute routeCtrl = new UserControlMapRoute() { Marker = _routeMaker, MapCtrl = MainMap }; routeCtrl.Init(); _routeMaker.Shape = routeCtrl; //將圖層添加到地圖 this.MainMap.Markers.Add(_routeMaker);
UserControlMapRoute有兩個(gè)功能:顯示軌跡起始點(diǎn),顯示軌跡。將軌跡顯示功能放在類MapRoutePath中實(shí)現(xiàn)。該類實(shí)現(xiàn)的功能就是根據(jù)gps坐標(biāo)顯示軌跡。該類包含的變量有:
class MapRoutePath { public GMapControl MapCtrl { get; private set; } //地圖控件 public Panel ParentPanel { get; private set; } //父面板,將PathRouteLine加入面板。 public Path PathRouteLine { get; private set; } //顯示軌跡 List<PointLatLng> _listGpsPoint = new List<PointLatLng>(); List<Point> _listCtrlPt = new List<Point>(); public List<PointLatLng> ListGpsPoint => _listGpsPoint; //包含的gps坐標(biāo) public List<Point> ListPathPoint => _listCtrlPt; //轉(zhuǎn)換成立控件坐標(biāo) }
實(shí)現(xiàn)軌跡功能是變量PathRouteLine,該變量的父控件是ParentPanel(就是控件UserControlMapRoute 中的根Grid控件)。MapCtrl 控件主要作用就是提供了將gps坐標(biāo)轉(zhuǎn)換成控件坐標(biāo)的函數(shù)。
實(shí)現(xiàn)將gps做標(biāo)注轉(zhuǎn)換成控件坐標(biāo)的方法:
private void ToLocalPoint() { //_listGpsPoint存儲(chǔ)所gps坐標(biāo) _listCtrlPt存儲(chǔ)轉(zhuǎn)換后控件坐標(biāo) _listCtrlPt.Clear(); foreach (PointLatLng pt in _listGpsPoint) { Point ptGrid = ToCtrlPoint(pt); _listCtrlPt.Add(ptGrid); } } Point ToCtrlPoint(PointLatLng gpsPoint) { //轉(zhuǎn)換成GMap.NET控件坐標(biāo) GPoint ptOfMapCtrl = MapCtrl.FromLatLngToLocal(gpsPoint); //GMap.NET控件坐標(biāo)要轉(zhuǎn)換成 控件相對(duì)于直接父面板的坐標(biāo) Point ptToMapCtrl2 = new Point(ptOfMapCtrl.X, ptOfMapCtrl.Y); Point ptOfScreen = MapCtrl.PointToScreen(ptToMapCtrl2); Point ptOfParentPanel = ParentPanel.PointFromScreen(ptOfScreen); return ptOfParentPanel; }
坐標(biāo)轉(zhuǎn)換過(guò)程就是: GPS坐標(biāo) --》 GMap.NET控件坐標(biāo) --》 屏幕坐標(biāo) --》 控件相對(duì)于直接父面板的坐標(biāo)。獲取了控件坐標(biāo),就根據(jù)這些坐標(biāo)畫直線就行了。
private static void CreatPath(Path path, List<Point> listPt) { if (listPt.Count <= 1) { path.Data = null; return; } PathFigure pathFigure = new PathFigure(); pathFigure.StartPoint = listPt[0]; //起始點(diǎn) for (int i = 1; i < listPt.Count; i++) { //加入線段 LineSegment line = new LineSegment() { Point = listPt[i] }; pathFigure.Segments.Add(line); } PathGeometry geometry = new PathGeometry(); geometry.Figures.Add(pathFigure); path.Data = geometry; }
2 實(shí)現(xiàn)軌跡回放.
要實(shí)現(xiàn)兩個(gè)功能:通過(guò)不同的線顏色來(lái)指示當(dāng)前行動(dòng)軌跡;提示當(dāng)前所在的位置、用時(shí)等信息的tip框。
軌跡移動(dòng) 假如顯示軌跡的線顏色為紅色,通過(guò)綠色來(lái)顯示當(dāng)前經(jīng)過(guò)的位置。再增加一個(gè)變量_pathMoveRouteLine(類型也為Path) 就可以了。_pathMoveRouteLine的顏色為綠色,所顯示的路徑要和PathRouteLine 路徑完全相同。_pathMoveRouteLine路徑長(zhǎng)度要實(shí)時(shí)計(jì)算出來(lái),隨著時(shí)間推移,路徑不斷變長(zhǎng)。需要增加一個(gè)定時(shí)器,不停的計(jì)算當(dāng)前所在的位置。在定時(shí)器中,調(diào)用函數(shù)ShowRouteMove();
private void ShowRouteMove() { //線路總長(zhǎng)度 double totalDistance = GetDistance(); if (totalDistance == 0) return; //更加時(shí)間,計(jì)算當(dāng)前走過(guò)的長(zhǎng)度 TimeSpan span = DateTime.Now - _startMoveTime; double curDistance = _moveSpeed * span.TotalHours; if (curDistance > totalDistance) { StopMove(); curDistance = totalDistance; } Path path = CreateMovePath(); //根據(jù)已走過(guò)的距離,獲取需要顯示的點(diǎn) List<Point> listCtrlPoint = GetListByDistance(curDistance); CreatPath(path, listCtrlPoint); ShowMoveTip(listCtrlPoint.Last(), curDistance); }
//根據(jù)當(dāng)前移動(dòng)的距離,獲取相應(yīng)的控件坐標(biāo) private List<Point> GetListByDistance(double distance) { List<Point> result = new List<Point>(); double start = 0; int i = 0; PointLatLng lastPt = new PointLatLng(); foreach (PointLatLng pt in ListGpsPoint) { i++; if (i == 1) //第一個(gè)點(diǎn) { result.Add(ToCtrlPoint(pt)); lastPt = pt; continue; } else { double lineDistance = MapHelper.GetDistance(lastPt, pt); lastPt = pt; if (lineDistance == 0) continue; if ((start + lineDistance) == distance) //gps坐標(biāo)恰好符合當(dāng)前的距離 { result.Add(ToCtrlPoint(pt)); break; } else if ((start + lineDistance) < distance) //當(dāng)前的點(diǎn)小于需要的距離 { result.Add(ToCtrlPoint(pt)); start += lineDistance; } else { //最終的點(diǎn) 落在兩個(gè)gps點(diǎn)之間,需要進(jìn)一步計(jì)算 double midDistance = distance - start; double rate = midDistance / lineDistance; Point endPoint = ToCtrlPoint(pt); Point midPoint = MapHelper.GetMidPoint(result.Last(), endPoint, rate); result.Add(midPoint); break; } } } return result; }
class MapHelper { //根據(jù)兩點(diǎn)坐標(biāo),和在這兩點(diǎn)之間的比例,獲取計(jì)算后的坐標(biāo) internal static Point GetMidPoint(Point start, Point end, double rate) { Point result = new Point(); result.X = start.X + rate * (end.X - start.X); result.Y = start.Y + rate * (end.Y - start.Y); return result; } }
提示框顯示
提示框所在的位置就是移動(dòng)軌跡的最后一個(gè)點(diǎn)的位置。為了更好的顯示效果,對(duì)這個(gè)坐標(biāo)做一定的偏移:
UserControlMoveTip _userControlMoveTip; private void ShowMoveTip(Point startPoint,double moveDistance) { if(_userControlMoveTip == null) { _userControlMoveTip = new UserControlMoveTip(); _userControlMoveTip.HorizontalAlignment = HorizontalAlignment.Left; _userControlMoveTip.VerticalAlignment = VerticalAlignment.Top; ParentPanel.Children.Add(_userControlMoveTip); } if (_userControlMoveTip.ActualHeight == double.NaN) { _userControlMoveTip.Visibility = Visibility.Hidden; return; } _userControlMoveTip.Visibility = Visibility.Visible; _userControlMoveTip.TotalDistance = GetDistance(); _userControlMoveTip.TotalTimeSpan = TimeSpan.FromHours(_userControlMoveTip.TotalDistance / _moveSpeed); _userControlMoveTip.MoveSpeed = _moveSpeed; _userControlMoveTip.MoveDistance = moveDistance; _userControlMoveTip.TimeElapse = (DateTime.Now - _startMoveTime); _userControlMoveTip.Margin = new Thickness(startPoint.X+5, startPoint.Y - _userControlMoveTip.ActualHeight-2, 0, 0); }
_userControlMoveTip是用戶控件,用來(lái)顯示總距離、已移動(dòng)距離、移動(dòng)時(shí)間等信息。
后記:
有些開發(fā)者反映GMap.NET控件的WPF版提供的功能不夠完善,有些功能不能采用拿來(lái)主義的方式。誠(chéng)然,軟件開發(fā)越來(lái)越復(fù)雜,借鑒別人的代碼是必須的,但是不能丟棄軟件開發(fā)的一些“基本功”。WPF確實(shí)不太好學(xué),好多新的概念難以理解。好多開發(fā)者學(xué)習(xí)WPF淺嘗輒止,所以在使用一些控件時(shí),感到茫然。
以上就是c# wpf使用GMap.NET類庫(kù),實(shí)現(xiàn)地圖軌跡回放的詳細(xì)內(nèi)容,更多關(guān)于c# wpf GMap.NET類庫(kù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Unity讀取Excel文件轉(zhuǎn)換XML格式文件
這篇文章主要為大家詳細(xì)介紹了Unity讀取Excel文件轉(zhuǎn)換XML格式文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06c#基于WinForm的Socket實(shí)現(xiàn)簡(jiǎn)單的聊天室 IM
這篇文章主要介紹了c#基于WinForm的Socket實(shí)現(xiàn)簡(jiǎn)單的聊天室 IM的步驟,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-05-05C#批量插入數(shù)據(jù)到Sqlserver中的三種方式
這篇文章主要為大家詳細(xì)介紹了C#批量插入數(shù)據(jù)到Sqlserver中的三種方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12C# 實(shí)現(xiàn)QQ式截圖功能實(shí)例代碼
本篇文章主要介紹了C# 實(shí)現(xiàn)QQ式截圖功能實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02C# WinForm中Panel實(shí)現(xiàn)用鼠標(biāo)操作滾動(dòng)條的實(shí)例方法
由于在WinForm中Panel不能直接響應(yīng)鼠標(biāo)的滾動(dòng)事件,只好采用捕獲窗體的滾動(dòng)事件。2013-03-03C#關(guān)鍵字in、out、ref的作用與區(qū)別
這篇文章介紹了C#關(guān)鍵字in、out、ref的作用與區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04C#?計(jì)算DataTime的4種時(shí)間差的方法(相差天數(shù)、相差小時(shí)、相差分鐘、相差秒)
這篇文章主要介紹了C#?計(jì)算DataTime的4種時(shí)間差(相差天數(shù)、相差小時(shí)、相差分鐘、相差秒),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05C#實(shí)現(xiàn)抓取和分析網(wǎng)頁(yè)類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)抓取和分析網(wǎng)頁(yè)類,實(shí)例分析了C#抓取及分析網(wǎng)頁(yè)中文本及連接的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-05-05