I want to realize an App that continuously send device\'s position to a web service. Looking in the documentation, I\'ve found Geolocation class and some articles where posi
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
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. 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.
But keep in mind that the BackgroundTask
must not need to run as soon as you leave the bounds of the fence. 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
For a sample project look here: 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.
I did exactly the same as @christoph... I just added the Entered event as well... My updates occurs every 2 minutes, ALWAYS.
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));
Unfortunately Windows Phone 8.1 doesn't support continuous tracking in the background. If you want this feature you'll have to develop a Windows Phone 8 app instead. Hopefully they'll fix this for 8.2, 9 or whatever's next!