示例#1
0
        public Task <Position> GetPositionAsync(int timeout, CancellationToken cancelToken, bool includeHeading)
        {
            if (timeout <= 0 && timeout != Timeout.Infinite)
            {
                throw new ArgumentOutOfRangeException("timeout", "timeout must be greater than or equal to 0");
            }

            var tcs = new TaskCompletionSource <Position>();

            if (!IsListening)
            {
                GeolocationSingleListener singleListener = null;
                singleListener = new GeolocationSingleListener((float)DesiredAccuracy, timeout,
                                                               providers.Where(manager.IsProviderEnabled), () =>
                {
                    for (int i = 0; i < providers.Length; ++i)
                    {
                        manager.RemoveUpdates(singleListener);
                    }
                });

                if (cancelToken != CancellationToken.None)
                {
                    cancelToken.Register(() =>
                    {
                        singleListener.Cancel();

                        for (int i = 0; i < providers.Length; ++i)
                        {
                            manager.RemoveUpdates(singleListener);
                        }
                    }, true);
                }

                try
                {
                    Looper looper = Looper.MyLooper() ?? Looper.MainLooper;

                    int enabled = 0;
                    for (int i = 0; i < providers.Length; ++i)
                    {
                        if (manager.IsProviderEnabled(providers[i]))
                        {
                            enabled++;
                        }

                        manager.RequestLocationUpdates(providers[i], 0, 0, singleListener, looper);
                    }

                    if (enabled == 0)
                    {
                        for (int i = 0; i < providers.Length; ++i)
                        {
                            manager.RemoveUpdates(singleListener);
                        }

                        tcs.SetException(new GeolocationException(GeolocationError.PositionUnavailable));
                        return(tcs.Task);
                    }
                }
                catch (SecurityException ex)
                {
                    tcs.SetException(new GeolocationException(GeolocationError.Unauthorized, ex));
                    return(tcs.Task);
                }

                return(singleListener.Task);
            }

            // If we're already listening, just use the current listener
            lock (positionSync)
            {
                if (lastPosition == null)
                {
                    if (cancelToken != CancellationToken.None)
                    {
                        cancelToken.Register(() => tcs.TrySetCanceled());
                    }

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

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

            return(tcs.Task);
        }
示例#2
0
		public Task<Position> GetPositionAsync(int timeout, CancellationToken cancelToken, bool includeHeading)
		{
			if (timeout <= 0 && timeout != Timeout.Infinite)
				throw new ArgumentOutOfRangeException("timeout", "timeout must be greater than or equal to 0");

			var tcs = new TaskCompletionSource<Position>();

			if (!IsListening)
			{
				GeolocationSingleListener singleListener = null;
				singleListener = new GeolocationSingleListener((float) DesiredAccuracy, timeout,
					providers.Where(manager.IsProviderEnabled), () =>
					{
						for (int i = 0; i < providers.Length; ++i)
							manager.RemoveUpdates(singleListener);
					});

				if (cancelToken != CancellationToken.None)
				{
					cancelToken.Register(() =>
					{
						singleListener.Cancel();

						for (int i = 0; i < providers.Length; ++i)
							manager.RemoveUpdates(singleListener);
					}, true);
				}

				try
				{
					Looper looper = Looper.MyLooper() ?? Looper.MainLooper;

					int enabled = 0;
					for (int i = 0; i < providers.Length; ++i)
					{
						if (manager.IsProviderEnabled(providers[i]))
							enabled++;

						manager.RequestLocationUpdates(providers[i], 0, 0, singleListener, looper);
					}

					if (enabled == 0)
					{
						for (int i = 0; i < providers.Length; ++i)
							manager.RemoveUpdates(singleListener);

						tcs.SetException(new GeolocationException(GeolocationError.PositionUnavailable));
						return tcs.Task;
					}
				}
				catch (SecurityException ex)
				{
					tcs.SetException(new GeolocationException(GeolocationError.Unauthorized, ex));
					return tcs.Task;
				}

				return singleListener.Task;
			}

			// If we're already listening, just use the current listener
			lock (positionSync)
			{
				if (lastPosition == null)
				{
					if (cancelToken != CancellationToken.None)
					{
						cancelToken.Register(() => tcs.TrySetCanceled());
					}

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

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

			return tcs.Task;
		}