GetGeopositionAsync does not return

后端 未结 10 688
北恋
北恋 2021-01-08 00:13

In my Windows Phone 8 app, I am trying to use GetGeopositionAsync on the main page to display some items based on user location.

Calling GetGeopositionAsync does not

10条回答
  •  别那么骄傲
    2021-01-08 00:35

    Well it looks like everyone hacked away until it worked... Here's what worked for me:

    /// 
    /// HACK: For some reason Geolocator.GetGeopositionAsync hangs indefinitely.
    /// The workaround is to add a PositionChanged handler.
    /// 
    private Geoposition GetGeoposition()
    {
        var geolocator = new Geolocator();
        var semaphoreHeldUntilPositionReady = new SemaphoreSlim(initialCount: 0);
        Geoposition position = null;
    
        geolocator.ReportInterval = 1000;
        geolocator.PositionChanged += (sender, args) =>
        {
            position = args.Position;
            semaphoreHeldUntilPositionReady.Release();
        };
    
        semaphoreHeldUntilPositionReady.Wait();
        return position;
    }
    

提交回复
热议问题