private void OnLocatorStatusChanged(Geolocator sender, StatusChangedEventArgs e)
        {
            GeolocationException geolocationException;

            switch (e.Status)
            {
            case PositionStatus.Disabled:
                geolocationException = new GeolocationUnauthorizedException();
                break;

            case PositionStatus.NoData:
                geolocationException = new GeolocationPositionUnavailableException();
                break;

            default:
                return;
            }

            if (this.IsListening)
            {
                this.StopListening();
                this.OnPositionError(new PositionErrorEventArgs(geolocationException));
            }

            this.locator = null;
        }
Exemple #2
0
        private void WatcherOnStatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            this.isEnabled = (this.watcher.Permission == GeoPositionPermission.Granted && this.watcher.Status != GeoPositionStatus.Disabled);

            GeolocationException geolocationException;

            switch (e.Status)
            {
            case GeoPositionStatus.Disabled:
                geolocationException = new GeolocationUnauthorizedException();
                break;

            case GeoPositionStatus.NoData:
                geolocationException = new GeolocationPositionUnavailableException();
                break;

            default:
                return;
            }

            this.StopListening();

            var perror = this.PositionError;

            if (perror != null)
            {
                perror(this, new PositionErrorEventArgs(geolocationException));
            }
        }
        /// <inheritdoc />
        public Task <Position> GetPositionAsync(int timeoutMilliseconds = Timeout.Infinite, CancellationToken?token = null, bool includeHeading = false)
        {
            if (timeoutMilliseconds < 0 && timeoutMilliseconds != Timeout.Infinite)
            {
                throw new ArgumentOutOfRangeException("timeoutMilliseconds");
            }

            if (!token.HasValue)
            {
                token = CancellationToken.None;
            }

            IAsyncOperation <Geoposition> pos = this.GetGeolocator().GetGeopositionAsync(TimeSpan.FromTicks(0), TimeSpan.FromDays(365));

            token.Value.Register(o => ((IAsyncOperation <Geoposition>)o).Cancel(), pos);

            var timer = new Timeout(timeoutMilliseconds, pos.Cancel);

            var tcs = new TaskCompletionSource <Position>();

            pos.Completed = (op, s) =>
            {
                timer.Cancel();

                switch (s)
                {
                case AsyncStatus.Canceled:
                    tcs.SetCanceled();
                    break;

                case AsyncStatus.Completed:
                    tcs.SetResult(GetPosition(op.GetResults()));
                    break;

                case AsyncStatus.Error:
                    Exception ex = op.ErrorCode;
                    if (ex is UnauthorizedAccessException)
                    {
                        ex = new GeolocationUnauthorizedException(ex);
                    }

                    tcs.SetException(ex);
                    break;
                }
            };

            return(tcs.Task);
        }