您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何在c# wpf中使用GMap.NET類庫回放地圖軌跡,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
通過自定義控件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存儲所gps坐標(biāo) _listCtrlPt存儲轉(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)換成 控件相對于直接父面板的坐標(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)換過程就是: GPS坐標(biāo) --》 GMap.NET控件坐標(biāo) --》 屏幕坐標(biāo) --》 控件相對于直接父面板的坐標(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; }
要實(shí)現(xiàn)兩個(gè)功能:通過不同的線顏色來指示當(dāng)前行動軌跡;提示當(dāng)前所在的位置、用時(shí)等信息的tip框。
軌跡移動 假如顯示軌跡的線顏色為紅色,通過綠色來顯示當(dāng)前經(jīng)過的位置。再增加一個(gè)變量_pathMoveRouteLine(類型也為Path) 就可以了。_pathMoveRouteLine的顏色為綠色,所顯示的路徑要和PathRouteLine 路徑完全相同。_pathMoveRouteLine路徑長度要實(shí)時(shí)計(jì)算出來,隨著時(shí)間推移,路徑不斷變長。需要增加一個(gè)定時(shí)器,不停的計(jì)算當(dāng)前所在的位置。在定時(shí)器中,調(diào)用函數(shù)ShowRouteMove();
private void ShowRouteMove() { //線路總長度 double totalDistance = GetDistance(); if (totalDistance == 0) return; //更加時(shí)間,計(jì)算當(dāng)前走過的長度 TimeSpan span = DateTime.Now - _startMoveTime; double curDistance = _moveSpeed * span.TotalHours; if (curDistance > totalDistance) { StopMove(); curDistance = totalDistance; } Path path = CreateMovePath(); //根據(jù)已走過的距離,獲取需要顯示的點(diǎn) List<Point> listCtrlPoint = GetListByDistance(curDistance); CreatPath(path, listCtrlPoint); ShowMoveTip(listCtrlPoint.Last(), curDistance); }
//根據(jù)當(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; } }
提示框所在的位置就是移動軌跡的最后一個(gè)點(diǎn)的位置。為了更好的顯示效果,對這個(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是用戶控件,用來顯示總距離、已移動距離、移動時(shí)間等信息。
關(guān)于如何在c# wpf中使用GMap.NET類庫回放地圖軌跡就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。