简体   繁体   English

Windows Phone 8.1位置跟踪

[英]Windows Phone 8.1 location-tracking

I want to realize an App that continuously send device's position to a web service. 我想实现一个不断向Web服务发送设备位置的应用程序。 Looking in the documentation, I've found Geolocation class and some articles where position-tracking is discussed: 查看文档,我发现了Geolocation类和一些讨论位置跟踪的文章:

Implementing both example projects discussed in these articles, I've noticed that the geolocator_PositionChanged() event is not fired at every position update. 实现这些文章中讨论的两个示例项目,我注意到每个位置更新都不会触发geolocator_PositionChanged()事件。 There is a delay (about 10/15 minutes) between 2 execution of the event. 两次执行事件之间有一段延迟(大约10/15分钟)。 The strange thing is that this happens even when the App executes in foreground (not only in background). 奇怪的是,即使App在前台执行(不仅在后台),也会发生这种情况。 I'm using Windows Phone emulator. 我正在使用Windows Phone模拟器。

In my App I have a map control where I need to show user's position and, so, I need that the geolocator_PositionChanged() event be correctly fired for each position update, without delays. 在我的应用程序中,我有一个地图控件,我需要显示用户的位置,因此,我需要为每个位置更新正确触发geolocator_PositionChanged()事件,没有延迟。

1) How can I track (without delays) the device's position using Geolocator class? 1)如何使用Geolocator类跟踪(没有延迟)设备的位置?

Searching over the network, I've found the GeoCoordinateWatcher class, that provides continuosly position-tracking of the device. 通过网络搜索,我找到了GeoCoordinateWatcher类,它提供了对设备的连续位置跟踪。 This is the code: 这是代码:

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);
}

It works: watcher_PositionChanged() event is fired without delays. 它的工作原理:watcher_PositionChanged()事件被触发而没有延迟。

2) Why the GeoCoordinateWatcher does not have delays? 2)为什么GeoCoordinateWatcher没有延迟? What is the difference between GeoCoordinateWatcher class and Geolocator class? GeoCoordinateWatcher类和Geolocator类之间有什么区别?

Finally, the App should send device's position to the web service even when it is not active. 最后,应用程序应将设备的位置发送到Web服务,即使它未处于活动状态。 So, I need a background task. 所以,我需要一个后台任务。 As proposed here by Romasz, I can use Geolocator class with some limitations. 正如Romasz 在这里提出的,我可以使用Geolocator类,但有一些限制。

3) Can I use GeoCoordinateWhatcher in background? 3)我可以在后台使用GeoCoordinateWhatcher吗? If yes, how? 如果有,怎么样?

My goal is to realize a position-tracking App without delays, that even works in background. 我的目标是实现一个没有延迟的位置跟踪应用程序,甚至可以在后台运行。 What is the best way to do this? 做这个的最好方式是什么? The App should track the device's position and continuously update the web service (even when in background). 应用程序应跟踪设备的位置并不断更新Web服务(即使在后台)。 How can I do this? 我怎样才能做到这一点? What is the best approach? 什么是最好的方法? I know about the Windows Phone Apps lifecycle and I can accept some limitations for the background execution. 我知道Windows Phone Apps生命周期,我可以接受后台执行的一些限制。 What are the background limits? 背景限制是什么?

Unfortunately Windows Phone 8.1 doesn't support continuous tracking in the background. 遗憾的是,Windows Phone 8.1不支持后台连续跟踪。 If you want this feature you'll have to develop a Windows Phone 8 app instead. 如果您需要此功能,则必须开发Windows Phone 8应用程序。 Hopefully they'll fix this for 8.2, 9 or whatever's next! 希望他们能够解决8.2,9或其他任何问题!

There is a way you can achieve a location tracking, but it has its limitations. 有一种方法可以实现位置跟踪,但它有其局限性。 It won´t be enough for a sports app, but for many other use cases it will fit. 对于体育应用程序来说,这还不够,但对于许多其他用例来说,它还是适合的。 Use Geofence and a BackgroundTask with LocationTrigger 使用GeofenceBackgroundTaskLocationTrigger

Here an example: 这是一个例子:

BackgroundAccessStatus backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();

Geolocator locator = new Geolocator();
locator.DesiredAccuracyInMeters = 10;
locator.DesiredAccuracy = PositionAccuracy.High;

Geoposition currentPosition = await locator.GetGeopositionAsync(TimeSpan.FromMinutes(1),TimeSpan.FromSeconds(30));
Geocircle fenceCircle = new Geocircle(currentPosition.Coordinate.Point.Position,25);
Geofence newFence = new Geofence(GEOFENCE_NAME, fenceCircle, MonitoredGeofenceStates.Exited, false, TimeSpan.FromSeconds(1), DateTimeOffset.Now, TimeSpan.FromDays(30));
GeofenceMonitor.Current.Geofences.Add(newFence);

BackgroundTaskBuilder observerTaskBuilder = new BackgroundTaskBuilder();
observerTaskBuilder.Name = OBSERVER_TASK_NAME;
observerTaskBuilder.SetTrigger(new LocationTrigger(LocationTriggerType.Geofence));
observerTaskBuilder.TaskEntryPoint = OBSERVER_TASK_ENTRY_POINT;
observerTaskBuilder.Register();

This will add a geofence circle with the center of your position and a radius of 25 meter. 这将为您的位置中心添加一个地理围栏圆,半径为25米。 When you exit that specified area the background task is triggered. 退出指定区域时,将触发后台任务。 Make sure you update the geofence to your new position and you will be informed when ever the user moves more than 25 meter. 确保将地理围栏更新到新位置,当用户移动超过25米时,您将收到通知。

But keep in mind that the BackgroundTask must not need to run as soon as you leave the bounds of the fence. 但请记住,只要离开围栏的边界, BackgroundTask就不需要运行。 It could have a delay up to a few minutes (I never noticed a delay of more than a minute after I left the circle). 它可能会延迟几分钟(我从未注意到离开圆圈后超过一分钟的延迟)。 As I said: not enough for a sports app but it may suits your needs. 正如我所说:体育应用程序还不够,但它可能适合您的需求。

For more detailed information look here: http://msdn.microsoft.com/en-us/library/windows.devices.geolocation.geofencing.aspx 有关更多详细信息,请访问: http//msdn.microsoft.com/en-us/library/windows.devices.geolocation.geofencing.aspx

For a sample project look here: https://code.msdn.microsoft.com/windowsapps/Geofencing-and-geolocation-d7ea0ef8 有关示例项目,请访问: https//code.msdn.microsoft.com/windowsapps/Geofencing-and-geolocation-d7ea0ef8

Remarks: I read that it is highly recommended to not use a radius smaller than 50. But in my tests 25 worked well, so you better check that yourself as well. 备注:我读到强烈建议不要使用小于50的半径。但在我的测试中,25运行良好,所以你最好自己检查一下。

I did exactly the same as @christoph... I just added the Entered event as well... My updates occurs every 2 minutes, ALWAYS. 我和@christoph完全一样......我刚刚添加了Entered事件......我的更新每2分钟发生一次,总是如此。

    Geoposition currentPosition = await locator.GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(30));
        Geocircle fenceCircle = new Geocircle(currentPosition.Coordinate.Point.Position, 25);
        Geofence newFence = new Geofence("CURRENT_LOC" + Guid.NewGuid(), fenceCircle, MonitoredGeofenceStates.Exited | MonitoredGeofenceStates.Entered, false, TimeSpan.FromSeconds(1), DateTimeOffset.Now, TimeSpan.FromMinutes(10));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM