Beispiel #1
0
        public async Task StartLocationUpdates()
        {
            if (_isRunning)
            {
                return;
            }

            if (await EnsureHasPermissions() is false)
            {
                _loggingService.Trace("Can't Execute LocationManager.StartLocationUpdates, no permissions");

                if (_preferences.Get(UserSettingsKeys.ShowDebugToasts, false) is true)
                {
                    _userDialogs.Toast("Can't start location sharing, no permissions");
                }

                return;
            }

            _loggingService.Trace("Executing LocationManager.StartLocationUpdates");

            try
            {
                if (_preferences.Get(UserSettingsKeys.ShowDebugToasts, false) is true)
                {
                    _userDialogs.Toast("Live Location Sharing has started");
                }

                _isRunning = true;

                if (Geolocator.IsListening)
                {
                    await Geolocator.StopListeningAsync();
                }

                if (Geolocator.IsGeolocationAvailable && Geolocator.IsGeolocationEnabled)
                {
                    Geolocator.DesiredAccuracy  = 5;
                    Geolocator.PositionChanged += Geolocator_PositionChanged;

                    _forceSendTimer.Elapsed += async(sender, args) =>
                    {
                        if (!_isRunning)
                        {
                            _forceSendTimer.Stop();
                            return;
                        }

                        // Ensure it updates at least every 60 seconds
                        if (DateTime.Now - _lastUpdateTime < TimeSpan.FromSeconds(MinimalUpdateTimespan))
                        {
                            return;
                        }

                        var lastKnownLocation = await Geolocator.GetLastKnownLocationAsync();

                        if (lastKnownLocation != null)
                        {
                            try
                            {
                                var position = new Position(lastKnownLocation.Latitude, lastKnownLocation.Longitude);
                                await UpdateLocation(position);
                            }
                            catch (Exception ex)
                            {
                                _loggingService.Error(ex, "Error Executing LocationManager._forceSendTimer.Elapsed");
                            }
                        }
                    };


                    //every 5 second, 5 meters
                    await Geolocator.StartListeningAsync(TimeSpan.FromSeconds(5), 5);

                    _forceSendTimer.Start();
                }
                else
                {
                    Mvx.IoCProvider.Resolve <IUserDialogs>().Alert(
                        "Please ensure that geolocation is enabled and permissions are allowed for Wasabee to start sharing your location.",
                        "Geolocation Disabled", "OK");
                }
            }
            catch (Exception ex)
            {
                _loggingService.Error(ex, "Error Executing LocationManager.StartLocationUpdates");
            }
        }