GeoCoordinateWatcher Class

GeoCoordinateWatcher Class

.NET Framework (current version)
 

Supplies location data that is based on latitude and longitude coordinates.

Namespace:   System.Device.Location
Assembly:  System.Device (in System.Device.dll)

System.Object
  System.Device.Location.GeoCoordinateWatcher

[SecurityCriticalAttribute]
public class GeoCoordinateWatcher : IDisposable, INotifyPropertyChanged, 
	IGeoPositionWatcher<GeoCoordinate>

NameDescription
System_CAPS_pubmethodGeoCoordinateWatcher()

Initializes a new instance of GeoCoordinateWatcher with default accuracy settings.

System_CAPS_pubmethodGeoCoordinateWatcher(GeoPositionAccuracy)

Initializes a new instance of GeoCoordinateWatcher, given an accuracy level.

NameDescription
System_CAPS_pubpropertyDesiredAccuracy

The requested accuracy level for the location data that is provided by the GeoCoordinateWatcher.

System_CAPS_pubpropertyMovementThreshold

The distance that must be moved, in meters, relative to the coordinate from the last PositionChanged event, before the location provider raises another PositionChanged event.

System_CAPS_pubpropertyPermission

Indicates whether permission to access location data from location providers has been granted or denied.

System_CAPS_pubpropertyPosition

Gets the GeoCoordinate which indicates the current location.

System_CAPS_pubpropertyStatus

Gets the current status of the GeoCoordinateWatcher.

NameDescription
System_CAPS_pubmethodDispose()

Releases all resources that are used by the current instance of the GeoCoordinateWatcher class.

System_CAPS_protmethodDispose(Boolean)

Releases all resources used by the current instance of the GeoCoordinateWatcher class.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Frees resources and performs other cleanup operations before the GeoCoordinateWatcher is reclaimed by garbage collection.(Overrides Object.Finalize().)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodOnPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate>)

Called when a PositionChanged event occurs.

System_CAPS_protmethodOnPositionStatusChanged(GeoPositionStatusChangedEventArgs)

Called when a StatusChanged event occurs.

System_CAPS_protmethodOnPropertyChanged(String)

Called when a property of the GeoCoordinateWatcher changes.

System_CAPS_pubmethodStart()

Initiate the acquisition of data from the current location provider. This method enables PositionChanged events and allows access to the Position property.

System_CAPS_pubmethodStart(Boolean)

Initiate the acquisition of data from the current location provider. This method enables PositionChanged events and allows access to the Position property.

System_CAPS_pubmethodStop()

Stops the GeoCoordinateWatcher from providing location data and events.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubmethodTryStart(Boolean, TimeSpan)

Initiates the acquisition of data from the current location provider. This method returns synchronously.

NameDescription
System_CAPS_pubeventPositionChanged

Indicates that the latitude or longitude of the location data has changed.

System_CAPS_pubeventStatusChanged

Indicates that the status of the GeoCoordinateWatcher object has changed.

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_priveventINotifyPropertyChanged.PropertyChanged

Indicates that the Status property, the Position property, or the Permission property has changed.

System_CAPS_pubinterfaceSystem_CAPS_priveventIGeoPositionWatcher<GeoCoordinate>.PositionChanged

Indicates that the location data has changed.

System_CAPS_pubinterfaceSystem_CAPS_priveventIGeoPositionWatcher<GeoCoordinate>.StatusChanged

Indicates that the status of the location provider has changed.

The GeoCoordinateWatcher class supplies coordinate-based location data from the current location provider. The current location provider is prioritized as the highest on the computer, based on a number of factors, such as the age and accuracy of the data from all providers, the accuracy requested by location applications, and the power consumption and performance impact associated with the location provider. The current location provider might change over time, for instance, when a GPS device loses its satellite signal indoors and a Wi-Fi triangulation provider becomes the most accurate provider on the computer.

To begin accessing location data, create a GeoCoordinateWatcher and call Start or TryStart to initiate the acquisition of data from the current location provider.

The Status property can be checked to determine if data is available. If data is available, you can get the location one time from the Position property, or receive continuous location updates by handling the PositionChanged event.

The Permission, Status, and Position properties support INotifyPropertyChanged, so that an application can data-bind to these properties.

In Windows 7, all the System.Device.Location classes are fully functional if a location provider is installed and able to resolve the computer's location.

System_CAPS_noteNote

On Windows 7 Starter Edition, the only supported location provider is the Default Location Provider in Control Panel, and an add-in must be installed to specify latitude and longitude.

Note   In versions of Windows prior to Windows 7, the following conditions apply:

The following program shows how to create a GeoCoordinateWatcher and start acquiring data by using an initialization timeout. The code then prints the coordinates of the location, if known.

using System;
using System.Device.Location;

namespace GetLocationProperty
{
    class Program
    {
        static void Main(string[] args)
        {
            GetLocationProperty();
        }

        static void GetLocationProperty()
        {
            GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();

            // Do not suppress prompt, and wait 1000 milliseconds to start.
            watcher.TryStart(false, TimeSpan.FromMilliseconds(1000));

            GeoCoordinate coord = watcher.Position.Location;

            if (coord.IsUnknown != true)
            {
                Console.WriteLine("Lat: {0}, Long: {1}",
                    coord.Latitude,
                    coord.Longitude);
            }
            else
            {
                Console.WriteLine("Unknown latitude and longitude.");
            }
        }
    }
}

The following program shows how to receive continuous location updates by subscribing to PositionChanged events.

using System;
using System.Device.Location;

namespace GetLocationEvent
{
    class Program
    {
        static void Main(string[] args)
        {
            CLocation myLocation = new CLocation();
            myLocation.GetLocationEvent();
            Console.WriteLine("Enter any key to quit.");
            Console.ReadLine();            
        }
        class CLocation
        {
            GeoCoordinateWatcher watcher;

            public void GetLocationEvent()
            {
                this.watcher = new GeoCoordinateWatcher();
                this.watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
                bool started = this.watcher.TryStart(false, TimeSpan.FromMilliseconds(2000));
                if (!started)
                {
                    Console.WriteLine("GeoCoordinateWatcher timed out on start.");
                }
            }

            void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
            {
                PrintPosition(e.Position.Location.Latitude, e.Position.Location.Longitude);
            }

            void PrintPosition(double Latitude, double Longitude)
            {
                Console.WriteLine("Latitude: {0}, Longitude {1}", Latitude, Longitude);
            }
        }
    }
}

.NET Framework
Available since 4.0
Windows Phone Silverlight
Available since 7.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft