Using Location Service APIs in WP7
This article explains how to use Location Service APIs (GeoCoordinateWatcher) in WP7.
Article Metadata
Tested with
Devices(s): WP7 Emulator
Compatibility
Platform(s): WP7
Article
Keywords: GeoCoordinateWatcher, GeoPositionStatus
Created: vvsnaresh
(10 Nov 2011)
Last edited: hamishwillee
(30 Nov 2012)
Introduction
To use the WP7 Location Service APIs you need to add a reference to System.Device.dll.
The following code snippet shows how to initialize the location service, handle changes in the service’s status and obtain location data on WP7.
public partial class MainPage : PhoneApplicationPage
{
GeoCoordinateWatcher watcher;
// Click the event handler for the “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
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
}
watcher.Start();
} // End of the Start button Click handler.
// Event handler for the GeoCoordinateWatcher.StatusChanged event.
// Implementation of the StatusChanged event handler.
// This event is raised whenever the status of the Location Service changes.
// The GeoPositionStatus enumeration that is passed in the GeoPositionStatusChangedEventArgs object,
// tells you the current status of the service.
void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Disabled:
// The Location Service is disabled or unsupported.
// Check to see whether 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 this application access to location.";
}
else
{
statusTextBlock.Text = "location is not functioning on this device";
}
break;
case GeoPositionStatus.Initializing:
// The Location Service is initializing.
// Disable the Start Location button.
startLocationButton.IsEnabled = false;
break;
case GeoPositionStatus.NoData:
// The Location Service is working, but it 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 Location button.
statusTextBlock.Text = "location data is available.";
stopLocationButton.IsEnabled = true;
break;
}
}
//When 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.
// In the event handler, access the Position member of the GeoPositionChangedEventArgs(Of T) 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 watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
latitudeTextBlock.Text = e.Position.Location.Latitude.ToString("0.000");
longitudeTextBlock.Text = e.Position.Location.Longitude.ToString("0.000");
}
// Click the event handler for the “Start Location” button.
private void stopLocationButton_Click(object sender, RoutedEventArgs e)
{
watcher.Stop();
}


Tbird - stopLocationButton.IsEnabled
Failnote:
'Does not exist in current context'. What I need to do?
Note comes in these lines:
stopLocationButton.IsEnabledTbird 15:21, 10 February 2012 (EET)
Chintandave er - Re: stopLocationButton.IsEnabled - Source code request
@ Tbird Looks like you didnt put the buttons in page and you are trying get the properties in c# code behind page.
Please put those two button on page first.
1) startLocationButton 2) stopLocationButton
@vvsnaresh Hi author, Can you share the full source code that can explain this article ? So other user can use it and understand the article easily.
Thanks.
Chintan Dave.Chintandave er 13:01, 21 February 2012 (EET)
Hamishwillee - Could you extend this a little?
Hi
This is quite useful because MSDN doesn't have all that much on using the location APIs. It would be great if you could add references to guide and reference material you used - I added a link to the watcher already.
Could you extend this by gathering best practice information and add it - for example there are some good questions & answers here: http://social.msdn.microsoft.com/Forums/eu/windowsphone7series/thread/0c6214f5-fa75-41c7-94a7-598646271cdf
The article would then have a much greater scope and value than it has now.
regards
Hamishhamishwillee 06:58, 24 April 2012 (EEST)