Geosense For Windows for devs

logo

Now that Rafael Rivera and Long Zheng have launched Geosense For Windows (or you can see Long’s post on it), I can happily brag how MahTweets has supported this since before they started working on it.

Geosense and MahTweets both use the Windows 7 Location and Sensor Platform – Geosense being the provider, MahTweet being the consumer. We supported it in MahTweets to geotag tweets and Flickr photos, in the hope that one day Microsoft or the hardware partners would be kind enough to consider Australia a real country and finally release some relevant hardware to the platform here. So far that hasn’t happened, which is where Geosense steps in.

Geosense with .NET

Geosense by itself doesn’t really do anything terribly exciting to the end user – it’s what Geosense enables for developers that is awesome. Location based services and games are primarily on phones only as it can be a hassle for the user to manually enter their location all the time – now it’s dead easy to bring those services and games to the desktop through rich clients.

To start with, grab the Sensor and Location .NET Interop Sample Library.

Below is an abridged version of the MahTweets GlobalPosition class file (it does some other stuff for other platforms that don’t have the sensor API’s, but that’s just a matter of people manually setting their location).

using Windows7.Location;
public class Location
{
    public String City { get; set; }
    public String Country { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

public static class GlobalPosition
{
    private static LatLongLocationProvider _provider;
    private static CivicAddressLocationProvider _civicprovider;
    private static bool _loadproviderthrowsexception = false;
    private const uint DEFAULT_REPORT_INTERVAL = 0;

    public static Location GetLocation()
    {
        Location l = new Location();

        try
        {
            if (_loadproviderthrowsexception)
            {
                return null;
            }

            if (_provider == null)
            {
                _provider = new LatLongLocationProvider(DEFAULT_REPORT_INTERVAL);
                _civicprovider = new CivicAddressLocationProvider(DEFAULT_REPORT_INTERVAL);
            }
            var y = _civicprovider.GetReport() as CivicAddressLocationReport;

            l.City = y.City;
            l.Country = y.CountryOrRegion;

            var x = _provider.GetReport() as LatLongLocationReport;
            l.Latitude = x.Latitude;
            l.Longitude = x.Longitude;
            return l;
        }
        catch (Exception ex)
        {
            return null;
        }

    }
}

Easy. The code speaks for itself – create a LocationProvider, query it to get the Latitude/Longitude. The rest is up to you.

Bonus Points

And if you’re a Powershell junkie, the following works too

[Reflection.Assembly]::LoadFile("<absolute location to dll>\Windows7.SensorAndLocation.dll")
$provider = new-object Windows7.Location.LatLongLocationProvider(0)
$position = $provider.GetReport()
$position.Latitude
$position.Longitude

Comments

One Comment

Trackbacks / Pingbacks

Leave a Reply