GeoCoordinateWatcher.OnPositionChanged 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在 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