Select to view content in your preferred language

Tracking location in the background while .NET MAUI app is suspended or screen is off

332
11
Jump to solution
3 weeks ago
Labels (3)
KevinCheriyan
Frequent Contributor

This may not exactly be a question about the Maps SDK for .NET, but given that it's a .NET MAUI app that uses the SDK and location services, I thought I'd probe the mobile dev community for advice. 

I have a .NET MAUI app with a map page that I'm using to track the location of the user on a map. It basically shows the breadcrumb path the user has taken on their hike with a dashboard showing their distance, timer and average speed. It works exactly as it should when the screen is turned on and the app is on the foreground: path updates when location is changed and dashboard metrics change as expected. 

But when the app is minimized/suspended, or if another app is being used, or if the screen is turned off, location stops being tracked. And the timer pauses in the background as well. I restructured some of this code for location tracking to work on a background thread like following, but no changes in results.

 

mapViewModel.locationDataSource.LocationChanged += async (sender, e) => await LocationChanged(sender, e); // call LocationChanged method when location changes in LocationDataSource

private async Task LocationChanged(object sender, Esri.ArcGISRuntime.Location.Location e)
    {
        await Task.Run(() => { 
            MapPoint mp = e.Position;
            UpdatePolyline(mp)
    }

private void UpdatePolyline(MapPoint mp)
    {
        polylineBuilder.AddPoint(mp);
        Geometry polylineGeometry = polylineBuilder.ToGeometry();

        // UI updates marshaled to the main thread.
        MainThread.BeginInvokeOnMainThread(() =>
        {
            trackedHikePolylineGraphicsOverlay.Graphics.Clear();
            trackedHikePolylineGraphicsOverlay.Graphics.Add(new Graphic(polylineGeometry));
            
        });
    }

 

I've also added these permissions to my AndroidManifest.xml

 

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

 

I will also need to add similar permissions to the .plist for iOS permissions. Not sure what they are yet. 

What have other developers done to implement background location tracking in their .NET MAUI apps? I know Android and iOS apps can do this, because I can record a manual activity in Strava while my screen is turned off. Permissions for my Maui app is set to "Allow location all the time" for Precise location.

Thanks in advance!


--------------------------------------------------
Application Developer, GeoMarvel
11 Replies
KevinCheriyan
Frequent Contributor

Thanks! The SDK makes it incredible simple for iOS for location tracking. Hopefully similar things in store for Android soon 🤞

I'm out of the office, so getting my colleague and fellow developer @SeanS to move this forward. Thanks for your help!


--------------------------------------------------
Application Developer, GeoMarvel
0 Kudos
dotMorten_esri
Esri Notable Contributor

Unfortunately the Android platform makes implementing the scenario a lot harder and has to be done at the application level with the foreground service. There's not much we can really do at the API level, besides improving our documentation to help implement this.