目前我使用此代码获取GPS:
Geolocator geolocator = new Geolocator();
geolocator.DesiredAccuracy = PositionAccuracy.Default;
geolocator.DesiredAccuracyInMeters = 50;
try
{
Geoposition currentPosition = await geolocator.GetGeopositionAsync(TimeSpan.FromSeconds(120), TimeSpan.FromSeconds(30));
MyCoordinate = new GeoCoordinate(currentPosition.Coordinate.Latitude, currentPosition.Coordinate.Longitude);
}
catch (Exception ex)
{
if (ex.Message.Contains("This operation returned because the timeout period expired."))
{
MessageBox.Show("GPS is taking too long too complete. Pleaes try again.");
this.SetProgressIndicator(false);
RadBusyIndicator.IsRunning = false;
return;
}
else
{
this.SetProgressIndicator(false);
RadBusyIndicator.IsRunning = false;
return;
}
};
但它总是需要很长时间才能完成,因为你可以看到我设置超时30秒,但不知道为什么 超过30秒时,它不显示超时异常。 我陷入了这个问题。有没有人有任何想法?
答案 0 :(得分:0)
确保wifi或手机设备已开启,这样可以在GPS设备无法找到信号时使用后备方法。
有一个或多或少相同问题的人在这里做了另一个线程:
GetGeopositionAsync does not return
有关GeoLocator类的更多信息: http://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.geolocation.geolocator#properties
答案 1 :(得分:0)
我也不知道为什么,但TimeSpan
真的很慢,但使用ReportInterval
做这件事很有效:
geolocator = new Geolocator();
geolocator.DesiredAccuracy = PositionAccuracy.High;
geolocator.ReportInterval = 2000;
geolocator.PositionChanged += geolocator_PositionChanged;
private void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
try
{
Dispatcher.BeginInvoke(() =>
{
myPosition = args.Position.Coordinate.ToGeoCoordinate();
});
}
catch(Exception ex)
{
if (ex.Data == null) throw;
else MessageBox.Show("Exception while Tracking: " + ex.InnerException.ToString());
}
}