首页 > 解决方案 > C# GeoLocation Watcher 获取坐标的时间太长

问题描述

我为我们的作品 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,然后突然开始工作并且工作正常,没有任何问题。

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

标签: c#winformsgeolocation

解决方案


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


推荐阅读