简体   繁体   中英

GetGeopositionAsync takes too long to complete

current I use this code to get 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;
    }
};

But it always takes too long to complete, as you can see I set timeout 30s but not sure why it doesn't show timeout exception when took more than 30s. I'm getting stuck on this issue. Does anyone have any idea?

Make sure the wifi or cellphone device is on, this enables the fallback methods to be used when the GPS device can't find a signal.

Someone with more or less the same problem made another thread in here:
GetGeopositionAsync does not return

More information on the GeoLocator class: http://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.geolocation.geolocator#properties

I don't know why too, but TimeSpan is realy slow, but doing it with ReportInterval works fine:

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());
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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