我试图在 GPS 的帮助下获取坐标,当我进行试运行或调试时,事件会使 PositionChange 和 StatusChange 调用 TWICE。这是我的代码,请帮助我。
private void button1_Click(object sender, RoutedEventArgs e)
{
if (NetworkInterface.GetIsNetworkAvailable())
{
flag = true;
if (watcher == null)
{
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
watcher.MovementThreshold = 20;
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
watcher.Start();
}
}
}
void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Disabled:
MessageBox.Show("Location Service is not enabled on the device");
break;
case GeoPositionStatus.NoData:
MessageBox.Show(" The Location Service is working, but it cannot get location data.");
break;
}
}
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
if (e.Position.Location.IsUnknown)
{
MessageBox.Show("Please wait while your prosition is determined....");
return;
}
List<string> locationData = new List<string>();
locationData.Add(e.Position.Location.Latitude.ToString("Latitude:" + "0.000"));
locationData.Add(e.Position.Location.Longitude.ToString("Longitude:" + "0.000"));
locationData.Add(e.Position.Location.Altitude.ToString());
locationData.Add(e.Position.Location.Speed.ToString());
locationData.Add(e.Position.Location.Course.ToString());
}