GeoCoordinateWatcher.OnPositionChanged Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Appelée lorsqu'un événement PositionChanged se produit.
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))
Paramètres
Objet GeoPositionChangedEventArgs<T> qui contient le nouvel emplacement.
Exemples
Le programme suivant montre comment s’abonner aux mises à jour continues à partir d’événements 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
S’applique à
Commentaires
https://aka.ms/ContentUserFeedback.
Bientôt disponible : Tout au long de 2024, nous allons supprimer progressivement GitHub Issues comme mécanisme de commentaires pour le contenu et le remplacer par un nouveau système de commentaires. Pour plus d’informations, consultezEnvoyer et afficher des commentaires pour