Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Phone 8 - Geolocator PositionChanged listener dies after some time

I'm setting a PositionChanged listener in a Geolocator object with

var geolocator = new Geolocator();
geolocator.PositionChanged += Geolocator_PositionChanged;

It works fine for some time. But after some time without user interaction (+- 4 Hours) it stops receiving position changes, I think because WP8 simply kills it. This is maybe desired from ms, but this is horrible for my app model.

What I've done is to additionally set a PeriodicTask and send the position as well. And this runs with no problems, but if the user change his positions I'm unable to really track it.

The question is: Is there anyway to wake up this Geolocator without the need of user interaction? It can be via the PeriodicTask or even in the PositionChanged.

I've tried already instantiating a new Geolocator already inside the PositionChanged delegate, but this doesn't work.

Any help is very much appreciated. Thank you!

like image 229
José Leal Avatar asked Apr 26 '13 15:04

José Leal


2 Answers

It seems to be some kind of GC that kills / disables the instance.

try to use geolocator.addEventListener("statuschanged", onStatusChanged);

and restart the object when status is Windows.Devices.Geolocation.PositionStatus.disabled or Windows.Devices.Geolocation.PositionStatus.notAvailable.

see Geolocator.StatusChanged | statuschanged event for more details.

Hope I helped

like image 190
Ofir Avatar answered Oct 13 '22 23:10

Ofir


Are you specifying a timeout in your GetGeopositionAsync call? According to the last comment at the bottom of this post, this code fixes an issue where the GetGeopositionAsync call was not returning. It may be related.

public Task GetCurrentPosition()
{

    return Task.Run(() =>
    {
        if (_geolocator == null)
        {
           _geolocator = new Geolocator { DesiredAccuracy = PositionAccuracy.High };
        }

        Geoposition geoposition = null;

        ManualResetEvent syncEvent = new ManualResetEvent(initialState: false);

        IAsyncOperation asyncOp = _geolocator.GetGeopositionAsync();

        asyncOp.Completed += (asyncInfo, asyncStatus) =>
        {
           if (asyncStatus == AsyncStatus.Completed)
           {
               geoposition = asyncInfo.GetResults();
           }

           syncEvent.Set();

        };

        syncEvent.WaitOne();

        return geoposition.Coordinate;

    });

}
like image 21
keyboardP Avatar answered Oct 13 '22 23:10

keyboardP