tonwei139 发表于 2015-5-23 07:55:45

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

  在 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 }
  下面是运行截图:

  在后台代码第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]
查看完整版本: Windows 8 地理位置定位 1.快速上手