team470 发表于 2015-5-23 07:57:43

Windows 8 地理位置定位 3.位置变化跟踪

  如果在应用中不仅需要知道用户的位置,还需要根据位置的变化呈现不同的内容,则需要跟踪位置变化。这就用上定位核心类Geolocator中的PositionChanged事件了。
Geolocator中有三个属性与该事件有关:
  1.public uint ReportInterval { get; set; }
在位置更新之间请求的最小时间间隔(以毫秒为单位)。如果您的应用程序很少需要更新,则设置此值,以便位置提供程序可通过仅在需要时计算位置来节省电源。
默认值为0,表示随时检测位置变化。
  2.public double MovementThreshold { get; set; }
获取相对于来自最后的 PositionChanged 事件的坐标的移动距离(以米为单位),Geolocator 需要该移动距离来引发 PositionChanged事件。
默认值为0.0,表示只要检测到位置发生变化就触发PositionChanged事件
  3.public PositionAccuracy DesiredAccuracy { get; set; }
处于 Geolocator 的精度级别提供位置更新。PositionAccuracy 是枚举类型,共有两个值:Default和High。由于一般Windows 8 设备没有GPS,只有Wi-Fi和IP定位两种,很多情况下两种精度是一样的。
  下面是代码,仍然只有一个页面。
  前台XAML


前台XAML




20























  后台cs


后台C#


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.Core;
9 using Windows.UI.Xaml;
10 using Windows.UI.Xaml.Controls;
11 using Windows.UI.Xaml.Controls.Primitives;
12 using Windows.UI.Xaml.Data;
13 using Windows.UI.Xaml.Input;
14 using Windows.UI.Xaml.Media;
15 using Windows.UI.Xaml.Navigation;
16
17 namespace Win8Location
18 {
19   public sealed partial class PositionTracking : Page
20   {
21         Geolocator geo = null;
22         public PositionTracking()
23         {
24             this.InitializeComponent();
25             geo = new Geolocator();
26         }
27
28         async private void btnStartTracking_Click(object sender, RoutedEventArgs e)
29         {
30             btnStartTracking.IsEnabled = false;
31             btnStopTracking.IsEnabled = true;
32
33             geo.StatusChanged += geo_StatusChanged;
34
35             if (tsAccuracy.IsOn)
36             {
37               //提供可能获得的最准确的报告。这包括使用可能收费的服务或消耗更高级别的电池电量或连接带宽。High 的精度级别可能会降低系统性能,只有在必要时才使用。
38               geo.DesiredAccuracy = PositionAccuracy.High;
39             }
40             else
41             {
42               //优化电源、性能和其他成本考虑。这是DesiredAccuracy的默认值.
43               geo.DesiredAccuracy = PositionAccuracy.Default;
44             }
45
46             uint reportInterval;
47             if (uint.TryParse(txtReportInterval.Text, out reportInterval))
48             {
49               geo.ReportInterval = reportInterval;
50             }
51
52             double movementThreshold;
53             if (double.TryParse(txtMovementThreshold.Text, out movementThreshold))
54             {
55               geo.MovementThreshold = movementThreshold;
56             }
57
58             Geoposition position = await geo.GetGeopositionAsync();
59             string msg = DateTime.Now + ">定位开始\n";
60             msg += "当前经度:" + position.Coordinate.Longitude + "\n";
61             msg += "当前纬度:" + position.Coordinate.Latitude + "\n";
62             msg += "定位精度(米):" + position.Coordinate.Accuracy + "\n\n";
63             txtMsg.Text += msg;
64             geo.PositionChanged += geo_PositionChanged;
65         }
66
67         async void geo_StatusChanged(Geolocator sender, StatusChangedEventArgs args)
68         {
69             string msg = DateTime.Now + ">状态:" + args.Status + "\n\n";
70             await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
71             {
72               txtMsg.Text += msg;
73             });
74         }
75
76         async void geo_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
77         {
78             string msg = DateTime.Now + "\n";
79             msg += "当前经度:" + args.Position.Coordinate.Longitude + "\n";
80             msg += "当前纬度:" + args.Position.Coordinate.Latitude + "\n";
81             msg += "定位精度(米):" + args.Position.Coordinate.Accuracy + "\n\n";
82             await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
83             {
84               txtMsg.Text += msg;
85             });
86         }
87
88         private void btnStopTracking_Click(object sender, RoutedEventArgs e)
89         {
90             btnStartTracking.IsEnabled = true;
91             btnStopTracking.IsEnabled = false;
92             geo.StatusChanged -= geo_StatusChanged;
93             geo.PositionChanged -= geo_PositionChanged;
94         }
95   }
96 }
  为了方便测试位置移动的效果,这里用模拟器测试,截图如下:
页: [1]
查看完整版本: Windows 8 地理位置定位 3.位置变化跟踪