简体   繁体   中英

Tracking location continuously windows phone 8

I'm developing an app based in GPS services and i must track the location of the user continuously, like HERE Maps.

geolocator = new Geolocator();
geolocator.DesiredAccuracy = PositionAccuracy.High;
geolocator.MovementThreshold = 20; //Doesn't matter the value I put here, it won't work
geolocator.PositionChanged += geolocator_PositionChanged;

void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
    Dispatcher.BeginInvoke(() =>
    {
        if(args.Position != null)
        {
             myPosition = args.Position.Coordinate.ToGeoCoordinate();
             UpDateData();
        }
     });
}

(I've tried with GeoCoordinateWatcher but i got nothing). These functions work perfectly when i'm standing at the same place or moving very slowly, but if i enter in a car and start to drive the app crashes after few seconds, and I don't know WHY. I've searched a lot of codes with the same finality and all of them don't work. Do you know any other solution for that problem or have already found yourselves in the same position as mine ?

try
    {
        geolocator = new Geolocator();
        geolocator.DesiredAccuracy = PositionAccuracy.High;
        geolocator.ReportInterval = 2000;
        geolocator.PositionChanged += geolocator_PositionChanged;
    }
    catch (UnauthorizedAccessException)
    {
        MessageBox.Show("Location  is Disabled in Phone Settings.");
    }

    private void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
    {
    try
    {
        Dispatcher.BeginInvoke(() =>
        {
            if (args.Position != null && args.Position.Coordinate.ToGeoCoordinate() != myPosition)
            {
                if(args.Position.Coordinate.Accuracy <= 1500)
                {
                    myPosition = args.Position.Coordinate.ToGeoCoordinate();
                    UpDateMyPositionCircle(args.Position.Coordinate.Accuracy);
                }
            }
        });
    } 
    catch (TargetInvocationException tie) 
    {
        if (tie.Data == null) throw;
        else MessageBox.Show("TargetInvocationException while Tracking: " + tie.InnerException.ToString());                    
    }
    catch(SystemException se)
    {
        if (se.Data == null) throw;
        else MessageBox.Show("SystemException while Tracking: " + se.InnerException.ToString());
    }
    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