/// <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 #2
0
        private ListenerSettings GetListenerSettings()
        {
            ListenerSettings settings = null;

            #if __IOS__
            float gpsDeferralDistanceMeters = SensusServiceHelper.Get().GpsDeferralDistanceMeters;
            float gpsDeferralTimeMinutes = SensusServiceHelper.Get().GpsDeferralTimeMinutes;

            settings = new ListenerSettings
            {
                AllowBackgroundUpdates = true,
                PauseLocationUpdatesAutomatically = SensusServiceHelper.Get().GpsPauseLocationUpdatesAutomatically,
                ActivityType = SensusServiceHelper.Get().GpsActivityType,
                ListenForSignificantChanges = SensusServiceHelper.Get().GpsListenForSignificantChanges,
                DeferLocationUpdates = SensusServiceHelper.Get().GpsDeferLocationUpdates,
                DeferralDistanceMeters = gpsDeferralDistanceMeters < 0 ? default(double?) : gpsDeferralDistanceMeters,
                DeferralTime = gpsDeferralTimeMinutes < 0 ? default(TimeSpan?) : TimeSpan.FromMinutes(gpsDeferralTimeMinutes)
            };
            #endif

            return settings;
        }
        /// <inheritdoc/>
		public Task<bool> StartListeningAsync(int minTime, double minDistance, bool includeHeading = false, ListenerSettings settings = null)
        {
            if (minTime < 0)
                throw new ArgumentOutOfRangeException("minTime");
            if (minDistance < 0)
                throw new ArgumentOutOfRangeException("minDistance");
            if (isListening)
                throw new InvalidOperationException("Already listening");

			// if no settings were passed in, instantiate the default settings. need to check this and create default settings since
			// previous calls to StartListeningAsync might have already configured the location manager in a non-default way that the
			// caller of this method might not be expecting. the caller should expect the defaults if they pass no settings.
			if (settings == null)
				settings = new ListenerSettings();

			// keep reference to settings so that we can stop the listener appropriately later
			listenerSettings = settings;

            double desiredAccuracy = DesiredAccuracy;

			#region apply settings to location manager
			// set background flag
			if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
				manager.AllowsBackgroundLocationUpdates = settings.AllowBackgroundUpdates;

			// configure location update pausing
			if (UIDevice.CurrentDevice.CheckSystemVersion(6, 0)) 
			{
				manager.PausesLocationUpdatesAutomatically = settings.PauseLocationUpdatesAutomatically;

				if (settings.ActivityType == ActivityType.AutomotiveNavigation)
					manager.ActivityType = CLActivityType.AutomotiveNavigation;
				else if (settings.ActivityType == ActivityType.Fitness)
					manager.ActivityType = CLActivityType.Fitness;
				else if (settings.ActivityType == ActivityType.Other)
					manager.ActivityType = CLActivityType.Other;
				else if (settings.ActivityType == ActivityType.OtherNavigation)
					manager.ActivityType = CLActivityType.OtherNavigation;
			}

			// to use deferral, CLLocationManager.DistanceFilter must be set to CLLocationDistance.None, and CLLocationManager.DesiredAccuracy must be 
			// either CLLocation.AccuracyBest or CLLocation.AccuracyBestForNavigation. deferral only available on iOS 6.0 and above.
			if (CanDeferLocationUpdate && settings.DeferLocationUpdates)
			{
				minDistance = CLLocationDistance.FilterNone;
				desiredAccuracy = CLLocation.AccuracyBest;
			}
			#endregion

            isListening = true;
            manager.DesiredAccuracy = desiredAccuracy;
            manager.DistanceFilter = minDistance;

            if (settings?.ListenForSignificantChanges ?? false)
                manager.StartMonitoringSignificantLocationChanges();
            else
                manager.StartUpdatingLocation();

            if (includeHeading && CLLocationManager.HeadingAvailable)
                manager.StartUpdatingHeading();

            return Task.FromResult(true);
        }
        /// <inheritdoc/>
        public Task<bool> StopListeningAsync()
        {
            if (!isListening)
                return Task.FromResult(true);

            isListening = false;
            if (CLLocationManager.HeadingAvailable)
                manager.StopUpdatingHeading();

            // it looks like deferred location updates can apply to the standard service or significant change service. disallow deferral in either case.
            if ((listenerSettings?.DeferLocationUpdates ?? false) && CanDeferLocationUpdate)
                manager.DisallowDeferredLocationUpdates();
			
            if (listenerSettings?.ListenForSignificantChanges ?? false)
                manager.StopMonitoringSignificantLocationChanges();
            else
                manager.StopUpdatingLocation();

            listenerSettings = null;
            position = null;

            return Task.FromResult(true);
        }
 /// <summary>
 /// Start listenting
 /// </summary>
 /// <param name="minTime"></param>
 /// <param name="minDistance"></param>
 /// <param name="includeHeading"></param>
 /// <returns></returns>
 public Task<bool> StartListeningAsync(int minTime, double minDistance, bool includeHeading = false, ListenerSettings settings = null)
     => Task.FromResult(false);