1

我试图在 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());
}
4

1 回答 1

1

它被调用了两次,因为状态从

初始化 -> 准备就绪

初始化它会触发一次并准备好第二次:)

于 2013-01-22T09:21:33.427 回答