Merge pull request #1739 from leonluc-dev/LocationAccuracyHandling

Added support for reduced accuracy detection on iOS 14
This commit is contained in:
Gerald Versluis 2023-08-30 13:58:46 +02:00 коммит произвёл GitHub
Родитель d521376076 abb0864f57
Коммит 6f527dbeba
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 36 добавлений и 4 удалений

Просмотреть файл

@ -69,11 +69,13 @@ namespace DeviceTests
});
var request = new GeolocationRequest(GeolocationAccuracy.Best);
request.RequestFullAccuracy = true;
var location = await Geolocation.GetLocationAsync(request);
Assert.NotNull(location);
Assert.True(location.Accuracy > 0);
Assert.False(location.ReducedAccuracy);
Assert.NotEqual(0.0, location.Latitude);
Assert.NotEqual(0.0, location.Longitude);

Просмотреть файл

@ -19,7 +19,14 @@ namespace Xamarin.Essentials
var manager = new CLLocationManager();
var location = manager.Location;
return location?.ToLocation();
var reducedAccuracy = false;
#if __IOS__
if (Platform.HasOSVersion(14, 0))
{
reducedAccuracy = manager.AccuracyAuthorization == CLAccuracyAuthorization.ReducedAccuracy;
}
#endif
return location?.ToLocation(reducedAccuracy);
}
static async Task<Location> PlatformLocationAsync(GeolocationRequest request, CancellationToken cancellationToken)
@ -51,9 +58,22 @@ namespace Xamarin.Essentials
manager.StartUpdatingLocation();
var reducedAccuracy = false;
#if __IOS__
if (Platform.HasOSVersion(14, 0))
{
if (request.RequestFullAccuracy && manager.AccuracyAuthorization == CLAccuracyAuthorization.ReducedAccuracy)
{
await manager.RequestTemporaryFullAccuracyAuthorizationAsync("XamarinEssentialsFullAccuracyUsageDescription");
}
reducedAccuracy = manager.AccuracyAuthorization == CLAccuracyAuthorization.ReducedAccuracy;
}
#endif
var clLocation = await tcs.Task;
return clLocation?.ToLocation();
return clLocation?.ToLocation(reducedAccuracy);
void HandleLocation(CLLocation location)
{

Просмотреть файл

@ -57,6 +57,8 @@ namespace Xamarin.Essentials
public GeolocationAccuracy DesiredAccuracy { get; set; }
public bool RequestFullAccuracy { get; set; }
public override string ToString() =>
$"{nameof(DesiredAccuracy)}: {DesiredAccuracy}, {nameof(Timeout)}: {Timeout}";
}

Просмотреть файл

@ -58,6 +58,7 @@ namespace Xamarin.Essentials
Altitude = point.Altitude;
Accuracy = point.Accuracy;
VerticalAccuracy = point.VerticalAccuracy;
ReducedAccuracy = point.ReducedAccuracy;
Speed = point.Speed;
Course = point.Course;
IsFromMockProvider = point.IsFromMockProvider;
@ -75,6 +76,8 @@ namespace Xamarin.Essentials
public double? VerticalAccuracy { get; set; }
public bool ReducedAccuracy { get; set; }
public double? Speed { get; set; }
public double? Course { get; set; }

Просмотреть файл

@ -34,6 +34,7 @@ namespace Xamarin.Essentials
#else
default(float?),
#endif
ReducedAccuracy = false,
Course = location.HasBearing ? location.Bearing : default(double?),
Speed = location.HasSpeed ? location.Speed : default(double?),
#pragma warning disable CS0618 // Type or member is obsolete

Просмотреть файл

@ -18,13 +18,14 @@ namespace Xamarin.Essentials
Longitude = placemark.Location.Coordinate.Longitude,
Altitude = placemark.Location.Altitude,
AltitudeReferenceSystem = AltitudeReferenceSystem.Geoid,
Timestamp = DateTimeOffset.UtcNow
Timestamp = DateTimeOffset.UtcNow,
ReducedAccuracy = false,
};
internal static IEnumerable<Location> ToLocations(this IEnumerable<CLPlacemark> placemarks) =>
placemarks?.Select(a => a.ToLocation());
internal static Location ToLocation(this CLLocation location) =>
internal static Location ToLocation(this CLLocation location, bool reducedAccuracy) =>
new Location
{
Latitude = location.Coordinate.Latitude,
@ -32,6 +33,7 @@ namespace Xamarin.Essentials
Altitude = location.VerticalAccuracy < 0 ? default(double?) : location.Altitude,
Accuracy = location.HorizontalAccuracy,
VerticalAccuracy = location.VerticalAccuracy,
ReducedAccuracy = reducedAccuracy,
Timestamp = location.Timestamp.ToDateTime(),
#if __IOS__ || __WATCHOS__
Course = location.Course < 0 ? default(double?) : location.Course,

Просмотреть файл

@ -34,6 +34,7 @@ namespace Xamarin.Essentials
Altitude = location.Coordinate.Point.Position.Altitude,
Accuracy = location.Coordinate.Accuracy,
VerticalAccuracy = location.Coordinate.AltitudeAccuracy,
ReducedAccuracy = false,
Speed = (!location.Coordinate.Speed.HasValue || double.IsNaN(location.Coordinate.Speed.Value)) ? default : location.Coordinate.Speed,
Course = (!location.Coordinate.Heading.HasValue || double.IsNaN(location.Coordinate.Heading.Value)) ? default : location.Coordinate.Heading,
IsFromMockProvider = false,
@ -49,6 +50,7 @@ namespace Xamarin.Essentials
Altitude = coordinate.Point.Position.Altitude,
Accuracy = coordinate.Accuracy,
VerticalAccuracy = coordinate.AltitudeAccuracy,
ReducedAccuracy = false,
Speed = (!coordinate.Speed.HasValue || double.IsNaN(coordinate.Speed.Value)) ? default : coordinate.Speed,
Course = (!coordinate.Heading.HasValue || double.IsNaN(coordinate.Heading.Value)) ? default : coordinate.Heading,
AltitudeReferenceSystem = coordinate.Point.AltitudeReferenceSystem.ToEssentials()