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

下列程式顯示如何藉由訂閱PositionChanged事件來接收連續位置更新。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);
            }
        }
    }
}
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建立並Start呼叫TryStart或,以起始從目前位置提供者取得資料。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屬性取得位置一次,或藉由PositionChanged處理事件來接收連續的位置更新。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.

、和屬性支援,INotifyPropertyChanged讓應用程式可以將資料系結至這些屬性。 Position Permission StatusThe 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 Starter Edition 上,唯一支援的位置提供者是 [控制台] 中的預設位置提供者,而且必須安裝增益集才能指定緯度和經度。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()

釋出資源並執行其他清除作業後,記憶體回收才能重新回收 GeoCoordinateWatcherFrees 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 的淺層複本 (Shallow Copy)。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.

適用於