Skip to main content
.NET Framework Class Library
GeoCoordinateWatcher Class

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

Inheritance Hierarchy
SystemObject
  System.Device.LocationGeoCoordinateWatcher

Namespace:   System.Device.Location
Assembly:  System.Device (in System.Device.dll)
Syntax
Public Class GeoCoordinateWatcher _
	Implements [%$TOPIC/ee808838_en-us_VS_110_2_0_0_0_0%], [%$TOPIC/ee808838_en-us_VS_110_2_0_0_0_1%], [%$TOPIC/ee808838_en-us_VS_110_2_0_0_0_2%](Of [%$TOPIC/ee808838_en-us_VS_110_2_0_0_0_3%])
public class GeoCoordinateWatcher : [%$TOPIC/ee808838_en-us_VS_110_2_0_1_0_0%], 
	[%$TOPIC/ee808838_en-us_VS_110_2_0_1_0_1%], [%$TOPIC/ee808838_en-us_VS_110_2_0_1_0_2%]<[%$TOPIC/ee808838_en-us_VS_110_2_0_1_0_3%]>
public ref class GeoCoordinateWatcher : [%$TOPIC/ee808838_en-us_VS_110_2_0_2_0_0%], 
	[%$TOPIC/ee808838_en-us_VS_110_2_0_2_0_1%], [%$TOPIC/ee808838_en-us_VS_110_2_0_2_0_2%]<[%$TOPIC/ee808838_en-us_VS_110_2_0_2_0_3%]^>
type GeoCoordinateWatcher =  
    class 
        interface [%$TOPIC/ee808838_en-us_VS_110_2_0_3_0_0%] 
        interface [%$TOPIC/ee808838_en-us_VS_110_2_0_3_0_1%] 
        interface [%$TOPIC/ee808838_en-us_VS_110_2_0_3_0_2%]<[%$TOPIC/ee808838_en-us_VS_110_2_0_3_0_3%]>
    end

The GeoCoordinateWatcher type exposes the following members.

Constructors
  NameDescription
Public method GeoCoordinateWatcherInitializes a new instance of GeoCoordinateWatcher with default accuracy settings.
Public method GeoCoordinateWatcher(GeoPositionAccuracy)Initializes a new instance of GeoCoordinateWatcher, given an accuracy level.
Top
Properties
  NameDescription
Public property DesiredAccuracyThe requested accuracy level for the location data that is provided by the GeoCoordinateWatcher.
Public property MovementThresholdThe distance that must be moved, in meters, relative to the coordinate from the last PositionChanged event, before the location provider raises another PositionChanged event.
Public property PermissionIndicates whether permission to access location data from location providers has been granted or denied.
Public property PositionGets the GeoCoordinate which indicates the current location.
Public property StatusGets the current status of the GeoCoordinateWatcher.
Top
Methods
  NameDescription
Public method DisposeReleases all resources that are used by the current instance of the GeoCoordinateWatcher class.
Protected method Dispose(Boolean)Releases all resources used by the current instance of the GeoCoordinateWatcher class.
Public method Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected method FinalizeFrees resources and performs other cleanup operations before the GeoCoordinateWatcher is reclaimed by garbage collection. (Overrides ObjectFinalize.)
Public method GetHashCodeServes as the default hash function. (Inherited from Object.)
Public method GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected method OnPositionChangedCalled when a PositionChanged event occurs.
Protected method OnPositionStatusChangedCalled when a StatusChanged event occurs.
Protected method OnPropertyChangedCalled when a property of the GeoCoordinateWatcher changes.
Public method StartInitiate the acquisition of data from the current location provider. This method enables PositionChanged events and allows access to the Position property.
Public method Start(Boolean)Initiate the acquisition of data from the current location provider. This method enables PositionChanged events and allows access to the Position property.
Public method StopStops the GeoCoordinateWatcher from providing location data and events.
Public method ToStringReturns a string that represents the current object. (Inherited from Object.)
Public method TryStartInitiates the acquisition of data from the current location provider. This method returns synchronously.
Top
Events
  NameDescription
Public event PositionChangedIndicates that the latitude or longitude of the location data has changed.
Public event StatusChangedIndicates that the status of the GeoCoordinateWatcher object has changed.
Top
Explicit Interface Implementations
  NameDescription
Explicit interface implemetation Private event IGeoPositionWatcherGeoCoordinatePositionChangedIndicates that the location data has changed.
Explicit interface implemetation Private event IGeoPositionWatcherGeoCoordinateStatusChangedIndicates that the status of the location provider has changed.
Explicit interface implemetation Private event INotifyPropertyChangedPropertyChangedIndicates that the Status property, the Position property, or the Permission property has changed.
Top
Remarks

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.

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:

Examples

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.

Imports System.Device.Location
Module GetLocationProperty
    Public Sub GetLocationProperty()
        Dim watcher As New System.Device.Location.GeoCoordinateWatcher()
        watcher.TryStart(False, TimeSpan.FromMilliseconds(1000))

        Dim coord As GeoCoordinate = watcher.Position.Location

        If coord.IsUnknown <> True Then
            Console.WriteLine("Lat: {0}, Long: {1}", coord.Latitude, coord.Longitude)
        Else
            Console.WriteLine("Unknown latitude and longitude.")
        End If 
    End Sub 

    Public Sub Main()
        GetLocationProperty()
        Console.ReadLine()
    End Sub 

End Module
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.

Imports System.Device.Location
Module GetLocationEvent
    Public Class CLocation
        Private WithEvents watcher As GeoCoordinateWatcher
        Public Sub GetLocationEvent()
            watcher = New System.Device.Location.GeoCoordinateWatcher()
            AddHandler watcher.PositionChanged, AddressOf watcher_PositionChanged
            Dim started As Boolean = watcher.TryStart(False, TimeSpan.FromMilliseconds(1000))

            If Not started Then
                Console.WriteLine("GeoCoordinateWatcher timed out on start.")
            End If 
        End Sub 

        Private Sub watcher_PositionChanged(ByVal sender As Object, ByVal e As GeoPositionChangedEventArgs(Of GeoCoordinate))
            PrintPosition(e.Position.Location.Latitude, e.Position.Location.Longitude)
        End Sub 

        Private Sub PrintPosition(ByVal Latitude As Double, ByVal Longitude As Double)
            Console.WriteLine("Latitude: {0}, Longitude {1}", Latitude, Longitude)
        End Sub 
    End Class 


    Public Sub Main()
        Dim myLocation As New CLocation()
        myLocation.GetLocationEvent()
        Console.WriteLine("Enter any key to quit.")
        Console.ReadLine()
    End Sub 

End Module
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);
            }
        }
    }
}

Version Information

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4

.NET Framework Client Profile

Supported in: 4

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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