I’ve been doing Windows Phone development on and off for the past couple of years. In fact, I recently published the first version of my application “Snap Receipt” to the Windows Phone Marketplace. It’s an easy way to get receipts from your phone into Concur’s expense reporting system. You can check it out here. During my experiences with Windows Phone development I’ve identified 10 essential things developers need to know if they’re going to starting writing code against the platform. I thought I’d take some time to share them with you over a series of posts. Please note, these posts refer to Windows Phone 7.0/7.1. I have yet to see any developer bits for Windows Phone 8, but it is my hope that many of the principles I discuss in this series apply to the new platform as well.
In my last post in this series I discussed working with the push notifications. In the last post in this series I’ll look at location services.
Let’s begin with an overview of the location services architecture as implemented on Windows Phone. The location services architecture consists of three layers:
The first layer consists of hardware in the Windows Phone device. This includes the GPS receiver, Wi-Fi, and the cellular radio. All of which function as publishers of location data, each having its own level of accuracy and power consumption.
On top of the hardware sits the native code layer. This layer talks directly with the available sources of location data and decides which sources to use for location based on data availability as well as the performance and accuracy requirements of your application. This layer also talks to a Microsoft-hosted web service to look up location-related information from a database.
It’s important to note that your applications do not directly interact with this native code layer. Instead, they use a managed interface, exposed through an API in the System.Device assembly that ships with the Windows Phone Developer Tools.
It is through this API that your applications start and stop the location service, set the level of accuracy they need, and receive location data as it bubbles up for the hardware to the native code layer then through the managed interface.
Let’s now look at some of the core classes you’ll need to get acquainted with in order to work with location information on windows phone.
When working with the location information, there are three classes you need to be aware of.
The first two reside in the System.Device.Location namespace. The first and most important class to understand is the GeoCoordinate class. This class is constantly used when working with location data. An instance of this class represents a specific geographic location as defined by latitude and longitude.
The GeoCoordinateWatcher class is the class we use in our application to obtain geo coordinates, or location data. The Position property reflects the device’s current location and you can handle the position changed event to perform actions when the device’s location has changed.
After you get your location data you’ll probably want to do something more with it than display coordinates. Chances are you’ll want to visualize it in some way. The last class we’ll talk about, the Map class, enables you to do exactly that. This class resides in the Microsoft.Phone.Controls.Maps namespace which is in the Microsoft.Phone.Controls.Maps assembly and can be leveraged to use Bing maps in your application. This control is nearly identical to the Silverlight version of the Bing maps control. You can draw pushpins and routes on the map, zoom in and out, etc.
When working with location data there are two key considerations you should keep in mind.
- Be very conscientious when deciding on which source to use for your location data. You should take into account the accuracy, power, and speed requirements of your application. If you need a high level of accuracy then you should go with GPS. However, GPS consumes more power than the other sources so make sure you only turn it on when needed. If you’re looking for speed, or a lower level of accuracy will suffice, then cellular is a better option.
- Allow your users to opt-in/out of consuming location data. Given the potential performance impacts of using location services as well exposing their current location, users should be able to turn the feature on or off.
Previous posts in this series
- Windows Phone Essentials #1 – Application Lifecycle
- Windows Phone Essentials #2 – Navigation Model
- Windows Phone Essentials #3 – Application Bar
- Windows Phone Essentials #4 – Isolated Storage
- Windows Phone Essentials #5 – Launchers
- Windows Phone Essentials #6 – Choosers
- Windows Phone Essentials #7 – Accelerometer
- Windows Phone Essentials #8 – Gestures
- Windows Phone Essentials #9 – Location Services