One of my favourite features of Windows 7 is the new Sensor and Location API. So far I have been able to utilise commercial sensor boards and my own home brew Basic Stamp facilitated sensors. the one thing I haven’t been able to play with is the Location API. This is due to my aging GPS devices not having a Windows 7 driver.
However, revisiting the topic today I have discovered a generic COM port redirection driver for GPS devices connected directly or via BlueTooth. This Windows 7 sensor driver has enabled me to use my BlueTooth HOLUX GPSlim240 with the demonstration applications from Gavin Gear’s blog, to make my Windows 7 laptop location aware.
You need Visual Studio VS2010 beta 2 or better to use the code. Note, changes are expected in the Location API in .Net 4.0 RC expected soon.
Here is the task list:
- Connect up your GPS device to your Windows 7 machine. I used a BlueTooth connection which came in on COM40. From reading other posts I tried 4800 baud as the data rate to the device and this is working great.
- Test your GPS connection. I used the TurboGPS application to do this, using the settings in 1. All worked fine – I’d forgotten just how fast the Holux GPS I have is in starting up. Awesome!
- Install the generic GPS com port redirection driver. You will get an unknown publisher error because the code is signed by the developers own cert. When installing you need to supply the correct com port and baud rate – hence the stage 2 test step!
- Windows 7 then asks permission to enable the location device. If you don’t approve it, you can't use it!
- Ok, so now our hardware is connected up lets look at the first simple application. This is from Gavin’s blog, paste the code into a new VS2010 C# console application project:
using System;
// 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 LocationAPI
{
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();
// Subscribe to updates when our location changes
this.provider.LocationChanged += new
System.EventHandler<GeoLocationChangedEventArgs>(provider_LocationChanged);
// When we call Start(), two things happen
// 1. If we don't have permissions to access the location sensor(s),
// the user will be presented with a dialog where they can grant
// permissions
// 2. If we have permissions, following this call, we'll be able to
// access location data, and we'll get LocationChanged events if
// we have setup an event handler
this.provider.Start();
}
void provider_LocationChanged(object sender, GeoLocationChangedEventArgs e)
{
// Output the new location coordinate to the console if present
if (e.Location.Coordinate != GeoCoordinate.Unknown)
{
Console.WriteLine(
"Latitude: " + e.Location.Coordinate.Latitude.ToString() +
" Longitude: " + e.Location.Coordinate.Longitude.ToString());
}
}
}
}
All being well you’ll have an exciting result looking like this:
Note: modified image to protect my security!
But now we have the basics running we can do all sorts of things.
For a start, enabling the built-in Windows 7 Weather Gadget has it instantly configured to match your current location, and giving you accurate weather status.
Yes I can confirm by looking out of my office window - it is snowing.
Enabling the Location API adds another capability to your context-aware applications.