繁体   English   中英

GetGeopositionAsync需要太长时间才能完成

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM