现在的位置: 首页 > 综合 > 正文

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

2012年12月22日 ⁄ 综合 ⁄ 共 4837字 ⁄ 字号 评论关闭

在 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 <Page
 2     x:Class="Win8Location.MainPage"
 3     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 4     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 5     xmlns:local="using:Win8Location"
 6     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 7     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 8     mc:Ignorable="d">
 9 
10     <Page.Resources>
11         <x:Double x:Key="MyFontSize">30</x:Double>
12         <Thickness x:Key="MyMargin">10</Thickness>
13         <SolidColorBrush x:Key="MyWarningColor" Color="Red"></SolidColorBrush>
14         <Style TargetType="Button">
15             <Setter Property="FontSize" Value="{StaticResource MyFontSize}"></Setter>
16             <Setter Property="Margin" Value="{StaticResource MyMargin}"></Setter>
17             <Setter Property="FontWeight" Value="Light"></Setter>
18         </Style>
19         <Style TargetType="TextBlock">
20             <Setter Property="FontSize" Value="{StaticResource MyFontSize}"></Setter>
21             <Setter Property="Margin" Value="{StaticResource MyMargin}"></Setter>
22             <Setter Property="IsTextSelectionEnabled" Value="True"></Setter>
23         </Style>
24     </Page.Resources>
25 
26     <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
27         <Grid.RowDefinitions>
28             <RowDefinition Height="auto"/>
29             <RowDefinition Height="*"/>
30         </Grid.RowDefinitions>
31         <Grid.ColumnDefinitions>
32             <ColumnDefinition Width="*"/>
33             <ColumnDefinition Width="*"/>
34         </Grid.ColumnDefinitions>
35         <StackPanel Grid.ColumnSpan="2">
36             <Button x:Name="btnGetLocation" Content="点击获得位置信息" Click="btnGetLocation_Click"></Button>
37             <TextBlock x:Name="txtMsg" Text="异常:无" Foreground="{StaticResource MyWarningColor}"></TextBlock>
38         </StackPanel>
39         <StackPanel Grid.Row="1">
40             <TextBlock x:Name="txtLongitude" Text="经度(以度为单位):"></TextBlock>
41             <TextBlock x:Name="txtLatitude" Text="纬度(以度为单位):"></TextBlock>
42             <TextBlock x:Name="txtAccuracy" Text="位置的精度(以米为单位):"></TextBlock>
43             <TextBlock x:Name="txtAltitude" Text="位置的海拔(以米为单位):"></TextBlock>
44             <TextBlock x:Name="txtAltitudeAccuracy" Text="海拔的精度(以米为单位):"></TextBlock>
45             <TextBlock x:Name="txtHeading" Text="当前行进方向(以相对于真北的度数为单位):"></TextBlock>
46             <TextBlock x:Name="txtSpeed" Text="速度(以米/秒为单位):"></TextBlock>
47             <TextBlock x:Name="txtTimestamp" Text="位置确定的时间:"></TextBlock>
48         </StackPanel>
49         <StackPanel Grid.Row="1" Grid.Column="1">
50             <TextBlock x:Name="txtCountry" Text="国家/地区:"></TextBlock>
51             <TextBlock x:Name="txtState" Text="洲或省:"></TextBlock>
52             <TextBlock x:Name="txtCity" Text="城市:"></TextBlock>
53             <TextBlock x:Name="txtPostalCode" Text="邮政编码:"></TextBlock>
54             <TextBlock x:Name="txtTimestamp2" Text="位置数据获取的时间:"></TextBlock>
55         </StackPanel>
56     </Grid>
57 </Page>

后台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

抱歉!评论已关闭.