GPS
Getting Started
Xamarin Essentials is great, but Shiny makes realtime background location updates a thing on Xamarin/MAUI based platform.
Setup
Starting/Stopping the GPS Service
IGpsManager gpsManager; // injected, resolved, etcawait gpsManager.StartListener(new GpsRequest{ UseBackground = true});
gpsManager.StopListening();
Watching Movement in the Foreground
var subscription = this.manager .WhenReading() .Subscribe(reading => { // make sure to dispatch to your main thread here });
// and dispose of your subscription when donesubscription.Dispose();
Observing in the background
First, create the delegate that implements IGpsDelegate
public partial class MyGpsDelegate : Shiny.Locations.IGpsDelegate{ public MyGpsDelegate() { // like all other shiny delegates, dependency injection works here // treat this as a singleton }
public Task OnReading(IGpsReading reading) { // do something with the reading }}
#if ANDROIDpublic partial class MyGpsDelegate : Shiny.IAndroidForegroundServiceDelegate{ public void ConfigureNotification(AndroidX.Core.App.NotificationCompat.Builder builder) { builder .SetContentTitle("MyApp") .SetContentText("My App is following you!! images") .SetSmallIcon(Resource.Mipmap.youricon); }}#endif
Next, let’s register that with your app builder/hosting/service collection
services.UseGps<MyGpsDelegate>();
Lastly, last start it up
IGpsManager gpsManager; // injected, resolved, etcawait gpsManager.StartListener(new GpsRequest{ UseBackground = true});