.NET Location API best practices for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Note

Windows Phone 8 has a new Windows Phone Runtime API for creating location-aware apps. The content in this topic applies to the .NET Location API which was introduced in Windows Phone OS 7.0 and which is still fully supported. For information on which API set is right for your application, see Location for Windows Phone 8.

This topic contains recommendations for creating Windows Phone applications that use the Location Service.

Minimize Power Consumption

When creating a location-aware application, developers must balance the need for the application to have precise location data with the desire to minimize the application’s power consumption. On mobile devices, these two application requirements have an inverse relationship. The hardware that produces less-accurate location information, such as the Wi-Fi and cellular radio, uses less power than the GPS receiver which, in general, can obtain more precise location data. When designing your application, there are two basic rules to follow.

  • Use the lower-accuracy, power-optimized setting for the Location Service unless your application absolutely requires higher accuracy.

  • Turn the Location Service on only when your application needs it, and turn it off when you are done with it.

Choose the Right Level of Accuracy for Location Data

Although the Location Service uses multiple sources of location information, and any of the sources may not be available at any given time (for example, no GPS satellites or cell phone towers may be accessible), the native code layer handles the work of evaluating the available data and choosing the best set of sources. All your application needs to do is to choose between high accuracy or the default, power-optimized setting. You can set this value when you initialize the main Location Service class, GeoCoordinateWatcher.

GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);

Set a Reasonable Movement Threshold

Because GPS hardware in mobile devices does not have an antenna, the sensors are usually designed to be very sensitive. This sensitivity can result in a small amount of noise in the signal from surface reflection and other environmental effects. The main Location Service class, GeoCoordinateWatcher exposes the MovementThreshold property. This property specifies the minimum change in position that must take place before the PositionChanged event is raised. If you set the MovementThreshold property to a very low value, you can cause your application to receive events that are actually the result of noise in the signal. To smooth out the signal to represent only significant changes in position, set the MovementThreshold property to at least 20 meters. This will also result in lower power consumption for your application. Setting the movement threshold to 0 meters will result in the event being raised as frequently as once per second. This setting may be useful for navigation applications.

Plan for Unavailability of Location Data

The Location Service uses multiple sources to determine the location of the device, but there will still be occasions when location data will be unavailable. Your application should handle this lack of data gracefully. The StatusChanged event is raised whenever the status of the Location Service changes. Your application can use the handler for this event to let the user know whether location data is available and to modify the behavior of the application accordingly.

Even when the Location Service is able to obtain location data, initializing and obtaining the first reading typically takes 15 seconds, but can take up to 120 seconds. Your application should be designed with this in mind and should warn the user if the application will be unresponsive while waiting for the service to start. Because the low-accuracy setting does not wait for the GPS to return data, it will typically be quicker if the GPS has not already been activated. If other applications are using the GPS and it is already retrieving data, the high-accuracy setting will return data as fast or faster than the low-accuracy setting.