Windows Phone 8.1 location-tracking
Solution 1
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!
Solution 2
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.
smn.tino
Updated on June 05, 2022Comments
-
smn.tino almost 2 years
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 position-tracking is discussed:
- 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
Implementing both example projects discussed in these articles, I've noticed that the geolocator_PositionChanged() event is not fired at every position update. There is a delay (about 10/15 minutes) between 2 execution of the event. The strange thing is that this happens even when the App executes in foreground (not only in background). I'm using Windows Phone emulator.
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.
1) How can I track (without delays) the device's position using Geolocator class?
Searching over the network, I've found the GeoCoordinateWatcher class, that provides continuosly position-tracking of the device. 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.
2) Why the GeoCoordinateWatcher does not have delays? What is the difference between GeoCoordinateWatcher class and Geolocator class?
Finally, the App should send device's position to the web service even when it is not active. So, I need a background task. As proposed here by Romasz, I can use Geolocator class with some limitations.
3) Can I use GeoCoordinateWhatcher in background? 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). 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. What are the background limits?
-
smn.tino almost 10 yearsThank you Gavin for your answer. Please, can you be more specific? Is there some Microsoft documentation? If this is possible in Windows Phone 8, how to realize it and why remove this feature from Windows Phone 8.1?
-
Gavin almost 10 yearsI did actually search for links to backup my statement when I posted this answer but couldn't find anything concise. I was given this info at a Windows Phone 8.1 Kickstart Event hosted by Hannes Nel in April 2014. He did provide handy slides showing what API's where and weren't available in the various 8 and 8.1 flavours of Windows Phone projects. The reason I remember this fact so well is because I won a Lumia 820 for answering a question asking what was only possible in with Windows Phone 8 API's and not 8.1 API's. I was disappointed as meant I can't upgrade my app to 8.1 because of this.
-
Depechie almost 10 yearsIt's stated here: msdn.microsoft.com/en-us/library/windowsphone/develop/… > Continuous background execution is not supported for Silverlight 8.1 apps
-
Gavin almost 10 yearsI also found the Silverlight 8.1 info, but it doesn't cover the Universal App side of Windows Phone 8.1 so I kept looking.
-
Gavin almost 10 yearsI got in contact with Hannes Nel. He tells me he sourced his slides from this free online course - microsoftvirtualacademy.com/training-courses/… - slide 58 on section "01 | Introducing the Windows Phone 8.1 App Development..." states "Features Only Available on Silverlight Platform" "Continuous background location tracking (SL 8.0 only)". Just to clarify - you can still do background tracking on WP8.1, you just have to create your app as a WP8 project to achieve this (unfortunately!).
-
smn.tino almost 10 yearsI've done some tests today and you're right Gavin: location-tracking is working only on WP8. So, I have to downgrade my application to WP8. Just one question: my WP8 app will be compatible with Windows Phone 8.1? can I install it and use it in WP8.1? I tested continuous tracking on a WP8.1 emulator and it works. Do you know if my WP8 app will work on a real device (with WP8.1)?
-
Gavin almost 10 years@top_broker - Yep, your WP8 app will run on WP8.1 no worries there ;)
-
Rico Suter almost 10 yearsOnly problem is that you are starting with a new app on a dying platform (silverlight vs winrt xaml) and you can only use the new 8.1 APIs through reflection (as silverlight 8.1 apps do not allow background location)
-
Gavin almost 10 years@RicoSuter - Yes, it's a shame that the OS forces you down this route if you require background tracking. Hopefully Microsoft correct this in 8.2, 9 or whatever comes next! I was disappointed to discover I couldn't upgrade my app to an 8.1 solution because of these shortcomings.
-
David Spence over 9 yearsHelp make it happen! Bring it back! wpdev.uservoice.com/forums/253374-missing-platform-apis/…
-
christoph over 8 years@ThilinaAkalanka well the sample code is posted above. What are you missing?
-
Thilina Akalanka over 8 yearsis it possible to send the location to azure mobile service within this background task
-
christoph over 8 years@ThilinaAkalanka In fact I do send my location to an azure backend with the registered background task. It is no mobile service but I see no reason why this should not work as expected.
-
Thilina Akalanka over 8 yearsHow did you do that "send location to azure backend with the registered background task". I started a question here [stackoverflow.com/questions/34831107/…
-
christoph over 8 years@ThilinaAkalanka I had a REST/HTTP backend there and used HttpClient. I never used mobile services. That table based approach you try to use is new to me. I maybe find some answers when I find some time.
-
130nk3r5 almost 8 yearsThanks. Worked like a charm. My updates occurred every two minutes or so...