【问题标题】:C# GeoLocation Watcher taking too long to get coordinatesC# GeoLocation Watcher 获取坐标的时间太长
【发布时间】:2020-03-09 08:36:53
【问题描述】:

我为我们的作品 Windows 平板电脑(Windows 10)创建了一个 Windows 窗体应用程序来跟踪位置。

我创建的类如下:

class LocationServices
{
    private GeoCoordinateWatcher myWatcher;
    private bool fgWatcherStarted = false;

    public LocationServices()
    {

        myWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
        fgWatcherStarted = myWatcher.TryStart(true, System.TimeSpan.FromMilliseconds(1000));
    }

    public LatLon GetDeviceLocation()
    {
        LatLon myReturn = new LatLon();
        System.Device.Location.GeoCoordinate myPosition = new System.Device.Location.GeoCoordinate();

        try
        {
            if (!fgWatcherStarted)
            {
                fgWatcherStarted = myWatcher.TryStart(true, System.TimeSpan.FromMilliseconds(1000));
            }

            myPosition = myWatcher.Position.Location;

            if (myPosition.IsUnknown)
            {
                myReturn.Latitude = 0;
                myReturn.Longitude = 0;
                myReturn.strMessage = "Unknown Position";
            }
            else
            {
                myReturn.Latitude = myPosition.Latitude;
                myReturn.Longitude = myPosition.Longitude;
                myReturn.strMessage = myPosition.Course.ToString();
            }

        }
        catch (Exception ex)
        {
            myReturn.Latitude = 0;
            myReturn.Longitude = 0;
            myReturn.strMessage = ex.Message.ToString();
        }

        return myReturn;
    }
}

在我的代码中,我每隔几秒钟就会运行一次,并通过调用上面的类来获取位置。

如果我连接到 wifi(从我的办公桌上运行),它会立即获取位置并按预期工作,但从设备上它会在很长一段时间内返回 0,然后突然开始工作并且工作正常,没有任何问题.

我有什么办法可以让这个开始更快吗?我最初认为它可能是位置/信号,但我尝试在同一个地方加载并离开它,一旦它初始化它就可以完美地工作,但它是需要很长时间才能加载的初始坐标。

【问题讨论】:

  • 您可能开启了代理但没有代理。因此,在获得结果之前,您可能要等待长达 30 秒的代理超时。

标签: c# winforms geolocation


【解决方案1】:

看起来好像需要更改位置才能返回值,请在此处查看答案:https://stackoverflow.com/a/52910209/6639187

【讨论】:

    【解决方案2】:

    不太确定您获取坐标的方式,我曾经使用以下方法 - 希望这会有所帮助(在控制台应用程序中运行并检查如下)

    class Program
        {
            private static GeoCoordinateWatcher Watcher;
    
            static void Main(string[] args)
            {
                Watcher = new GeoCoordinateWatcher();
                Watcher.StatusChanged += Watcher_StatusChanged;
                Watcher.Start();
    
                Console.ReadLine();
    
            }
    
            static void Watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
            {
                if (e.Status == GeoPositionStatus.Ready)
                {
                    if (Watcher.Position.Location.IsUnknown)
                    {
                        Console.Write("Cannot find location");
                    }
                    else
                    {
                        Console.WriteLine("Lat: " + Watcher.Position.Location.Latitude.ToString());
                        Console.WriteLine("Lon: " + Watcher.Position.Location.Longitude.ToString());
                    }
                }
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2021-10-13
      • 1970-01-01
      • 2014-08-04
      • 2017-12-20
      • 2013-11-15
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      相关资源
      最近更新 更多