Edit

Share via


GeoCoordinateWatcher.OnPositionChanged Method

Definition

Called when a PositionChanged event occurs.

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

Parameters

e
GeoPositionChangedEventArgs<GeoCoordinate>

A GeoPositionChangedEventArgs<T> object that contains the new location.

Examples

The following program shows how to subscribe to continuous updates from PositionChanged events.

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

Applies to