GeoCoordinateWatcher 只偶尔工作一次
GeoCoordinateWatcher only works once in a while
以下代码尝试获取代码所在计算机的位置 运行:
GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
if (watcher.TryStart(false, TimeSpan.FromMilliseconds(3000)))
{
GeoCoordinate coord = watcher.Position.Location;
if (!coord.IsUnknown)
{
Printer.Print(String.Format("Current Lat: {0}, Current Long: {1}", coord.Latitude, coord.Longitude));
}
else // Path taken most often
{
throw new CommandException("Weather data unknown. (Are location services enabled?)");
}
}
else
{
throw new CommandException("Weather data unknown. (Are location services enabled?)");
}
每隔一段时间,打印正确的位置,但大多数时候,注释的else语句是运行。经过多次测试,我意识到它是否有效完全是随机的。我做错了吗?
您遇到问题的原因可能是您正在初始化一个新的定位器,但没有等待状态报告它已准备就绪,然后再检查位置。
bool abort = false;
GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
if (watcher.TryStart(false, TimeSpan.FromMilliseconds(3000)))
{
DateTime start = DateTime.Now;
while(watcher.Status != GeoPositionStatus.Ready && !abort)
{
Thread.Sleep(200);
if(DateTime.Now.Subtract(start).TotalSeconds > 5)
abort = true;
}
GeoCoordinate coord = watcher.Position.Location;
if (!coord.IsUnknown)
{
Printer.Print(String.Format("Current Lat: {0}, Current Long: {1}", coord.Latitude, coord.Longitude));
}
else // Path taken most often
{
throw new CommandException("Weather data unknown. (Are location services enabled?)");
}
}
else
{
throw new CommandException("Weather data unknown. (Are location services enabled?)");
}
基本上这会添加一个检查以查看状态是否就绪并最多等待 5 秒。
或者,观察者通常应在模块级别设置并注册 PositionChanged
事件,以便您仅在位置实际更改时更新打印输出,而不是重复的轮询循环静止时反复移动当前位置。
以下代码尝试获取代码所在计算机的位置 运行:
GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
if (watcher.TryStart(false, TimeSpan.FromMilliseconds(3000)))
{
GeoCoordinate coord = watcher.Position.Location;
if (!coord.IsUnknown)
{
Printer.Print(String.Format("Current Lat: {0}, Current Long: {1}", coord.Latitude, coord.Longitude));
}
else // Path taken most often
{
throw new CommandException("Weather data unknown. (Are location services enabled?)");
}
}
else
{
throw new CommandException("Weather data unknown. (Are location services enabled?)");
}
每隔一段时间,打印正确的位置,但大多数时候,注释的else语句是运行。经过多次测试,我意识到它是否有效完全是随机的。我做错了吗?
您遇到问题的原因可能是您正在初始化一个新的定位器,但没有等待状态报告它已准备就绪,然后再检查位置。
bool abort = false;
GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
if (watcher.TryStart(false, TimeSpan.FromMilliseconds(3000)))
{
DateTime start = DateTime.Now;
while(watcher.Status != GeoPositionStatus.Ready && !abort)
{
Thread.Sleep(200);
if(DateTime.Now.Subtract(start).TotalSeconds > 5)
abort = true;
}
GeoCoordinate coord = watcher.Position.Location;
if (!coord.IsUnknown)
{
Printer.Print(String.Format("Current Lat: {0}, Current Long: {1}", coord.Latitude, coord.Longitude));
}
else // Path taken most often
{
throw new CommandException("Weather data unknown. (Are location services enabled?)");
}
}
else
{
throw new CommandException("Weather data unknown. (Are location services enabled?)");
}
基本上这会添加一个检查以查看状态是否就绪并最多等待 5 秒。
或者,观察者通常应在模块级别设置并注册 PositionChanged
事件,以便您仅在位置实际更改时更新打印输出,而不是重复的轮询循环静止时反复移动当前位置。