Matt Ohren
Matt Ohren

Reputation: 21

Need a way to continually track user's current location in Xamarin Forms on both Android and iOS

I am working on a senior project for school that requires me to continuously track users through their phones GPS coordinates. My end goal is continually to update my Mapsui map with the current user's location. I am hitting a roadblock on how to constantly update the data to Mapsui. If I enter static GPS coordinates, the point is displayed properly on my map.

Really, the core issue that is tripping me up is the async functions and how to properly implement them when getting location data with Xamarin.Essentials.

Any ideas or tips would be greatly appreciated!

Upvotes: 0

Views: 3021

Answers (1)

Lucas Zhang
Lucas Zhang

Reputation: 18861

It seems that you want track user's location in the background . If you have read the docs about Xamarin.Essentials: Geolocation .There is someting more that you should do.

in iOS project

1. Add the Privacy in info.plist

<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access location when open.</string>
<key>NSLocationAlwaysAndWhenInUsageDescription</key>
<string>This app needs access location when open or in background.</string>
// support iOS 10 and before
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access location when open.</string>

2.For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended.

Support for some types of background execution must be declared in advance by the app that uses them. You declare the background modes your app supports from the Capabilities tab of your project settings in VS . Enabling the Background Modes option adds the UIBackgroundModes key to your app’s Info.plist file.

enter image description here

For more details about Background Execution in iOS you can refer this docs

in Android project

You can create a background service .

The IntentService class provides a straightforward structure for running an operation on a single background thread. This allows it to handle long-running operations without affecting your user interface's responsiveness. Also, an IntentService isn't affected by most user interface lifecycle events, so it continues to run in circumstances that would shut down an AsyncTask.

For more detail you can refer here.

Upvotes: 1

Related Questions