Welcome to MSDN Blogs Sign in | Join | Help

Let's write a simple .NET 4 Location-aware application!

So you want to write an application in .NET that’s location-aware? Let’s start with a code sample (console application) that will show you how to use a GeoLocationProvider to respond to location updates, and print those updates out to the console.

 

In order to compile and run this code, you’ll need Visual Studio 2010 installed, and you’ll need to create a new console application project. If you paste this code into your Program.cs, and then add a reference to System.Device.dll, you should be ready to go.

 

Sample Code:

 

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 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();

 

            // 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());

            }

        }

    }

}

 

 

If you have a Windows 7 supported GPS, WAN card, or other provider (such as a software-based lookup provider) attached/installed, you will see output that will look like the following:

 

When we create our LocationWatcher object, it in turn creates an instance of a GeoLocationProvider, subscribes to the LocationChanged event, starts the provider, and prints out location updates to the console from the LocationChanged event handler.

 

This is a simple example of using location data with .NET 4. In future blog posts, we’ll explore other parts of this API, and see what else we can do with it.

 

**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

Posted by GavinGear | 1 Comments

Video of PDC 2009 .NET 4 Location Session

Are you interested to learn more about how location devices and applications work in Windows 7? Do you want to see a demo of Windows 7, Location awareness, the new .NET 4 Location API, and Visual Studio 2010? If so, you can watch the session I presented on these topics on the PDC website:

Link: http://microsoftpdc.com/Sessions/CL17

Screenshot:

Until next time,
Gavin

Posted by GavinGear | 0 Comments

.NET 4 and VS 2010 to support Location API in all SKUs

Last year at PDC 2008, Microsoft first announced the Windows 7 Sensor and Location platform. Last month, we shipped Windows 7 worldwide, and the corresponding Windows 7 Sensor and Location platform. This is great because now sensor and location hardware manufacturers can easily integrate these devices with Windows. Software developers can also leverage our in-box Win32 COM (Sensors and Location) and IDispatch (Location only) APIs to integrate sensor and/or location functionality into their applications.

 

But one question I get frequently from developers is “What about .NET?”. Our answer until now has been to use our .NET interop samples, or to write their own interop code. But now I’m happy to say that .NET 4 and Visual Studio 2010 will support location functionality, which is new for the Beta 2 release.

 

Here are some fundamentals for location in .NET 4:

·         New assembly: System.Device.dll, represents the PC/device

·         New namespace: System.Device.Location, contains classes, data types, etc for location awareness

 

Here is a high-level diagram showing the core object model for this API:

 

... and here’s an introduction to how these objects are used:

GeoLocationProvider:
This is the “root object” for the API. It is used to access location data, status, as well as to get location and status updates. This is a value type, so you start by creating an instance of this type.

 

GeoLocation:
Represents the current location of the device/PC

 

GeoCoordinate:

Represents the position of the PC (latitude, longitude, etc)

 

CivicAddress:
Represents the CivicAddress corresponding to the location of the PC

 

GeoLocationStatus:
Represents the accessibility (whether you have permissions to access location data) and availability (is location data available at all) of the GeoLocationProvider

 

So, if you install .NET 4 / Visual Studio 2010 Beta 2, you can start to play with this new API!

 

I’ll be following up this post with more posts covering different aspects (in more detail) of what you can do with this new API.

 

**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

Posted by GavinGear | 0 Comments

Going to PDC09? Want to learn about developing Location-Aware applications with .NET?

Hey there- If you're interested in building location-aware applications, and if you're going to be at PDC this year, come check out the session I'll be presenting on Location-Awareness in .NET 4.

Here's the information:
http://microsoftpdc.com/Sessions/CL17 

Building Sensor- and Location-Aware Applications with Windows 7 and .NET Framework 4

How many times have you thought to yourself, “My application would be so much better if it knew where the user was?” With Windows 7 and the .NET Framework 4.0, you now have the tools at your fingertips to location-enable your applications. Based on the new Location platform for Windows 7, the location API in .NET Framework 4.0 provides a single, consistent API to get you your latitude and longitude regardless of the underlying technology that acquired it—allowing you to focus on creating exciting, differentiated location-aware applications.

See you there!
Gavin

Posted by GavinGear | 0 Comments

Great article covering Windows 7 Sensor development kit driver install

I just found this article:

http://sondreb.com/blog/post/Windows-7-Sensor-Platform.aspx 

 Where Sondre covers many aspects of Windows 7 Sensor and Location platform, including how to troubleshoot driver installation for the Windows 7 Sensor development kits. One minor clarification: You should run sensordriverkitinstaller.exe *after* you plug in the sensor development kit board.

From his post:

"Part of Windows 7 is a completely new platform for Location and Sensors. This is a framework that tries to remove the old legacy of COM-ports for communication with external devices. Even a lot of today's USB devices, still rely on the COM protocol to communicate with the PC. The Location and Sensor Platform in Windows 7 improves this with a modern architecture for arbitrary hardware sensory input.

One of the most important advances in this platform is the ability for multiple applications to use the same sensors at the same time, this has always been some of a problem with GPS, where only one instance of the application could read the GPS coordinates."

Posted by GavinGear | 0 Comments

Great writeup on car racing game XNA sample and sensor development kit

Hello all- Pietro Brambati just posted a great writeup on how you can install and run the XNA car racing sample application, and drive the car using the accelerometer built into the Windows 7 Sensor Development kit- with step by step instructions!

Check it out!

http://blogs.msdn.com/pietrobr/archive/2009/09/24/having-fun-with-windows-7-sensor-api-xna-game-studio.aspx 

From his post:

Posted by GavinGear | 0 Comments

Windows 7, coming soon to the public!

With Windows 7 coming to the public soon, I thought I'd take a moment to write down a quick list of some of my favorite things that are new for Windows 7.

Easy connection to networks
With the new wireless connection interface, all you have to do is click on the wireless icon on the taskbar, and you are presented with a list of availble wireless networks to connect to. One easy interface! Just click on the connect button next to the network, and you're done!

Screenshot:

Quick and easy projection interface
When you need to project, it's likely that you don't have time to goof around and waste time from your presentation trying to get your PC to project. In Windows 7, you can use the (Windows + P) key combo to cycle through projection options, works every time for me!

Screenshot:

The Superbar!
The new superbar (was the taskbar) does a great job of collapsing the running program icons down to a manageable size, and they are grouped by application as well! When you hover over an application icon (takes up less space than old style icons with text) it shows you a preview of instances of the application, and lets you click on one to focus that application. A great time saver and workflow enhancement.

Screenshot:

Updated in-box applications
Quite a few of the in-box applications (calculator, paint, etc) have new interfaces and features that make them look better and also make them easier to use.

Screenshot:

That's a few highlights, but really I'm just scratching the surface here... There are a lot of great new or updated things in Windows 7 including:

  • The Windows 7 Sensor and Location Platform, built-in automatic screen brightness control (Adaptive Brightness) ...I'm  a bit biased here, as this is the feature I worked on :)
  • New Media Center interface/experiences
  • Performance improvements
  • Homegroup (media sharing and much more)
  • ...and many more!

When you can, pick up a copy of Windows 7, I think you'll really like it!

 -Gavin

Posted by GavinGear | 0 Comments

Windows 7 Sensor development kit downloads are live!

If you have a sensor development kit, and either don’t have the required software/firmware, or want to update your board’s firmware (to enter Windows 7 sensor demo mode by default, or to experiment with Adaptive Brightness in Windows 7) the following download will be of interest:

See “Sensor Development Kit for Windows 7” on the following page: Windows Sensors and Location MSDN Code Gallery Downloads

 

If you want to update your firmware, follow the instructions in the root “readme.txt” file.

How to get new Freescale sensor boards to work on Windows 7

If you've purchased one of the new Freescale dev kits online, you may have noticed that the board does not work out of the box on Windows 7. The default mode is for Freescale demos.

There are two options to get this board to work on Windows 7:

  1. Repeatedly tap the E8 capacative touch button while you plug in the board (keep tapping until you see the alternate flashing red LEDs) - see the picture below. This will not persist across reboots unfortunately.
  2. Update the firmware on the board - You can use firmware from one of the sensor development kits (pictured below) or download the package from Microsoft (We dont' have this public yet)

I will post download details and firmware update instructions when the download is posted.

See ya,
Gavin

Windows API Code Pack is live, and with support for Windows 7 Sensors!

As a part of the Windows 7 RC release wave, the Windows SDK team has released the “Windows® API Code Pack for Microsoft® .NET Framework (v0.85)”. What is this code pack you may ask? This .NET source code  supports new Windows 7 light-up developer features including the following:

-          Shell features

-          Direct 3D 11.0

-          Sensor Platform APIs

 

This is a preview release, so it may change between now and RTM, but it does enable you to code against these features with your favorite .NET language for Windows 7. The sensor API code even includes samples (For accelerometer and light sensor) that show off the capabilities of the Windows 7 Sensor Development Kit hardware!

 

Here’s a link to the Code Pack release page:

http://code.msdn.microsoft.com/WindowsAPICodePack

 

Thanks,

Gavin

Windows 7 RC Developer Essentials

Now that Windows 7 RC builds are live, you may want to know “what do I need to download and install to get going with driver or application development on Windows 7”?

 

Well, I’ll do my best to outline that here!

 

Essential: Download and install Windows 7 RC


The public download for Windows 7 RC builds is located here:

http://www.microsoft.com/windows/windows-7/download.aspx

 

For application developers: Download and install the Windows 7 RC SDK


Websetup:

http://www.microsoft.com/downloads/details.aspx?FamilyID=f75f2ca8-c1e4-4801-9281-2f5f28f12dbd

 

ISOs:

http://www.microsoft.com/downloads/details.aspx?FamilyID=6db1f17f-5f1e-4e54-a331-c32285cdde0c

 

 

For driver developers: Download and install the Windows 7 RC WDK


Main page for WDK:

http://www.microsoft.com/whdc/DevTools/WDK/WDKpkg.mspx

Download details and requirements:
Download the WDK for Windows 7 RC: The Windows 7 RC WDK (v.7.0.0) is available through the Windows Ecosystem Readiness Program and through MSDN Subscriber Downloads. It is also available to developers who are participants in the WDK beta program. If you want to participate in the WDK beta program, send an e-mail to wdkbeta@microsoft.com. You will receive an e-mail in return that contains instructions about how to sign up for the appropriate program on Microsoft Connect.

 There you go!

-Gavin

Posted by GavinGear | 1 Comments
Filed under:

Don't have a light sensor? Use VirtualLightSensor.exe from the SDK!

What is the Virtual Light Sensor tool? This tool allows the user to simulate an ALS (Ambient Light Sensor) device. The main purposes for simulating ALS devices would be to either experiment with the new Windows 7 Adaptive Brightness feature, or to test sensor applications that implement light-awareness for readability optimizations, or use ALS in some other way (night/day themes, etc).

Screenshot:

This tool is included in the Windows 7 SDK, so once you install the SDK, you'll have this tool.

Fore more detail and install directions, see:
http://msdn.microsoft.com/en-us/library/dd318911(VS.85).aspx 

Posted by GavinGear | 1 Comments

Windows 7 Sensor development kit boards are available for order!

If you've been wanting to get a Windows 7 Sensor development kit, you can now order directly from freescale here:

http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=JMBADGE&tab=Buy_Parametric_Tab&fromSearch=false

 

This board has the following sensors onboard:

- Ambient light sensor

- 3D accelerometer

- Dual touch strips

 

 

 

More on this kit later!

-Gavin

Want to write a sensor driver? Windows 7 Beta WDK docs are live on MSDN.com!

If you have been wondering about how to support your sensor or location device on Windows 7, wonder no longer! The Windows 7 Sensor WDK documentation is live on MSDN:

http://msdn.microsoft.com/en-us/library/bb870493.aspx 

 

This documention covers driver development concepts including coverage of the two interfaces of interest:

 

ISensorClassExtension (Docs HERE)

This is the interface for the root Co-Creatable object that sensor drivers use for outgoing calls (Initialization, posting events, etc).

 

ISensorDriver (Docs HERE)

This is the interface that sensor drivers implement as a callback to handle incoming calls. Requests for property data and sensor data are serviced on this callback interface.

 

That's enough to get you started!

 

See ya,

Gavin

More Posts Next page »
 
Page view tracker