This article will introduce the Location features in Windows Phone 8 and how the windows phone developers can use the new location features in their app to get the current location of the device.
Introduction
The Windows Phone SDK provides Location and Maps APIs for the developers to develop location aware applications. With the Location API's the developers can utilize the Phone's hardware like GPS, Wifi etc. to identify and track the location of the phone.
Location Provider technologies
The Windows Phone the following Location Provider technologies to track the location
Assisted Global Position System is a satellite based positioning system which provides one of the most accurate results of the location from accessed from the phone. A-GPS consumes more power and hence the developers might need to use it carefully and only when the app needs the high accuracy of location.
The Cell tower technique uses the network provider and the phone’s location is found based on the distance and the ping time between Cell Towers. This technique is less accurate than A-GPS but the accuracy increases as more towers are included for calculation of the location. It consumes less power than A-GPS.
The Wifi consumes less power than A-GPS and can provide better accuracy than cell tower technique in some scenarios. This uses the global location database of Wi-Fi networks and the phone identifying it within its reach.
Getting the Current Location in Windows Phone 7.5
The Windows Phone 7.5 SDK provided the GeoCoordinateWatcher class which can be used to determine the user's current location of the Phone. In the Windows Phone 7.5 Project, you should add the reference to the System.Device.dll. Note that this dll is automatically added in Windows Phone 8 project.
Below is a code snippet on how to use GeoCoordinateWatcher class to find the current location of the phone.
GeoCoordinateWatcher watcher =
new
GeoCoordinateWatcher ();
watcher.PositionChanged += watcher_PositionChanged;
private
void
watcher_PositionChanged(
object
sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
var Latitude = e.Position.Location.Latitude;
var longitude = e.Position.Location.Longitude;
}
Start the GeoCoordinateWatcher
watcher.Start();
Capabilities
The Location capabilities has to be set in the app manifest file for the app to work fine when using the location APIs. If the Location capabilities are not set in manifest file, the following is the behaviour.
1. If the App is targeted at Windows Phone 7.1, then a status error will be received when trying to start the GeoCoordinate Watcher.
2. If the App is targeted at Windows Phone 8, then the app will throw UnauthorizedAccessException.
Setting the location capabilities is pretty easy, you will need to include the ID_CAP_LOCATION in the Capabilities section of your WMAppManifest.xml
<Capabilities>
<Capability Name="ID_CAP_LOCATION" />
...
</Capabilities>
If you are using Microsoft Visual Studio 2012, you can add the capabilities with the GUI editor as shown in the below screenshot.
In Windows Phone 8, the Geolocator class can be used to retrieve the location of the phone. It is the equivalent of the GeoCoordinateWatcher class in Windows Phone 7.5. The GeoCoordinateWatcher is still supported in windows phone 8 but it is recommended to use the new Geolocator class which is part of the Windows Phone Runtime. The Geolocator can be used to either retrieve the location on demand using GetGeopositionAsync and also to continuously get the location by handling the PositionChanged event. Here's the the click event handler of the button which is used to retrieve the current location using Geolocator
async
Button_Click_1(
sender, RoutedEventArgs e)
Geolocator geolocator =
Geolocator();
geolocator.DesiredAccuracy = PositionAccuracy.Default;
Geoposition myLocation = await geolocator.GetGeopositionAsync();
var latitude = myLocation.Coordinate.Latitude;
var longitude = myLocation.Coordinate.Longitude;
The Geolocator class includes different properties like Desired Accuracy, Movement Threshold, DesiredAccuracyInMeters .
Desired Accuracy is a property that allows the developers to specify the desired accuracy of the location. It accepts Enum with the values Default or High. If you see this property to default, then the operating system decides the accuracy level. When you set this to high, the operating system prefers the accuracy of GPS.
The Windows Phone 8 SDK enables the developer to specify the app as location tracking app which runs in the background for just tracking the location. To enable the app to be a location tracking app, you need to add the BackgroundExecution element to the default Task element in the WMAppManifest.xml file as shown below and subscribe to the PositionChanged event of the Geolocator class.
<
DefaultTask
Name
=
"_default"
NavigationPage
"MainPage.xaml"
>
BackgroundExecution
ExecutionType
"LocationTracking"
/>
</
When the app is running in the background, you need to subscribe to the RunningInBackground event if the PhoneApplicationService in the App.xaml file to receive notification on change in the location.