-
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f7673c
commit 29f653d
Showing
37 changed files
with
326 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace MauiMaps; | ||
|
||
public static class Geolocator | ||
{ | ||
public static IGeolocator Default = new GeolocatorImplementation(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace MauiMaps; | ||
|
||
public interface IGeolocator | ||
{ | ||
Task StartListening(IProgress<Location> positionChangedProgress, CancellationToken cancellationToken); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using Android.Locations; | ||
using Android.OS; | ||
using Android.Runtime; | ||
using CommunityToolkit.Maui.Alerts; | ||
using Location = Android.Locations.Location; | ||
using Permissions = Microsoft.Maui.ApplicationModel.Permissions; | ||
|
||
namespace MauiMaps; | ||
|
||
public class GeolocatorImplementation : IGeolocator | ||
{ | ||
GeolocationContinuousListener? locator; | ||
|
||
public async Task StartListening(IProgress<Microsoft.Maui.Devices.Sensors.Location> positionChangedProgress, CancellationToken cancellationToken) | ||
{ | ||
var permission = await Permissions.CheckStatusAsync<Permissions.LocationAlways>(); | ||
if (permission != PermissionStatus.Granted) | ||
{ | ||
permission = await Permissions.RequestAsync<Permissions.LocationAlways>(); | ||
if (permission != PermissionStatus.Granted) | ||
{ | ||
await Toast.Make("No permission").Show(CancellationToken.None); | ||
return; | ||
} | ||
} | ||
|
||
locator = new GeolocationContinuousListener(); | ||
var taskCompletionSource = new TaskCompletionSource(); | ||
cancellationToken.Register(() => | ||
{ | ||
locator.Dispose(); | ||
locator = null; | ||
taskCompletionSource.TrySetResult(); | ||
}); | ||
locator.OnLocationChangedAction = location => | ||
positionChangedProgress.Report( | ||
new Microsoft.Maui.Devices.Sensors.Location(location.Latitude, location.Longitude)); | ||
await taskCompletionSource.Task; | ||
} | ||
} | ||
|
||
internal class GeolocationContinuousListener : Java.Lang.Object, ILocationListener | ||
{ | ||
public Action<Location>? OnLocationChangedAction { get; set; } | ||
|
||
LocationManager? locationManager; | ||
|
||
public GeolocationContinuousListener() | ||
{ | ||
locationManager = (LocationManager?)Android.App.Application.Context.GetSystemService(Android.Content.Context.LocationService); | ||
locationManager?.RequestLocationUpdates(LocationManager.GpsProvider, 1000, 0, this); | ||
} | ||
|
||
public void OnLocationChanged(Location location) | ||
{ | ||
OnLocationChangedAction?.Invoke(location); | ||
} | ||
|
||
public void OnProviderDisabled(string provider) | ||
{ | ||
} | ||
|
||
public void OnProviderEnabled(string provider) | ||
{ | ||
} | ||
|
||
public void OnStatusChanged(string? provider, [GeneratedEnum] Availability status, Bundle? extras) | ||
{ | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
base.Dispose(disposing); | ||
locationManager?.RemoveUpdates(this); | ||
locationManager?.Dispose(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
MauiMaps/Platforms/MacCatalyst/GeolocatorImplementation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
namespace MauiMaps; | ||
|
||
using CommunityToolkit.Maui.Alerts; | ||
using CoreLocation; | ||
|
||
public class GeolocatorImplementation : IGeolocator | ||
{ | ||
readonly CLLocationManager manager = new(); | ||
|
||
public async Task StartListening(IProgress<Location> positionChangedProgress, CancellationToken cancellationToken) | ||
{ | ||
var permission = await Permissions.CheckStatusAsync<Permissions.LocationAlways>(); | ||
if (permission != PermissionStatus.Granted) | ||
{ | ||
permission = await Permissions.RequestAsync<Permissions.LocationAlways>(); | ||
if (permission != PermissionStatus.Granted) | ||
{ | ||
await Toast.Make("No permission").Show(CancellationToken.None); | ||
return; | ||
} | ||
} | ||
var taskCompletionSource = new TaskCompletionSource(); | ||
cancellationToken.Register(() => | ||
{ | ||
manager.LocationsUpdated -= PositionChanged; | ||
taskCompletionSource.TrySetResult(); | ||
}); | ||
manager.LocationsUpdated += PositionChanged; | ||
|
||
void PositionChanged(object? sender, CLLocationsUpdatedEventArgs args) | ||
{ | ||
if (args.Locations.Length > 0) | ||
{ | ||
var coordinate = args.Locations[^1].Coordinate; | ||
positionChangedProgress.Report(new Location(coordinate.Latitude, coordinate.Longitude)); | ||
} | ||
} | ||
|
||
await taskCompletionSource.Task; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.