GeoCoordinateWatcher 仅在初始时间触发并且不再响应

发布于 2025-01-09 06:42:28 字数 878 浏览 5 评论 0原文

我正在尝试获取设备的纬度/经度。这是我的代码:

private void button5_Click(object sender, EventArgs e)
{
  // Create the watcher.
  Watcher = new GeoCoordinateWatcher();

  // Catch the StatusChanged event.
  Watcher.StatusChanged += Watcher_StatusChanged;

  // Start the watcher.
  Watcher.Start();
}

private void Watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{

  if (e.Status == GeoPositionStatus.Ready)
  {
    // Display the latitude and longitude.
    if (Watcher.Position.Location.IsUnknown)
    {
      MessageBox.Show("Unknown latitude and longitude.");
    }
    else
    {
      GeoCoordinate location = Watcher.Position.Location;
      MessageBox.Show($"Lat: {location.Latitude} Long: {location.Longitude}");
    }
  }
}

当我单击按钮触发事件时,这是我获得纬度/经度读数的唯一时间。该事件不会再次触发,我在其上放置了一个断点,除非再次单击该按钮。我是否误解了这段代码应该如何工作?难道它不应该在纬度/经度发生变化时触发吗?

I'm trying to get the lat/lon of the device. Here is my code:

private void button5_Click(object sender, EventArgs e)
{
  // Create the watcher.
  Watcher = new GeoCoordinateWatcher();

  // Catch the StatusChanged event.
  Watcher.StatusChanged += Watcher_StatusChanged;

  // Start the watcher.
  Watcher.Start();
}

private void Watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{

  if (e.Status == GeoPositionStatus.Ready)
  {
    // Display the latitude and longitude.
    if (Watcher.Position.Location.IsUnknown)
    {
      MessageBox.Show("Unknown latitude and longitude.");
    }
    else
    {
      GeoCoordinate location = Watcher.Position.Location;
      MessageBox.Show(
quot;Lat: {location.Latitude} Long: {location.Longitude}");
    }
  }
}

When I click the button to fire the event is the only time I'm getting a lat/lon reading. The event is not firing again, I put a break point on it, unless I click the button again. Am I misunderstanding how this code should work? Shouldn't it fire off any time that the lat/lon changes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文