Friday, November 30, 2012

Updating your Windows Phone Location code to use WinRT

Updating your Windows Phone Location code to use WinRT:
This blog post was authored by Daniel Estrada Alva, a software development engineer on the Windows Phone team.
- Adam


In this post I describe how you can optionally modify your Windows Phone 7 geolocation code to use the new Windows Runtime (WinRT) Geolocation APIs in Windows Phone 8. Modifying your code to use the new Geolocation APIs in Windows Phone 8 is optional, but offers some solid advantages. In future posts I’ll go into more detail about the extended functionality that the new APIs offer, and how to implement the Windows Runtime. I’ll also walk you through some scenarios that you can build into your Windows Phone 8 apps.
Windows Phone 8 offers a set of APIs in the new namespace Windows.Devices.Geolocation. The following table maps the types of the new Windows Phone 8 APIs to types of the set of APIs provided in the namespace System.Device.Location in Windows Phone 7:






Windows.Devices.Geolocation [Windows Phone 8]


System.Device.Location [Windows Phone 7]

Geolocator GeoCoordinateWatcher
Geoposition GeoPosition
Geocoordinate GeoCoordinate
[Not implemented in Windows Phone 8] CivicAddress


 
Each Windows Phone 8 WinRT API provides the same functionality as the Windows Phone 7 API it replaces. However, Windows Phone 8 APIs also give you new ways to define your positioning requirements. Now you can use fewer requests to accomplish your objective. The following table summarizes the functionality in the Windows Phone 8 namespace Windows.Devices.Geolocation.Geolocator:










Windows.Devices.Geolocation.Geolocator member


Function

DesiredAccuracy Sets the accuracy requirements through an enumeration with common values.
DesiredAccuracyInMeters (NEW) Provides a more granular way to define your accuracy requirements.
MovementThreshold Sets the movement threshold, in meters, at which the app should be notified.
ReportInterval (NEW) Sets the time interval at which the app expects to receive periodic position notifications.
PositionChanged event Provides notifications when a position update is available. Position updates are available depending on the parameters specified for the session.
StatusChanged event Provides notifications when the status of a tracking session changes.
GetGeopositionAsync() (NEW) Asynchronous operation through which an app can obtain a single position update.
GetGeopositionAsync()
parameters:

MaximumAge
Timeout

(NEW) Asynchronous operation through which an app can obtain a single position update. In addition, you can specify how long to wait for the results of the operation (Timeout), and the maximum age of cached position data (MaximumAge).


The following table compares Windows.Devices.Geolocation.Geolocator and System.Device.Location.GeoCoordinateWatcher members:












Windows.Devices.Geolocation.Geolocator member


System.Device.Location.GeoCoordinateWatcher member

DesiredAccuracy DesiredAccuracy
MovementThreshold MovementThreshold
[ NOT NEEDED: You can use GetGeopositionAsync(Age, Timeout) to obtain the same results; see the following example.] Position
LocationStatus Status
PositionChanged PositionChanged
StatusChanged StatusChanged
[NOT NEEDED] Start()
[NOT NEEDED] Stop()
[NOT NEEDED] Dispose()
[NOT NEEDED] TryStart()


 
TryStart() and any other synchronous behaviors have been removed from the new interfaces in favor of the WinRT asynchronous model in Windows Phone 8.
Some tips:

  • The signatures of the PositionChanged and StatusChanged events have changed. The new API passes in the Geolocator object that generated the event as its concrete type instead of as a System.Object as it does in the Windows Phone 7 API.

  • Start/Stop are now implicit to the Add/Remove operations for the PositionChanged and StatusChanged events.

  • The Geolocator object doesn’t implement IClosable (which is the equivalent to IDisposable in the .NET Framework), so it relies mostly on event handlers to be removed when the tracking operation isn’t needed. To stop tracking, you need to unregister the event handlers before the Geolocator object is thrown away. Make sure you don’t forget to do this!

  • Remember that a tracking operation can be expensive, in terms of battery consumption. Use the new asynchronous GetGeopositionAsync method to get a single position update if you only care about detecting the current position (see the GetGeopositionAsync sample below).


 
The following code snippet is an example of how your code may look in Windows Phone 7:



Code Snippet



  1. private GeoCoordinateWatcher geoCoordinateWatcher;

  2.  

  3. public void StartTracking()

  4. {

  5.     if (this.geoCoordinateWatcher != null)

  6.     {

  7.         return;

  8.     }

  9.  

  10.     this.geoCoordinateWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);

  11.     this.geoCoordinateWatcher.MovementThreshold = 100; // in meters

  12.  

  13.     this.geoCoordinateWatcher.PositionChanged += (watcherSender, eventArgs) =>

  14.         {

  15.             // ...

  16.         };

  17.  

  18.  

  19.     geoCoordinateWatcher.Start();

  20. }

  21.  

  22. public void StopTracking()

  23. {

  24.     if (geoCoordinateWatcher == null)

  25.     {

  26.         return;

  27.     }

  28.  

  29.     geoCoordinateWatcher.Stop();

  30.     geoCoordinateWatcher.Dispose();

  31.  

  32.     geoCoordinateWatcher = null;

  33. }





The following is how your code will look with the new APIs. Note that you no longer need to provide the accuracy you require in the constructor. You also can modify the property on the object as long as a tracking operation is not in progress.



Code Snippet



  1. private Geolocator trackingGeolocator;

  2. private TypedEventHandler<Geolocator, PositionChangedEventArgs> positionChangedHandler;

  3.  

  4. public void StartTracking()

  5. {

  6.     if (this.trackingGeolocator!= null)

  7.     {

  8.         return;

  9.     }

  10.  

  11.     this.trackingGeolocator = new Geolocator();

  12.     this.trackingGeolocator.MovementThreshold = 100; // in meters

  13.  

  14.     if (this.positionChangedHandler != null)

  15.     {

  16.         this.positionChangedHandler = (geolocator, eventArgs) =>

  17.             {    

  18.                 // ...

  19.             };

  20.     }

  21.  

  22.     this.trackingGeolocator.PositionChanged += positionChangedHandler;

  23. }

  24.  

  25. public void StopTracking()

  26. {

  27.     if (this.trackingGeolocator == null)

  28.     {

  29.         return;

  30.     }

  31.  

  32.     this.trackingGeolocator.PositionChanged -= this.positionChangedHandler;

  33.     this.trackingGeolocator = null;

  34. }





 
The StatusChanged API hasn’t changed, so I don’t show it here. The list of supported statuses is listed here for reference: http://msdn.microsoft.com/en-US/library/windows/apps/windows.devices.geolocation.geolocator.statuschanged .
Finally, the Geolocator object is a simple way to get a single position update asynchronously. You can also simplify your code with this API if you only need the current position of the device. Here’s an example of how your code may look on Windows Phone 7:



Code Snippet



  1. public GeoCoordinate GetSingleLocation()

  2. {

  3.     GeoCoordinateWatcher geoCoordinateWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);

  4.     EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);

  5.     GeoCoordinate geoCoordinate = null;

  6.  

  7.     geoCoordinateWatcher.PositionChanged += (watcherSender, eventArgs) =>

  8.     {

  9.         geoCoordinate = eventArgs.Position.Location;

  10.         waitHandle.Set();

  11.     };

  12.  

  13.     geoCoordinateWatcher.Start();

  14.     waitHandle.WaitOne();

  15.  

  16.     geoCoordinateWatcher.Stop();

  17.     geoCoordinateWatcher.Dispose();

  18.  

  19.     return geoCoordinate;

  20. }





 
When you use the new GetGeopositionAsync() API, you can leverage the new await operator (http://msdn.microsoft.com/en-us/library/hh156528.aspx) to significantly simplify your code:



Code Snippet



  1. public async Task<Geocoordinate> GetSinglePositionAsync()

  2. {

  3.     Geolocator geolocator = new Geolocator();

  4.     geolocator.DesiredAccuracy = PositionAccuracy.High;

  5.  

  6.     Geoposition geoposition = await geolocator.GetGeopositionAsync();

  7.  

  8.     return geoposition.Coordinate;

  9. }





 
We will be adding follow-up posts that take a deeper look at the new APIs, and how you can use them in advanced scenarios in your Windows Phone 8 apps.



















DIGITAL JUICE

No comments:

Post a Comment

Thank's!