Let’s look at how to initialize the Location Service, recognize changes in the service’s status, get obtain location data.
WARNING: I don’t have anything but the emulator but this code seems to work on it..
Referencing the Location Service
First: add a reference to System.Device.Location.dll to your application references.
Next: add a reference to the Location Service dll
-
Open a new or existing Windows Phone solution
-
From the Project menu, select Add Reference…
-
On the .NET tab, select the component name “System.Device.Location” and click OK.
Next, add a using directive to the top of the .xaml.cs file for the page that is going to use the location service.
using System.Device.Location; Add a variable declaration for the GeoCoordinateWatcher object that you will use to access the Location Service. Give the variable class scope so that it will stay in memory as long as the page is displayed.
public partial class MainPage : PhoneApplicationPage
{
GeoCoordinateWatcher watcher;

Sample
You should really think about how you might use location services in your application. Not just how it works with the user interface and displays on screen but how the user will intereact with it. You need to design your application so that you can enable and disable location-based functionality.
Creating a location-optional application
-
Use the Start method of the GeoCoordinateWatcher object to start the acquisition of data from the Location Service. You can call this as soon as the page is loaded. However, you can allow the user greater control over the power consumption of the application by allowing them to choose when to start the service. For example, you could start the service in the handler for a button click event.
// Click event handler for “Start Location” button
private void StartLocationButton_Click(object sender, RoutedEventArgs e)
{
//The watcher variable was previously declared as type GeoCoordinateWatcher
if (watcher == null)
{
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High); // using high accuracy
watcher.MovementThreshold = 20; // use MovementThreshold to ignore noise in the signal
-
Add event handlers for the StatusChanged and PositionChanged events.
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
}
-
Start the Location Service. This version of the Start method starts acquisition of data from the service asynchronously, so the user can continue to use your application while the application service attempts to start and obtain data.
watcher.Start();
} // end of start button Click handler
-
Implement the StatusChanged event handler. This event is raised whenever the status of the Location Service changes. Because the event handler is called from a different thread than the one in which your page is running, you will need to use the Dispatcher class from the System.Windows.Deployment namespace to invoke a method in your page’s thread. This method, which will be shown in the next step, takes the GeoPositionStatusChangedEventArgs object as its parameter.
void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
Deployment.Current.Dispatcher.BeginInvoke(() => MyStatusChanged(e));
}
-
The GeoPositionStatus enumeration that is passed in the GeoPositionStatusChangedEventArgs object, tells you the current status of the service. You can use it to enable or enable location-based functionality in your application and to communicate the current state of the service to the user. If the status of the service is Disabled, you can check the Permission property to see if the user has disabled Location Service functionality on the device and alert them.
// Custom method called from the StatusChanged event handler
void MyStatusChanged(GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Disabled:
// The location service is disabled or unsupported.
// Check to see if the user has disabled the Location Service
if (watcher.Permission == GeoPositionPermission.Denied)
{
// The user has disabled the Location Service on their device.
StatusTextBlock.Text = "you have disabled the Location Service.";
}
else
{
StatusTextBlock.Text = "location is not functioning currently";
}
break;
case GeoPositionStatus.Initializing:
// The location service is initializing.
// Disable the Start Location button
StartLocationButton.IsEnabled = false;
break;
case GeoPositionStatus.NoData:
// The location service cannot get location data
// Alert the user and enable the Stop Location button
StatusTextBlock.Text = "Location data is not available.";
StopLocationButton.IsEnabled = true;
break;
case GeoPositionStatus.Ready:
// The location service is working and is receiving location data
// Show the current position and enable the Stop button
StatusTextBlock.Text = "Location information is available.";
StopLocationButton.IsEnabled = true;
break;
}
}
-
Once the Location Service is ready and receiving data, it will begin to raise the PositionChanged event and call your application’s handler if you have implemented one. As with the StatusChanged handler implemented in the previous steps, you will need to use the Dispatch object to call a custom method to handle the data.
// Custom method called from the PositionChanged event handler
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
Deployment.Current.Dispatcher.BeginInvoke(() => MyPositionChanged(e));
}
-
Now, access the Position member of the GeoPositionChangedEventArgs object. The Position field is a GeoPosition object, which consists of a Timestamp and a GeoCoordinate object that contains the location information for the reading. This example accesses the latitude and longitude values.
void MyPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate> e)
{
LatitudeTextBlock.Text = e.Position.Location.Latitude.ToString("0.000");
LongitudeTextBlock.Text = e.Position.Location.Longitude.ToString("0.000");
}
-
Make sure that you stop the location service when it is no longer needed, to maximize the battery life of the device. For this example, a “Stop Location” button is implemented to allow the user to stop the Location Service.
// Click event handler for “Start Location” button
private void StopLocationButton_Click(object sender, RoutedEventArgs e)
{
watcher.Stop();
}
Making a One-Time Query
If you only need to access the Location Service to get a single reading, you can ignore the PositionChanged handler and access the location information in the StatusChanged handler.
private void StartLocationButton_Click(object sender, RoutedEventArgs e)
{
//The watcher variable was previously declared as type GeoCoordinateWatcher
if (watcher == null)
{
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High); // using high accuracy
watcher.MovementThreshold = 20; // use MovementThreshold to ignore noise in the signal
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
}
watcher.Start();
}
void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
Dispatcher.BeginInvoke(() => MyStatusChanged(e));
}
void MyStatusChanged(GeoPositionStatusChangedEventArgs e)
{
if (e.Status == GeoPositionStatus.Ready)
//Use the Position property of the GeoCoordinateWatcher object to get the current location
GeoCoordinate co = watcher.Position.Location;
LatitudeTextBlock.Text = e.Position.Location.Latitude.ToString("0.000");
LongitudeTextBlock.Text = e.Position.Location.Longitude.ToString("0.000");
//Stop the Location Service to conserve battery power
watcher.Stop();
}
}