Exemplo n.º 1
0
        static Task DoStartTracking(LocationTrackingSettings settings)
        {
            IsTracking = true;

            Locator.ReportInterval    = (uint)settings.ReportInterval.TotalMilliseconds;
            Locator.MovementThreshold = settings.MovementThreshold;
            Locator.PositionChanged  += OnLocatorPositionChanged;
            Locator.StatusChanged    += OnLocatorStatusChanged;

            return(Task.CompletedTask);
        }
Exemplo n.º 2
0
        static async Task DoStartTracking(LocationTrackingSettings settings)
        {
            CurrentTrackingSettings = settings;

            Manager = await CreateManager();

            if (Device.OS.IsAtLeastiOS(9))
            {
                Manager.AllowsBackgroundLocationUpdates = settings.AllowBackgroundUpdates;
            }

            if (Device.OS.IsAtLeastiOS(6))
            {
                Manager.PausesLocationUpdatesAutomatically = settings.AutoPauseWhenSteady;

                switch (settings.Purpose)
                {
                case LocationTrackingPurpose.AutomotiveNavigation:
                    Manager.ActivityType = CLActivityType.AutomotiveNavigation;
                    break;

                case LocationTrackingPurpose.OtherNavigation:
                    Manager.ActivityType = CLActivityType.OtherNavigation;
                    break;

                case LocationTrackingPurpose.Fitness:
                    Manager.ActivityType = CLActivityType.Fitness;
                    break;

                default:
                    Manager.ActivityType = CLActivityType.Other;
                    break;
                }
            }

            if (CanDeferLocationUpdate && settings.DeferLocationUpdates)
            {
                settings.MovementThreshold = (float)CLLocationDistance.FilterNone;
            }

            IsTracking = true;
            Manager.DesiredAccuracy = CLLocation.AccuracyBest;
            Manager.DistanceFilter  = settings.MovementThreshold;

            if (settings.IgnoreSmallChanges)
            {
                Manager.StartMonitoringSignificantLocationChanges();
            }
            else
            {
                Manager.StartUpdatingLocation();
            }
        }
Exemplo n.º 3
0
        static Task DoStartTracking(LocationTrackingSettings settings)
        {
            Listener = new GeoLocationContinuousListener(Manager, settings.ReportInterval, Providers);
            Listener.PositionChanged.Handle(x => OnListenerPositionChanged(x));
            Listener.PositionError.Handle(OnListenerPositionError);

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

            for (var i = 0; i < Providers.Length; ++i)
            {
                Manager.RequestLocationUpdates(Providers[i], (long)settings.ReportInterval.TotalMilliseconds, settings.MovementThreshold, Listener, looper);
            }

            return(Task.CompletedTask);
        }
Exemplo n.º 4
0
        /// <summary>Starts tracking the user's DeviceLocation.</summary>
        /// <param name="silently">If set to true, then the tracking will start if DeviceLocation permission is already granted but there will be no user interaction (this can only be used in combination with OnError.Ignore or Throw). If set to false (default) then if necessary, the user will be prompted for granting permission, and in any case the specified error action will apply when there i any problem (GPS not supported or enabled on the device, permission denied, general error, etc.)</param>
        public static async Task <bool> StartTracking(LocationTrackingSettings settings = null, bool silently = false, OnError errorAction = OnError.Alert)
        {
            if (silently && errorAction != OnError.Ignore && errorAction != OnError.Throw)
            {
                throw new Exception("If you want to track the DeviceLocation silently, ErrorAction must be Ignore or Throw.");
            }

            await AskForPermission();

            if (silently && !await Permission.Location.IsGranted())
            {
                await errorAction.Apply("Permission is not already granted to access the current DeviceLocation.");

                return(false);
            }

            if (!await Permission.Location.IsRequestGranted())
            {
                await errorAction.Apply("Permission was not granted to access your current DeviceLocation.");

                return(false);
            }

            if (IsTracking)
            {
                await StopTracking();
            }

            if (settings == null)
            {
                settings = new LocationTrackingSettings();
            }

            try
            {
                await DoStartTracking(settings);

                return(true);
            }
            catch (Exception ex)
            {
                await errorAction.Apply(ex, "Failed to start tracking your DeviceLocation.");

                return(false);
            }
        }