共用方式為


GeoCoordinateWatcher.OnPositionChanged 方法

定義

PositionChanged 事件發生時呼叫。

protected:
 void OnPositionChanged(System::Device::Location::GeoPositionChangedEventArgs<System::Device::Location::GeoCoordinate ^> ^ e);
protected void OnPositionChanged (System.Device.Location.GeoPositionChangedEventArgs<System.Device.Location.GeoCoordinate> e);
member this.OnPositionChanged : System.Device.Location.GeoPositionChangedEventArgs<System.Device.Location.GeoCoordinate> -> unit
Protected Sub OnPositionChanged (e As GeoPositionChangedEventArgs(Of GeoCoordinate))

參數

範例

下列程式示範如何訂閱來自 PositionChanged 事件的連續更新。

using System;
using System.Device.Location;

namespace GetLocationEvent
{
    class AsyncProgram
    {
        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

適用於