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

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube