Skip to content

Commit bc477a6

Browse files
authored
Geolocation foreground listening (#1832)
* Geolocation foreground listening. * Edits.
1 parent ebd130c commit bc477a6

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

docs/platform-integration/device/geolocation.md

+63
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,69 @@ Not all location values may be available, depending on the device. For example,
138138
> [!WARNING]
139139
> <xref:Microsoft.Maui.Devices.Sensors.IGeolocation.GetLocationAsync%2A> can return `null` in some scenarios. This indicates that the underlying platform is unable to obtain the current location.
140140
141+
::: moniker range=">=net-maui-8.0"
142+
143+
## Listen for location changes
144+
145+
In addition to querying the device for the current location, you can listen for location changes while an app is in the foreground.
146+
147+
To check to see if the app is currently listening for location changes, there's a `IsListeningForeground` property you can query. Once you're ready to start listening for location changes you should call the `StartListeningForegroundAsync` method. This method starts listening for location updates and raises the `LocationChanged` event when the location changes, provided that the app is in the foreground. The `GeolocationLocationChangedEventArgs` object that accompanies this event has a `Location` property, of type <xref:Microsoft.Maui.Devices.Sensors.Location>, that represents the new location that's been detected.
148+
149+
> [!NOTE]
150+
> When necessary, the Geolocation API prompts the user for permissions.
151+
152+
The following code example demonstrates how to listen for a location change, and how to process the changed location:
153+
154+
```csharp
155+
async void OnStartListening()
156+
{
157+
try
158+
{
159+
Geolocation.LocationChanged += Geolocation_LocationChanged;
160+
var request = new GeolocationListeningRequest((GeolocationAccuracy)Accuracy);
161+
var success = await Geolocation.StartListeningForegroundAsync(request);
162+
163+
string status = success
164+
? "Started listening for foreground location updates"
165+
: "Couldn't start listening";
166+
}
167+
catch (Exception ex)
168+
{
169+
// Unable to start listening for location changes
170+
}
171+
}
172+
173+
void Geolocation_LocationChanged(object sender, GeolocationLocationChangedEventArgs e)
174+
{
175+
// Process e.Location to get the new location
176+
}
177+
```
178+
179+
Error handling can be implemented by registering an event handler for the `ListeningFailed` event. The `GeolocationListeningFailedEventArgs` object that accompanies this event has an `Error` property, of type `GeolocationError`, that indicates why listening failed. When the `ListeningFailed` event is raised, listening for further location changes stops and no further `LocationChanged` events are raised.
180+
181+
To stop listening for location changes, call the `StopListeningForeground` method:
182+
183+
```csharp
184+
void OnStopListening()
185+
{
186+
try
187+
{
188+
Geolocation.LocationChanged -= Geolocation_LocationChanged;
189+
Geolocation.StopListeningForeground();
190+
string status = "Stopped listening for foreground location updates";
191+
}
192+
catch (Exception ex)
193+
{
194+
// Unable to stop listening for location changes
195+
}
196+
}
197+
```
198+
199+
> [!NOTE]
200+
> The `StopListeningForeground` method has no effect when the app isn't listening for location changes.
201+
202+
::: moniker-end
203+
141204
## Accuracy
142205

143206
The following sections outline the location accuracy distance, per platform:

docs/whats-new/dotnet-8.md

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ For information about what's new in .NET 8, see [What's new in .NET 8](/dotnet/c
4444
- Shell navigation gains a `GoToAsync` overload that enables you to pass single use navigation data, that's cleared after navigation has occurred, as a `ShellNavigationQueryParameters` object. For more information, see [Pass single use object-based navigation data](~/fundamentals/shell/navigation.md#pass-single-use-object-based-navigation-data).
4545
- Window management can be decoupled from the `App` class. For more information, see [Decouple window management from the App class](~/fundamentals/windows.md#decouple-window-management-from-the-app-class).
4646
- Menu bar items and context menu items can be invoked through keyboard shortcuts known as keyboard accelerators. For more information, see [Keyboard accelerators](~/user-interface/keyboard-accelerators.md).
47+
- The <xref:Microsoft.Maui.Devices.Sensors.Geolocation> class can listen for location changes when app's are in the foreground. For more information, see [Listen for location changes](~/platform-integration/device/geolocation.md#listen-for-location-changes).
4748

4849
The following types or members have been deprecated:
4950

0 commit comments

Comments
 (0)