【发布时间】:2014-08-20 07:24:26
【问题描述】:
我想实现一个不断将设备位置发送到网络服务的应用程序。查看文档,我发现 Geolocation 类和一些讨论位置跟踪的文章:
- How to continuously track the phone's location for Windows Phone 8
- How to run location-tracking apps in the background for Windows Phone 8
实现这些文章中讨论的两个示例项目,我注意到 geolocator_PositionChanged() 事件不会在每次位置更新时触发。两次执行事件之间存在延迟(约 10/15 分钟)。奇怪的是,即使应用程序在前台执行(不仅在后台),也会发生这种情况。我正在使用 Windows Phone 模拟器。
在我的应用程序中,我有一个地图控件,我需要在其中显示用户的位置,因此,我需要为每次位置更新正确触发 geolocator_PositionChanged() 事件,没有延迟。
1) 如何使用 Geolocator 类跟踪(无延迟)设备的位置?
通过网络搜索,我发现了 GeoCoordinateWatcher 类,它提供了设备的连续位置跟踪。这是代码:
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 类有什么区别?
最后,应用程序应该将设备的位置发送到网络服务,即使它不活动。所以,我需要一个后台任务。正如 Romasz 建议的 here,我可以使用 Geolocator 类,但有一些限制。
3) 我可以在后台使用 GeoCoordinateWhatcher 吗?如果是,怎么做?
我的目标是实现一个无延迟的位置跟踪应用程序,甚至可以在后台运行。做这个的最好方式是什么?应用程序应跟踪设备的位置并不断更新网络服务(即使在后台)。我怎样才能做到这一点?最好的方法是什么?我了解 Windows Phone 应用程序的生命周期,并且我可以接受后台执行的一些限制。背景限制是多少?
【问题讨论】:
标签: c# windows-phone-8 windows-runtime geolocation windows-phone-8.1