【发布时间】: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