Mobile Corner

Working with Location in Windows Phone

When most people think of location, they assume this information comes from a GPS module. While this is true, Windows Phone also has support for determining location based on cell tower location and WiFi networks.

One important concept that differentiates desktop application and Web site development from building mobile applications is that the user is quite often on the move. All applications should always take into consideration of the context of the current user and personalize the experience accordingly. This is all the more important if the location of the user changes. In this article we'll look at how you can integrate location into your Windows Phone application. We'll be determining the user's current location and then displaying that location on a map using the Bing Map control.

When most people think of location, they immediately assume this information comes from a GPS module on the phone. While this is true, Windows Phone also has support for determining location based on cell tower location and WiFi networks.

Rather than exposing each of these data sources to the application developer and requiring them to combine three, there's a single API through which Windows Phone applications can acquire location information. Depending on which data sources are available and the required level of precision requested by the application, one or more of the data sources will be used in order to provide data to the application as quickly and accurately as possible.

Determining the user's location is relatively simple. However, there are a couple of preliminary steps to complete:

  • Add a reference to System.Device to your Windows Phone application
  • Make sure the ID_CAP_LOCATION capability is in the WPAppManifest.xml file
  • Add a ToggleSwitch to a settings page within your application so the user can disable location use within your application (s2.7.3)
  • Add a privacy policy to your application that outlines how your application is going to use the user's location (s2.7.2)

Note that the last two of these steps are Microsoft application certification requirements defined by the corresponding section number. In addition, there are other requirements around the use of location, such as s6.1 which requires the application to remain responsive even if the user turns location off via System Settings on the device.

Listing 1 creates an instance of the GeoCoordinateWatcher, which I'll use to receive location information. The StartTracking method creates an instance of the GeoCoordinateWatcher, wires up event handlers for both the StatusChanged and PositionChanged event handlers and calls the Start method to initiate location tracking. Conversely, the StopTracking method calls Stop to stop location tracking and unwires the event handlers.

In the event handlers for StatusChanged and PositionChanged, I'm going to extract relevant location service information and update local properties. These can be data-bound to display information to the user. See Listing 2 for the code.

In this case I'm going to be using the Bing Maps control to display the current location of the user. The control is free, but you still need register for use and be assigned a key to use within your application.

You can create a Bing Maps Key for your application by visiting the Bing Maps Portal (follow the link to Create or view keys). This will generate a key you'll need to use within your application. For the purpose of this example, the key is hard coded within the application.

The XAML in Listing 3 lays out a Grid containing two buttons: to start and stop the location tracing, and a map. The map has a border which will be used to determine whether location tracking is running, stopped or disabled (see Listing 2 for the LocationTrackingBrush property settings), and has a Pushpin whose location will be updated to show the current location of the use (also see Listing 2 for the CurrentLocation property setting).

Figure 1 illustrates this application running in both the stopped and running state for the location tracking.


[Click on image for larger view.]
Figure 1. The running application.

In this article you've seen how to use the GeoCoordinateWatcher to retrieve location information within your Windows Phone application. In the next article, we'll continue this example to talk about other aspects of the location service and the Map control.

About the Author

Nick Randolph runs Built to Roam, a consulting company that specializes in training, mentoring and assisting other companies build mobile applications. With a heritage in rich client applications for both the desktop and a variety of mobile platforms, Nick currently presents, writes and educates on the Windows Phone platform.

comments powered by Disqus

Featured

Subscribe on YouTube