Skip to content

GPS

Operating Systems
Android
iOS
Windows

Shiny makes realtime background location updates easy on MAUI.

FeatureiOSAndroidWindows
Foreground GPSFullFullFull
Background GPSFullFull (foreground service)Not Supported
Shiny.LocationsNuGet package Shiny.Locations
Shiny.Hosting.MauiNuGet package Shiny.Hosting.Maui
IGpsManager gpsManager; // injected, resolved, etc
await gpsManager.StartListener(new GpsRequest
{
UseBackground = true
});
gpsManager.StopListening();

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 ANDROID
public 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, etc
await gpsManager.StartListener(new GpsRequest
{
UseBackground = true
});

The platform mechanics don’t always play by the rules for time & distance filters that you may expect especially with a more ‘real time’ setup.
To work around these issues, you can use the GpsDelegate class which will handle the filtering for you.

public class MyGpsDelegate : GpsDelegate
{
public MyGpsDelegate(ILogger<MyGpsDelegate> logger) : base(logger)
{
// if either of these filters pass, the OnGpsReading method will be called
this.MinimumDistance = Distance.FromMeters(200);
this.MinimumTime = TimeSpan.FromMinutes(1);
}
protected override async Task OnGpsReading(GpsReading reading)
{
}
}