Exemple #1
0
        /// <inheritdoc/>
        public Task <bool> StopListeningAsync()
        {
            if (listener == null)
            {
                return(Task.FromResult(true));
            }

            if (listeningProviders == null)
            {
                return(Task.FromResult(true));
            }

            var providers = listeningProviders;

            listener.PositionChanged -= OnListenerPositionChanged;
            listener.PositionError   -= OnListenerPositionError;

            for (var i = 0; i < providers.Count; i++)
            {
                try
                {
                    Manager.RemoveUpdates(listener);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Unable to remove updates: " + ex);
                }
            }

            listener = null;
            return(Task.FromResult(true));
        }
Exemple #2
0
        /// <summary>
        /// Start listening for changes
        /// </summary>
        /// <param name="minimumTime">Time</param>
        /// <param name="minimumDistance">Distance</param>
        /// <param name="includeHeading">Include heading or not</param>
        /// <param name="listenerSettings">Optional settings (iOS only)</param>
        public async Task <bool> StartListeningAsync(TimeSpan minimumTime, double minimumDistance, bool includeHeading = false, ListenerSettings listenerSettings = null)
        {
            var hasPermission = await CheckPermissions();

            if (!hasPermission)
            {
                throw new GeolocationException(GeolocationError.Unauthorized);
            }


            var minTimeMilliseconds = minimumTime.TotalMilliseconds;

            if (minTimeMilliseconds < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(minimumTime));
            }
            if (minimumDistance < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(minimumDistance));
            }
            if (IsListening)
            {
                throw new InvalidOperationException("This Geolocator is already listening");
            }

            var providers = Providers;

            listener = new GeolocationContinuousListener(Manager, minimumTime, providers);
            listener.PositionChanged += OnListenerPositionChanged;
            listener.PositionError   += OnListenerPositionError;

            var looper = Looper.MyLooper() ?? Looper.MainLooper;

            listeningProviders.Clear();
            for (var i = 0; i < providers.Length; ++i)
            {
                var provider = providers[i];

                //we have limited set of providers
                if (ProvidersToUseWhileListening != null &&
                    ProvidersToUseWhileListening.Length > 0)
                {
                    //the provider is not in the list, so don't use it.
                    if (!ProvidersToUseWhileListening.Contains(provider))
                    {
                        continue;
                    }
                }


                listeningProviders.Add(provider);
                Manager.RequestLocationUpdates(provider, (long)minTimeMilliseconds, (float)minimumDistance, listener, looper);
            }
            return(true);
        }
Exemple #3
0
        /// <inheritdoc/>
        public async Task <bool> StartListeningAsync(int minTime, double minDistance, bool includeHeading = false, ListenerSettings settings = null)
        {
            var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permissions.Abstractions.Permission.Location).ConfigureAwait(false);

            if (status != Permissions.Abstractions.PermissionStatus.Granted)
            {
                Console.WriteLine("Currently does not have Location permissions, requesting permissions");

                var request = await CrossPermissions.Current.RequestPermissionsAsync(Permissions.Abstractions.Permission.Location);

                if (request[Permissions.Abstractions.Permission.Location] != Permissions.Abstractions.PermissionStatus.Granted)
                {
                    Console.WriteLine("Location permission denied, can not get positions async.");
                    return(false);
                }
                providers = manager.GetProviders(enabledOnly: false).Where(s => s != LocationManager.PassiveProvider).ToArray();
            }

            if (providers.Length == 0)
            {
                providers = manager.GetProviders(enabledOnly: false).Where(s => s != LocationManager.PassiveProvider).ToArray();
            }

            if (minTime < 0)
            {
                throw new ArgumentOutOfRangeException("minTime");
            }
            if (minDistance < 0)
            {
                throw new ArgumentOutOfRangeException("minDistance");
            }
            if (IsListening)
            {
                throw new InvalidOperationException("This Geolocator is already listening");
            }

            listener = new GeolocationContinuousListener(manager, TimeSpan.FromMilliseconds(minTime), providers);
            listener.PositionChanged += OnListenerPositionChanged;
            listener.PositionError   += OnListenerPositionError;

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

            for (int i = 0; i < providers.Length; ++i)
            {
                manager.RequestLocationUpdates(providers[i], minTime, (float)minDistance, listener, looper);
            }

            return(true);
        }
Exemple #4
0
        /// <inheritdoc/>
        public Task <bool> StopListeningAsync()
        {
            if (listener == null)
            {
                return(Task.FromResult(true));
            }

            listener.PositionChanged -= OnListenerPositionChanged;
            listener.PositionError   -= OnListenerPositionError;

            for (int i = 0; i < Providers.Length; ++i)
            {
                Manager.RemoveUpdates(listener);
            }

            listener = null;
            return(Task.FromResult(true));
        }
        /// <inheritdoc/>
        public async Task <bool> StartListeningAsync(TimeSpan minTime, double minDistance, bool includeHeading = false, ListenerSettings settings = null)
        {
            var hasPermission = await CheckPermissions();

            if (!hasPermission)
            {
                return(false);
            }


            var minTimeMilliseconds = minTime.TotalMilliseconds;

            if (minTimeMilliseconds < 0)
            {
                throw new ArgumentOutOfRangeException("minTime");
            }
            if (minDistance < 0)
            {
                throw new ArgumentOutOfRangeException("minDistance");
            }
            if (IsListening)
            {
                throw new InvalidOperationException("This Geolocator is already listening");
            }

            var providers = Providers;

            listener = new GeolocationContinuousListener(Manager, minTime, providers);
            listener.PositionChanged += OnListenerPositionChanged;
            listener.PositionError   += OnListenerPositionError;

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

            for (int i = 0; i < providers.Length; ++i)
            {
                Manager.RequestLocationUpdates(providers[i], (long)minTimeMilliseconds, (float)minDistance, listener, looper);
            }

            return(true);
        }
        /// <inheritdoc/>
        public async Task<bool> StartListeningAsync(int minTime, double minDistance, bool includeHeading = false, ListenerSettings settings = null)
        {
            var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permissions.Abstractions.Permission.Location).ConfigureAwait(false);
            if (status != Permissions.Abstractions.PermissionStatus.Granted)
            {
                Console.WriteLine("Currently does not have Location permissions, requesting permissions");

                var request = await CrossPermissions.Current.RequestPermissionsAsync(Permissions.Abstractions.Permission.Location);

                if (request[Permissions.Abstractions.Permission.Location] != Permissions.Abstractions.PermissionStatus.Granted)
                {
                    Console.WriteLine("Location permission denied, can not get positions async.");
                    return false;
                }
                providers = manager.GetProviders(enabledOnly: false).Where(s => s != LocationManager.PassiveProvider).ToArray();
            }

            if (providers.Length == 0)
            {
                providers = manager.GetProviders(enabledOnly: false).Where(s => s != LocationManager.PassiveProvider).ToArray();
            }

            if (minTime < 0)
                throw new ArgumentOutOfRangeException("minTime");
            if (minDistance < 0)
                throw new ArgumentOutOfRangeException("minDistance");
            if (IsListening)
                throw new InvalidOperationException("This Geolocator is already listening");

            listener = new GeolocationContinuousListener(manager, TimeSpan.FromMilliseconds(minTime), providers);
            listener.PositionChanged += OnListenerPositionChanged;
            listener.PositionError += OnListenerPositionError;

            Looper looper = Looper.MyLooper() ?? Looper.MainLooper;
            for (int i = 0; i < providers.Length; ++i)
                manager.RequestLocationUpdates(providers[i], minTime, (float)minDistance, listener, looper);

            return true;
        }
        /// <inheritdoc/>
        public void StartListening(int minTime, double minDistance, bool includeHeading = false)
        {
            if (minTime < 0)
                throw new ArgumentOutOfRangeException("minTime");
            if (minDistance < 0)
                throw new ArgumentOutOfRangeException("minDistance");
            if (IsListening)
                throw new InvalidOperationException("This Geolocator is already listening");

            listener = new GeolocationContinuousListener(manager, TimeSpan.FromMilliseconds(minTime), providers);
            listener.PositionChanged += OnListenerPositionChanged;
            listener.PositionError += OnListenerPositionError;

            Looper looper = Looper.MyLooper() ?? Looper.MainLooper;
            for (int i = 0; i < providers.Length; ++i)
                manager.RequestLocationUpdates(providers[i], minTime, (float)minDistance, listener, looper);
        }