2014-06-29 78 views
0

目前我使用此代码来获得GPS:GetGeopositionAsync花费太长时间才能完成

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

@RowlandShaw一百个设备正在使用我的应用程序,获取GPS的这些代码,但其中10%的人警告我,虽然我为它设置了超时时间(30秒),但获取GPS需要太长的时间。 谢谢 –

回答

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()); 
} 
} 
相关问题