/// <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 }
protected override void OnStart() { gpsManager.StartListener(new GpsRequest { ThrottledInterval = TimeSpan.FromSeconds(2), Interval = TimeSpan.FromSeconds(4), Priority = GpsPriority.Highest, UseBackground = true }); }
public static async Task <AccessState> RequestAccessAndStart(this IGpsManager gps, GpsRequest request) { var access = await gps.RequestAccess(request.UseBackground); if (access == AccessState.Available) { await gps.StartListener(request); } return(access); }
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 MainPage(IGpsManager manager, IEventAggregator eventAggregator) { InitializeComponent(); var map = new Map { CRS = "EPSG:3857", Transformation = new MinimalTransformation() }; // map.Limiter.PanLimits = new BoundingBox(new Mapsui.Geometries.Point(45.26, 24.68), new Mapsui.Geometries.Point(60.56, 39.26)); map.Layers.Add(OpenStreetMap.CreateTileLayer()); map.Widgets.Add(new ScaleBarWidget(map) { TextAlignment = Alignment.Center, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Top }); map.Widgets.Add(new ZoomInOutWidget { MarginX = 20, MarginY = 40 }); mapView.Map = map; manager.RequestAccessAndStart(new GpsRequest()); manager.StartListener(new GpsRequest()); mapView.Navigator = new Navigator(mapView.Map, (IViewport)mapView.Viewport); eventAggregator.GetEvent <GpsDataReceivedEvent>().Subscribe(e => { Xamarin.Forms.Device.BeginInvokeOnMainThread(() => { var coords = new Position(e.Position.Latitude, e.Position.Longitude); info.Text = $"{coords} - D:{(int)e.Heading} S:{Math.Round(e.Speed, 2)}"; mapView.MyLocationLayer.UpdateMyLocation(new Position(e.Position.Latitude, e.Position.Longitude)); mapView.MyLocationLayer.UpdateMyDirection(e.Heading, mapView.Viewport.Rotation); mapView.MyLocationLayer.UpdateMySpeed(e.Speed); }); }); }
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(); }); }