1. Understanding Windows Phone Location Services Architecture
A Windows Phone device can
determine its current position on the surface of the earth in one of
three ways. The first approach is to use the built-in GPS receiver,
which uses satellites and is the most accurate, but which also consumes
the most power. The second and third approaches are to use Wi-Fi and the
triangulation of the cell phone towers, which are much less accurate
then GPS receiver but consume less power. Fortunately, the Windows Phone
Location Service automatically decides which option is best for the
location of a device and presents its best guess of longitude and
latitude through the Bing Maps location service. With a longitude and
latitude reading in hand, an application can plot it on a Bing Maps
Silverlight control map. Or you can use a street or civic
address returned by the on-board location service to query the Bing
Maps web service for its corresponding GPS coordinates (longitude and
latitude) and plot them on a Bing Maps map.
2. Introducing the Windows Phone Location Service and Mapping APIs
In order to use the location service on a device, you need to reference the System.Device assembly and declare System.Device.Location
in your code. And before you can take advantage of the location
service, you must enable the location service on the phone by going to
Settings => Location => turn on Location Services option. You can detect whether the phone's location service is enabled using the StatusChanged event of GeoCoordinateWatcher, as seen in the following code.
GeoCoordinateWatcher geoCoordinateWatcher;
geoCoordinateWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
geoCoordinateWatcher.MovementThreshold = 100; // in Meters
geoCoordinateWatcher.StatusChanged += (s, e) =>
{
if (e.Status == GeoPositionStatus.Disabled)
{
MessageBox.Show("Please enable your location service by going to Settings ->
Location -> Turn on Location Services option.");
}
};
Another way to check if the location service is enabled is to use TryStart to see if the GeoCoordinateWatcher can be started.
if (!_geoCoordinateWatcher.TryStart(true, TimeSpan.FromSeconds(5)))
{
MessageBox.Show("Please enable Location Service on the Phone.",
"Warning", MessageBoxButton.OK);
}
Next you have to set DesiredAccuracy and provide MovementThreshold in GeoCoordinateWatcher as seen in the foregoing code.
GeoPositionAccuracy.Default
will use Wi-Fi or the cell phone towers and depends on the availability
of the sources. Windows Phone will automatically choose one to use, and
GeoPositionAccuracy.High will use the GPS receiver built into the phone device. MovementThreshold is a very important property to set because MovementThreshold specifies the change in distance in meters before the PositionChanged event notifies the application that new coordinates are available; the lower the value of MovementThreshold,
the more accurately the position will be tracked, but you will pay a
price in higher power consumption. Microsoft recommends that you set MovementThreshold to at least 20 meters to filter out this noise.
In the following sections, you
will learn how to use the Windows Phone Location Service by simulating
the behavior of the GPS receiver.