c# - 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,然后突然开始工作并且工作正常,没有任何问题。
我能做些什么来让这个开始更快吗?我最初认为它可能是位置/信号,但我尝试在同一个地方加载并离开它,一旦它初始化它就可以完美地工作,但它是需要很长时间才能加载的初始坐标。
解决方案
看起来好像需要更改位置才能返回值,请在此处查看答案:https ://stackoverflow.com/a/52910209/6639187
推荐阅读
- php - 如何在 Stripe Oauth 过程中恢复 php 会话
- mysql - 来自工会的玛丽亚更新 - 或建议
- c# - String.用逐字字符串替换
- python - 我如何编程这个骰子滚轮?
- postgresql - 使用 Data Studio 进行 Postgres 数据库身份验证时出错
- css - GtkScrolledWindow - 如何始终显示覆盖滚动条
- azure-service-fabric - 如何在 Linux 上的 Service Fabric 中查看死机或崩溃容器的日志?
- sql - 如何从减号查询中插入带有预选值的表?
- pytest - 如何从 tmpdir_factory 读取文件
- electron - 在不自动启动的情况下安装 Electron 应用程序