[英]GetGeopositionAsync takes too long to complete
目前,我使用此代碼獲取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秒時它不會顯示超時異常。 我在這個問題上陷入困境。 有人有什么主意嗎?
確保wifi或手機設備處於打開狀態,這可以在GPS設備找不到信號時使用后備方法。
有或多或少相同問題的人在這里做了另一個話題:
GetGeopositionAsync不返回
有關GeoLocator類的更多信息: http : //msdn.microsoft.com/zh-cn/library/windows/apps/windows.devices.geolocation.geolocator#properties
我也不知道為什么,但是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());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.