我想实现一个将设备位置持续发送到网络服务的应用程序。查看文档,我发现了 Geolocation 类和一些讨论位置跟踪的文章:
- 如何持续跟踪 Windows Phone 8 手机的位置
- 如何在 Windows Phone 8 后台运行位置跟踪应用程序
在实现这些文章中讨论的两个示例项目时,我注意到 geolocator_PositionChanged() 事件不会在每次位置更新时触发。事件的两次执行之间存在延迟(大约 10/15 分钟)。奇怪的是,即使应用程序在前台执行(不仅仅是在后台),也会发生这种情况。我正在使用 Windows Phone 模拟器。
在我的应用程序中,我有一个地图控件,需要在其中显示用户的位置,因此,我需要为每个位置更新正确触发地理定位器 Position Change() 事件,而不会出现延迟。
1)如何使用 Geolocator 类跟踪(无延迟)设备的位置?
通过网络搜索,我发现了 GeoCooperativeWatcher 类,它提供设备的连续位置跟踪。这是代码:
public MainPage()
{
InitializeComponent();
this.GetCoordinate();
}
private void GetCoordinate()
{
var watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High)
{
MovementThreshold = 1
};
watcher.PositionChanged += this.watcher_PositionChanged;
watcher.Start();
}
private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
//Get position data
var pos = e.Position.Location;
//Update mypos object
mypos.update(pos.Latitude, pos.Longitude);
//Update data on the main interface
MainMap.SetView(mypos.getCoordinate(), MainMap.ZoomLevel, MapAnimationKind.Parabolic);
}
它有效:watcher_PositionChanged() 事件立即触发。
2)为什么GeoCoordinateWatcher没有延迟? GeoCoordinateWatcher 类和 Geolocator 类有什么区别?
最后,即使设备未处于活动状态,应用程序也应将设备的位置发送到网络服务。所以,我需要一个后台任务。按照提议here通过 Romasz,我可以使用 Geolocator 类,但有一些限制。
3) 我可以在后台使用GeoCooperativeWatcher吗?如果是,怎么办?
我的目标是实现一个没有延迟的位置跟踪应用程序,甚至可以在后台运行。做这个的最好方式是什么?应用程序应跟踪设备的位置并持续更新网络服务(即使在后台)。我怎样才能做到这一点?最好的方法是什么?我了解 Windows Phone 应用程序生命周期,并且可以接受后台执行的一些限制。背景限制是什么?