GeoCoordinateWatcher 类

定义

提供基于纬度和经度坐标的位置数据。Supplies location data that is based on latitude and longitude coordinates.

public ref class GeoCoordinateWatcher : IDisposable, System::ComponentModel::INotifyPropertyChanged, System::Device::Location::IGeoPositionWatcher<System::Device::Location::GeoCoordinate ^>
[System.Security.SecurityCritical]
public class GeoCoordinateWatcher : IDisposable, System.ComponentModel.INotifyPropertyChanged, System.Device.Location.IGeoPositionWatcher<System.Device.Location.GeoCoordinate>
[<System.Security.SecurityCritical>]
type GeoCoordinateWatcher = class
    interface IDisposable
    interface INotifyPropertyChanged
    interface IGeoPositionWatcher<GeoCoordinate>
Public Class GeoCoordinateWatcher
Implements IDisposable, IGeoPositionWatcher(Of GeoCoordinate), INotifyPropertyChanged
继承
GeoCoordinateWatcher
属性
实现

示例

下面的程序演示如何创建 GeoCoordinateWatcher 并开始使用初始化超时获取数据。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.");
            }
        }
    }
}
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

以下程序演示如何通过订阅事件来接收连续位置更新 PositionChangedThe 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);
            }
        }
    }
}
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

注解

GeoCoordinateWatcher类提供来自当前位置提供程序的基于坐标的位置数据。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. 当前位置提供程序可能会随时间而变化,例如,当 GPS 设备失去其附属信号,而 Wi-Fi 三角化提供程序成为计算机上最准确的提供程序时。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.

若要开始访问位置数据,请创建 GeoCoordinateWatcher 并调用, StartTryStart 启动从当前位置提供程序获取数据。To begin accessing location data, create a GeoCoordinateWatcher and call Start or TryStart to initiate the acquisition of data from the current location provider.

Status可以通过检查属性来确定数据是否可用。The Status property can be checked to determine if data is available. 如果数据可用,可以从属性获取该位置一次 Position ,或通过处理事件接收连续位置更新 PositionChangedIf data is available, you can get the location one time from the Position property, or receive continuous location updates by handling the PositionChanged event.

PermissionStatusPosition 属性支持 INotifyPropertyChanged ,使应用程序可以将数据绑定到这些属性。The Permission, Status, and Position properties support INotifyPropertyChanged, so that an application can data-bind to these properties.

在 Windows 7 中, System.Device.Location 如果安装了位置提供程序并能够解析计算机的位置,则所有类都能完全正常运行。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.

备注

在 Windows 7 简易版中,唯一支持的位置提供程序是控制面板中的默认位置提供程序,必须安装外接程序以指定纬度和经度。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.

注意 在 Windows 7 之前的 Windows 版本中,以下条件适用:Note In versions of Windows prior to Windows 7, the following conditions apply:

构造函数

GeoCoordinateWatcher()

使用默认精度设置初始化 GeoCoordinateWatcher 类的新实例。Initializes a new instance of GeoCoordinateWatcher with default accuracy settings.

GeoCoordinateWatcher(GeoPositionAccuracy)

使用给定的精度级别初始化 GeoCoordinateWatcher 类的新实例。Initializes a new instance of GeoCoordinateWatcher, given an accuracy level.

属性

DesiredAccuracy

GeoCoordinateWatcher 提供的位置数据所需的精度级别。The requested accuracy level for the location data that is provided by the GeoCoordinateWatcher.

MovementThreshold

相对于最后一个 PositionChanged 事件中的坐标必须移动的距离(以米为单位),移动该距离之后位置提供程序将引发另一个 PositionChanged 事件。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.

Permission

指示访问来自位置提供程序的位置数据的权限是已授予还是已拒绝。Indicates whether permission to access location data from location providers has been granted or denied.

Position

获取指示当前位置的 GeoCoordinateGets the GeoCoordinate which indicates the current location.

Status

获取 GeoCoordinateWatcher 的当前状态。Gets the current status of the GeoCoordinateWatcher.

方法

Dispose()

释放由 GeoCoordinateWatcher 类的当前实例占用的所有资源。Releases all resources that are used by the current instance of the GeoCoordinateWatcher class.

Dispose(Boolean)

释放 GeoCoordinateWatcher 类的当前实例所使用的所有资源。Releases all resources used by the current instance of the GeoCoordinateWatcher class.

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
Finalize()

在通过垃圾回收回收 GeoCoordinateWatcher 之前,释放资源并执行其他清理操作。Frees resources and performs other cleanup operations before the GeoCoordinateWatcher is reclaimed by garbage collection.

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
OnPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate>)

PositionChanged 事件发生时调用。Called when a PositionChanged event occurs.

OnPositionStatusChanged(GeoPositionStatusChangedEventArgs)

StatusChanged 事件发生时调用。Called when a StatusChanged event occurs.

OnPropertyChanged(String)

GeoCoordinateWatcher 的某个属性更改时调用。Called when a property of the GeoCoordinateWatcher changes.

Start()

开始从当前位置提供程序获取数据。Initiate the acquisition of data from the current location provider. 此方法启用 PositionChanged 事件并允许访问 Position 属性。This method enables PositionChanged events and allows access to the Position property.

Start(Boolean)

开始从当前位置提供程序获取数据。Initiate the acquisition of data from the current location provider. 此方法启用 PositionChanged 事件并允许访问 Position 属性。This method enables PositionChanged events and allows access to the Position property.

Stop()

停止 GeoCoordinateWatcher 提供位置数据和事件。Stops the GeoCoordinateWatcher from providing location data and events.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)
TryStart(Boolean, TimeSpan)

开始从当前位置提供程序获取数据。Initiates the acquisition of data from the current location provider. 此方法同步返回。This method returns synchronously.

事件

PositionChanged

指示位置数据的纬度或经度已更改。Indicates that the latitude or longitude of the location data has changed.

StatusChanged

指示 GeoCoordinateWatcher 对象的状态已更改。Indicates that the status of the GeoCoordinateWatcher object has changed.

显式接口实现

IGeoPositionWatcher<GeoCoordinate>.PositionChanged

指示位置数据已更改。Indicates that the location data has changed.

IGeoPositionWatcher<GeoCoordinate>.StatusChanged

指示位置提供程序的状态已更改。Indicates that the status of the location provider has changed.

INotifyPropertyChanged.PropertyChanged

指示 Status 属性、Position 属性或 Permission 属性已更改。Indicates that the Status property, the Position property, or the Permission property has changed.

适用于