/// <inheritdoc/>
        public Task<Position> GetPositionAsync(int timeoutMilliseconds = Timeout.Infinite, CancellationToken? cancelToken = null, bool includeHeading = false)
		{
            if (timeoutMilliseconds <= 0 && timeoutMilliseconds != Timeout.Infinite)
                throw new ArgumentOutOfRangeException("timeoutMilliseconds", "Timeout must be positive or Timeout.Infinite");

      if (!cancelToken.HasValue)
        cancelToken = CancellationToken.None;

			TaskCompletionSource<Position> tcs;
			if (!IsListening)
			{
				var m = GetManager();

                if (UIDevice.CurrentDevice.CheckSystemVersion (9, 0))
                {
                    m.AllowsBackgroundLocationUpdates = AllowsBackgroundUpdates;
                }

				tcs = new TaskCompletionSource<Position> (m);
                var singleListener = new GeolocationSingleUpdateDelegate(m, DesiredAccuracy, includeHeading, timeoutMilliseconds, cancelToken.Value);
				m.Delegate = singleListener;

				m.StartUpdatingLocation ();
				if (includeHeading && SupportsHeading)
					m.StartUpdatingHeading ();

				return singleListener.Task;
			}
			else
			{
				tcs = new TaskCompletionSource<Position>();
				if (this.position == null)
				{
					EventHandler<PositionErrorEventArgs> gotError = null;
					gotError = (s,e) =>
					{
						tcs.TrySetException (new GeolocationException (e.Error));
						PositionError -= gotError;
					};
					
					PositionError += gotError;
					
					EventHandler<PositionEventArgs> gotPosition = null;
					gotPosition = (s, e) =>
					{
						tcs.TrySetResult (e.Position);
						PositionChanged -= gotPosition;
					};

					PositionChanged += gotPosition;
				}
				else
					tcs.SetResult (this.position);
			}

			return tcs.Task;
		}
Beispiel #2
0
        /// <inheritdoc/>
        public Task <Position> GetPositionAsync(int timeout = Timeout.Infinite, CancellationToken?cancelToken = null, bool includeHeading = false)
        {
            if (timeout <= 0 && timeout != Timeout.Infinite)
            {
                throw new ArgumentOutOfRangeException("timeout", "Timeout must be positive or Timeout.Infinite");
            }

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

            TaskCompletionSource <Position> tcs;

            if (!IsListening)
            {
                var m = GetManager();

                tcs = new TaskCompletionSource <Position> (m);
                var singleListener = new GeolocationSingleUpdateDelegate(m, DesiredAccuracy, includeHeading, timeout, cancelToken.Value);
                m.Delegate = singleListener;

                m.StartUpdatingLocation();
                if (includeHeading && SupportsHeading)
                {
                    m.StartUpdatingHeading();
                }

                return(singleListener.Task);
            }
            else
            {
                tcs = new TaskCompletionSource <Position>();
                if (this.position == null)
                {
                    EventHandler <PositionErrorEventArgs> gotError = null;
                    gotError = (s, e) =>
                    {
                        tcs.TrySetException(new GeolocationException(e.Error));
                        PositionError -= gotError;
                    };

                    PositionError += gotError;

                    EventHandler <PositionEventArgs> gotPosition = null;
                    gotPosition = (s, e) =>
                    {
                        tcs.TrySetResult(e.Position);
                        PositionChanged -= gotPosition;
                    };

                    PositionChanged += gotPosition;
                }
                else
                {
                    tcs.SetResult(this.position);
                }
            }

            return(tcs.Task);
        }