设为首页 收藏本站
查看: 1005|回复: 0

Windows 8 地理位置定位 4.根据经纬度计算地面两点间的距离

[复制链接]

尚未签到

发表于 2015-5-23 08:56:03 | 显示全部楼层 |阅读模式
  关于根据经纬度计算地面两点间距离的公式及推导可以参考我的另一篇博客http://www.iyunv.com/chengyujia/archive/2013/01/13/2858484.html
  本例依然只有一个页面
  先上运行截图:
DSC0000.png
  前台XAML代码:


DSC0001.gif DSC0002.gif 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 }
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-69699-1-1.html 上篇帖子: Windows 8实用窍门系列:15.Windows 8中的4种视图状态和锁屏通知 下篇帖子: Windows 8 应用商店应用开发 之 图形绘制(1)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表