다음을 통해 공유


.NET 4 Location: Let's take a look at status...

In my last post, I showed how you can write a simple .NET 4 Location-enabled console application.This application just output the location of the PC/device when the location changed. Here, we'll see how we can write an application that displays changes in location status rather than location data.

 

Sample Code:

// This namespace is where you'll find the location API in .NET 4

// **Note that you'll have to add a reference to System.Device.dll first

using System.Device.Location;

namespace LocationConsoleApp

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine("Outputting location updates, press any key to exit...");

// The LocationWatcher object will monitor location updates

// and output them to the console

LocationWatcher watcher = new LocationWatcher();

Console.ReadKey();

}

}

class LocationWatcher

{

// Root object for the .NET 4 Location API

// we'll hang on to a reference for the lifetime of

// the LocationWatcher object

private GeoLocationProvider provider;

public LocationWatcher()

{

// Initialize our private member

this.provider = new GeoLocationProvider();

// This event will get raised when the status of location data availability or

// accessibility changes. Here, availability means whether or not data is being sent

// from any of the available location sensors, and accessibility means whether or

// not we have permissions to access the location provider(s)

this.provider.StatusChanged +=

new EventHandler<GeoLocationStatusChangedEventArgs>(provider_StatusChanged);

this.provider.Start();

}

void provider_StatusChanged(object sender, GeoLocationStatusChangedEventArgs e)

{

Console.WriteLine("LocationStatus: " + e.Status.ToString());

}

}

}

 

 

 

Here's the output I saw when I ran the application with a sensor enabled, then disabled and enabled the location sensor:

 

 

Here's an overview of the different GeoLocationStatus enum values:

 

Ready There is a device that is ready to supply location data.
Initializing A device is working to acquire location data (such as when a GPS device is working to acquire a fix).
NoData There are no devices than can currently resolve location.
Disabled The location system feature has been disabled. On Windows 7, this is the case when the Sensors/Location platform has been disabled using group policy.
NoPermissions One or more location devices are available, but the application does not have permission to access any of the devices.

 

So, when we started the application, we had location data available, and the corresponding status was "Ready". When I disabled the location sensor in the "Location and Other Sensors" CPL, our status updated to "NoPermissions". Finally, when I re-enabled the sensor, the sensor went to "Initializing" (default initial state while location data is acquired), then "Ready" again when data was available.

 

There's a simple into to how GeoLocationStatus works in .NET 4.

 

**Note: There will be some changes in this object model coming for RC, so make sure to check back here if you are writing .NET 4 Location apps

 

See ya,

Gavin