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

Windows 8 地理位置定位 1.快速上手

[复制链接]

尚未签到

发表于 2015-5-23 07:55:45 | 显示全部楼层 |阅读模式
  在 Windows 8 中,内置的 Windows 定位程序基于 Wi-Fi 三角定位法和 IP 地址数据为应用提供位置数据。
  Windows 定位程序使用来自 Wi-Fi 访问点的数据来计算纬度和经度。根据 Wi-Fi 数据计算的位置在市区内可精确到 350 米。
  当 Wi-Fi 数据不可用时,Windows 定位程序使用 IP 地址解析来获得可精确到 50 千米的大概位置。
  打开Visual Studio 2012
  新建->项目->Windows 应用商店->空白应用程序,取名叫Win8Location
  打开Package.appxmanifest,在“功能”标签页中勾选“位置”。没有这一步下面的程序是不能用的哦。
  就一个页面MainPage
  先上代码
  前台MainPage.xaml代码如下:



1
9
10     
11         30
12         10
13         
14         
15            
16            
17            
18         
19         
20            
21            
22            
23         
24     
25
26     
27         
28            
29            
30         
31         
32            
33            
34         
35         
36            
37            
38         
39         
40            
41            
42            
43            
44            
45            
46            
47            
48         
49         
50            
51            
52            
53            
54            
55         
56     
57
  后台MainPage.xaml.cs代码如下:



1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using Windows.Devices.Geolocation;
6 using Windows.Foundation;
7 using Windows.Foundation.Collections;
8 using Windows.UI.Xaml;
9 using Windows.UI.Xaml.Controls;
10 using Windows.UI.Xaml.Controls.Primitives;
11 using Windows.UI.Xaml.Data;
12 using Windows.UI.Xaml.Input;
13 using Windows.UI.Xaml.Media;
14 using Windows.UI.Xaml.Navigation;
15
16 namespace Win8Location
17 {
18     public sealed partial class MainPage : Page
19     {
20         Geolocator geo = null;
21
22         public MainPage()
23         {
24             this.InitializeComponent();
25         }
26
27         private async void btnGetLocation_Click(object sender, RoutedEventArgs e)
28         {
29             txtMsg.Text = "异常:无";
30             if (geo == null)
31             {
32                 geo = new Geolocator();
33             }
34             try
35             {
36                 Geoposition pos = await geo.GetGeopositionAsync();
37                 Geocoordinate coordinate = pos.Coordinate;
38                 CivicAddress civicAddress = pos.CivicAddress;
39
40                 txtLongitude.Text = "经度(以度为单位): " + coordinate.Longitude.ToString();
41                 txtLatitude.Text = "纬度(以度为单位): " + coordinate.Latitude.ToString();
42                 txtAccuracy.Text = "位置的精度(以米为单位): " + coordinate.Accuracy.ToString();
43                 txtAltitude.Text = "位置的海拔(以米为单位): " + coordinate.Altitude.ToString();
44                 txtAltitudeAccuracy.Text = "海拔的精度(以米为单位): " + coordinate.AltitudeAccuracy.ToString();
45                 txtHeading.Text = "当前行进方向(以相对于真北的度数为单位): " + coordinate.Heading.ToString();
46                 txtSpeed.Text = "速度(以米/秒为单位): " + coordinate.Speed.ToString();
47                 txtTimestamp.Text = "位置确定的时间: " + coordinate.Timestamp.ToString();
48
49                 txtCountry.Text = "国家/地区:" + civicAddress.Country;
50                 txtState.Text = "洲或省:" + civicAddress.State;
51                 txtCity.Text = "城市:" + civicAddress.City;
52                 txtPostalCode.Text = "邮政编码:" + civicAddress.PostalCode;
53                 txtTimestamp2.Text = "位置数据获取的时间:" + civicAddress.Timestamp;
54             }
55             catch (Exception ex)
56             {
57                 txtMsg.Text = "异常:" + ex.Message;
58             }
59         }
60     }
61 }
  下面是运行截图:
DSC0000.png
  在后台代码第36行,通过F12查看类Geoposition的定义,可以看到该类有两个只读属性public Geocoordinate Coordinate { get; }和public CivicAddress CivicAddress { get; }。
继续F12可以看到类Geocoordinate有8个只读属性,类CivicAddress有5个只读属性,于是这里把这13个属性都显示了出来。
咦!为啥有些数据没有获得呢?
因为Windows 定位程序向应用程序提供纬度、经度和精确度信息。Windows 定位程序不会提供有关朝向、速度、海拔或街道地址的信息—其他定位程序可能会向应用程序提供此数据。
MSDN上有相关说明http://msdn.microsoft.com/zh-cn/library/windows/apps/hh464919.aspx

运维网声明 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.iyunv.com/thread-69677-1-1.html 上篇帖子: Windows 8 应用商店应用开发 之 画刷 下篇帖子: Windows 8 地理位置定位 3.位置变化跟踪
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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