/// <summary> /// This method will start the GPS with realtime and close on disposable (or when the app backgrounds) /// </summary> /// <param name="gpsManager"></param> /// <returns></returns> public static IObservable <IGpsReading> StartAndReceive(this IGpsManager gpsManager) => Observable.Create <IGpsReading>(ob => { var composite = new CompositeDisposable(); var platform = ShinyHost.Resolve <IPlatform>(); gpsManager .WhenReading() .Subscribe( ob.OnNext, ob.OnError ) .DisposedBy(composite); platform .WhenStateChanged() .Where(x => x == PlatformState.Background) .Subscribe(_ => ob.Respond(null)) .DisposedBy(composite); gpsManager .StartListener(GpsRequest.Foreground) .ContinueWith(x => { if (x.IsFaulted) { ob.OnError(x.Exception); } }); return(() => { composite.Dispose(); gpsManager.StopListener(); }); });
/// <summary> /// Requests a single GPS reading by starting the listener and stopping once a reading is received /// Requests a single GPS reading - This will start & stop the gps listener if wasn't running already /// </summary> /// <param name="gpsManager"></param> /// <returns></returns> public static IObservable <IGpsReading> GetCurrentPosition(this IGpsManager gpsManager) => Observable.FromAsync(async ct => { var iStarted = false; try { await currentLocSemaphore .WaitAsync(ct) .ConfigureAwait(false); var task = gpsManager .WhenReading() .Take(1) .ToTask(ct); if (!gpsManager.IsListening()) { iStarted = true; await gpsManager .StartListener(GpsRequest.Foreground) .ConfigureAwait(false); } var reading = await task.ConfigureAwait(false); return(reading); } finally { currentLocSemaphore.Release(); if (iStarted) { await gpsManager.StopListener().ConfigureAwait(false); } } });
private void StartShinyLocation() { SafeExecute(async() => { var result = await this._gpsManager.RequestAccess(new GpsRequest { UseBackground = false }); if (!(result == Shiny.AccessState.Available)) { return; } if (_gpsManager.IsListening) { await _gpsManager.StopListener(); } await _gpsManager.StartListener(new GpsRequest { UseBackground = false, Priority = GpsPriority.Highest, Interval = TimeSpan.FromSeconds(5), ThrottledInterval = TimeSpan.FromSeconds(3) //Should be lower than Interval }); }); }
public async Task Run(CancellationToken token) { if (_gpsManager.IsListening) { return; } #region Subscribe GPS reading event //購読重複を避けるために購読を解除する _gpsObserver?.Dispose(); //位置情報取得イベントを購読 _gpsObserver = _gpsManager .WhenReading() .Subscribe(async x => { if (token.IsCancellationRequested) { await _gpsManager.StopListener(); _gpsObserver?.Dispose(); return; } MainThread.BeginInvokeOnMainThread(() => { var message = new LocationReadMessage { GpsInfo = x, }; MessagingCenter.Send(message, nameof(LocationReadMessage)); }); await _notificationManager.ScheduleNotification($"{x.Position.Latitude}, {x.Position.Longitude}", "test"); }); #endregion #region GPS start listen. //位置情報取得許可を得る var checkResult = await Permissions.CheckStatusAsync <Permissions.LocationAlways>(); if (checkResult != PermissionStatus.Granted) { var requestResult = await Permissions.RequestAsync <Permissions.LocationAlways>(); if (requestResult != PermissionStatus.Granted) { return; } } var request = new GpsRequest { Interval = TimeSpan.FromSeconds(5), UseBackground = false, }; await _gpsManager.StartListener(request); #endregion }
public async void StopGps() { if (IsGpsConnected) { await _gpsManager.StopListener(); Log.Event($"GPS Disconnected"); MessagingCenter.Send <IGpsManager>(_gpsManager, Constants.Message.GPS_STATE_CHANGED); } }
public async void OnNavigatedTo(INavigationParameters parameters) { if (_gpsManager.IsListening) { await _gpsManager.StopListener(); } await _gpsManager.StartListener(new GpsRequest { UseBackground = true, Priority = GpsPriority.Highest, Interval = TimeSpan.FromSeconds(5), ThrottledInterval = TimeSpan.FromSeconds(3) //Should be lower than Interval }); }
public GpsViewModel(IGpsManager gps) { this.Start = ReactiveCommand.CreateFromTask( async() => { this.foreground = gps .WhenReading() .Subscribe(reading => { this.Latitude = reading.Position.Latitude; this.Longitude = reading.Position.Longitude; }); await gps.StartListener(GpsRequest.Realtime(true)); } ); this.Stop = ReactiveCommand.CreateFromTask(async() => { await gps.StopListener(); this.foreground?.Dispose(); }); }