示例#1
0
        /// <summary>
        /// Requests a single GPS reading by starting the listener and stopping once a reading is received
        /// Requests a single GPS reading - This will start & stop the gps listener if wasn't running already
        /// </summary>
        /// <param name="gpsManager"></param>
        /// <returns></returns>
        public static IObservable <IGpsReading> GetCurrentPosition(this IGpsManager gpsManager) => Observable.FromAsync(async ct =>
        {
            var iStarted = false;
            try
            {
                await currentLocSemaphore
                .WaitAsync(ct)
                .ConfigureAwait(false);

                var task = gpsManager
                           .WhenReading()
                           .Take(1)
                           .ToTask(ct);
                if (!gpsManager.IsListening())
                {
                    iStarted = true;
                    await gpsManager
                    .StartListener(GpsRequest.Foreground)
                    .ConfigureAwait(false);
                }
                var reading = await task.ConfigureAwait(false);

                return(reading);
            }
            finally
            {
                currentLocSemaphore.Release();
                if (iStarted)
                {
                    await gpsManager.StopListener().ConfigureAwait(false);
                }
            }
        });