2014-06-29 76 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()); 
} 
} 
相關問題