Share via


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

Comments

  • Anonymous
    November 22, 2009
    Hey Gavin, You guys are doing a great job. I just wanted to thank you for the great talk at PDC. I got the Freescale board from you on that event and the work you guys have done makes the path to creating feature rich sensor and location aware applications extremely easy. Thanks again!

  • Anonymous
    November 16, 2010
    Hi, I cannot see the GeoLocationProvider. See my screen shot that shows objects in System.Device namespace. How can I make it run? I use .net 4. The link to the image: GeoLocationProvider img253.imageshack.us/.../geolocation1.jpg . Please reposnd me to the email: bioinfguy@gmail.com. Thanks in advance

  • Anonymous
    December 26, 2013
    This would be the way to do using the system.device namespace. msdn.microsoft.com/.../system.device.location.geopositionstatus%28v=vs.110%29.aspx