|
关于根据经纬度计算地面两点间距离的公式及推导可以参考我的另一篇博客http://www.iyunv.com/chengyujia/archive/2013/01/13/2858484.html
本例依然只有一个页面
先上运行截图:
前台XAML代码:
XAML
1
9
10 25
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
后台C#代码:
C#
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using System.Text;
6 using Windows.Devices.Geolocation;
7 using Windows.Foundation;
8 using Windows.Foundation.Collections;
9 using Windows.UI.Core;
10 using Windows.UI.Xaml;
11 using Windows.UI.Xaml.Controls;
12 using Windows.UI.Xaml.Controls.Primitives;
13 using Windows.UI.Xaml.Data;
14 using Windows.UI.Xaml.Input;
15 using Windows.UI.Xaml.Media;
16 using Windows.UI.Xaml.Navigation;
17
18 namespace Win8Location
19 {
20 public sealed partial class DistanceAndSpeed : Page
21 {
22 Geolocator geo = null;
23 private double prevLongitude;
24 private double prevLatitude;
25 private double totalDistance;//(米)
26 private DateTimeOffset prevTime;
27 private TimeSpan totalTime = TimeSpan.Zero;
28
29 public DistanceAndSpeed()
30 {
31 this.InitializeComponent();
32 }
33
34 async private void btnStartTracking_Click(object sender, RoutedEventArgs e)
35 {
36 btnStartTracking.IsEnabled = false;
37 btnStopTracking.IsEnabled = true;
38 if (geo == null)
39 {
40 geo = new Geolocator();
41 }
42 if (geo != null)
43 {
44 geo.StatusChanged += geo_StatusChanged;
45 try
46 {
47 Geoposition position = await geo.GetGeopositionAsync();
48 string msg = position.Coordinate.Timestamp + ">开始定位并跟踪\n";
49 msg += "当前经度:" + position.Coordinate.Longitude + "\n";
50 msg += "当前纬度:" + position.Coordinate.Latitude + "\n\n";
51 txtMsg.Text += msg;
52 prevLongitude = position.Coordinate.Longitude;
53 prevLatitude = position.Coordinate.Latitude;
54 prevTime = position.Coordinate.Timestamp;
55 }
56 catch (Exception ex)
57 {
58 txtMsg.Text += DateTime.Now + ">出错了:" + ex.Message + "\n\n";
59 }
60 geo.PositionChanged += geo_PositionChanged;
61 }
62 }
63
64 async void geo_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
65 {
66 Geoposition pos = args.Position;
67 double currLong = pos.Coordinate.Longitude;
68 double currLat = pos.Coordinate.Latitude;
69 DateTimeOffset currTime = pos.Coordinate.Timestamp;
70
71 double updateDistance = GetDistance(prevLongitude, prevLatitude, currLong, currLat);
72 totalDistance += updateDistance;
73 TimeSpan updateTime = currTime - prevTime;
74 totalTime += updateTime;
75
76 StringBuilder msg = new StringBuilder();
77 msg.Append(currTime + "\n");
78 msg.Append("当前经度:" + args.Position.Coordinate.Longitude + "\n");
79 msg.Append("当前纬度:" + args.Position.Coordinate.Latitude + "\n");
80 msg.Append("距上一个位置的距离(米):" + updateDistance.ToString("0") + "\n");
81 msg.Append("距上一个位置经历的时间:" + updateTime.ToString(@"hh\:mm\:ss") + "\n");
82 msg.Append("距上一个位置间的平均速度(米/秒):" + GetSpeed(updateDistance, updateTime).ToString("0") + "\n");
83 msg.Append("已经历的总路程(米):" + totalDistance.ToString("0") + "\n");
84 msg.Append("已经历的总时间:" + totalTime.ToString(@"hh\:mm\:ss") + "\n");
85 msg.Append("已经历的总平均速度(米/秒):" + GetSpeed(totalDistance, totalTime).ToString("0") + "\n\n");
86
87 await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
88 {
89 txtMsg.Text += msg;
90 });
91
92 prevLongitude = currLong;
93 prevLatitude = currLat;
94 prevTime = currTime;
95 }
96
97 async void geo_StatusChanged(Geolocator sender, StatusChangedEventArgs args)
98 {
99 string msg = DateTime.Now + ">定位器状态:" + args.Status + "\n\n";
100 await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
101 {
102 txtMsg.Text += msg;
103 });
104 }
105
106 private void btnStopTracking_Click(object sender, RoutedEventArgs e)
107 {
108 btnStartTracking.IsEnabled = true;
109 btnStopTracking.IsEnabled = false;
110 if (geo != null)
111 {
112 txtMsg.Text += DateTime.Now + ">停止位置跟踪\n\n";
113 geo.StatusChanged -= geo_StatusChanged;
114 geo.PositionChanged -= geo_PositionChanged;
115 }
116 }
117
118 ///
119 /// 根据经纬度计算地面两点间的距离
120 ///
121 /// 前一个点的经度
122 /// 前一个点的纬度
123 /// 当前点的经度
124 /// 当前点的纬度
125 /// 两点间的距离(米)
126 private double GetDistance(double prevLong, double prevLat, double currLong, double currLat)
127 {
128 const double degreesToRadians = (Math.PI / 180.0);
129 const double earthRadius = 6371; // 地球半径平均值为6371千米
130
131 var prevRadLong = prevLong * degreesToRadians;
132 var prevRadLat = prevLat * degreesToRadians;
133 var currRadLong = currLong * degreesToRadians;
134 var currRadLat = currLat * degreesToRadians;
135
136 double cosX = Math.Cos(prevRadLat) * Math.Cos(currRadLat) * Math.Cos(prevRadLong - currRadLong) + Math.Sin(prevRadLat) * Math.Sin(currRadLat);
137 double X = Math.Acos(cosX);//两点与球心连线的夹角
138 double d = earthRadius * X * 1000;//单位转换为米
139 return d;
140 }
141
142 private double GetSpeed(double distance, TimeSpan timeSpan)
143 {
144 double speed = distance / timeSpan.TotalSeconds;
145 return speed;
146 }
147
148 private void btnClear_Click(object sender, RoutedEventArgs e)
149 {
150 txtMsg.Text = string.Empty;
151 }
152 }
153 }
|
|
|