Skip to content

Commit

Permalink
feat: more fields in Geoposition (on Android)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkar70 committed Sep 17, 2020
1 parent 4511160 commit 81fbb8d
Showing 1 changed file with 66 additions and 24 deletions.
90 changes: 66 additions & 24 deletions src/Uno.UWP/Devices/Geolocation/Geolocator.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,30 +150,72 @@ static class Extensions
{
private const uint Wgs84SpatialReferenceId = 4326;

public static Geoposition ToGeoPosition(this Location location)
=> new Geoposition(
new Geocoordinate(
latitude: location.Latitude,
longitude: location.Longitude,
altitude: location.Altitude,
timestamp: FromUnixTime(location.Time),
speed: location.HasSpeed ? location.Speed : 0,
point: new Geopoint(
new BasicGeoposition
{
Latitude = location.Latitude,
Longitude = location.Longitude,
Altitude = location.Altitude,
},
AltitudeReferenceSystem.Ellipsoid,
Wgs84SpatialReferenceId
),
accuracy: 0,
altitudeAccuracy: 0,
heading: null
)
);

public static Windows.Devices.Geolocation.Geoposition ToGeoPosition(this Location location)
{
double? geoheading = null;
if (location.HasBearing)
{
geoheading = location.Bearing;
}

Windows.Devices.Geolocation.PositionSource posSource;
switch (location.Provider)
{
case Android.Locations.LocationManager.NetworkProvider:
posSource = Windows.Devices.Geolocation.PositionSource.Cellular; // cell, wifi
break;
case Android.Locations.LocationManager.PassiveProvider:
posSource = Windows.Devices.Geolocation.PositionSource.Unknown; // other apps
break;
case Android.Locations.LocationManager.GpsProvider:
posSource = Windows.Devices.Geolocation.PositionSource.Satellite;
break;
default:
// ex.: "fused" - all merged, also e.g. Google Play
posSource = Windows.Devices.Geolocation.PositionSource.Unknown;
break;
}

Windows.Devices.Geolocation.BasicGeoposition basicGeoposition;
basicGeoposition.Altitude = location.Altitude;
basicGeoposition.Latitude = location.Latitude;
basicGeoposition.Longitude = location.Longitude;

Windows.Devices.Geolocation.Geopoint geopoint = new Windows.Devices.Geolocation.Geopoint(basicGeoposition,
Windows.Devices.Geolocation.AltitudeReferenceSystem.Ellipsoid,
Wgs84SpatialReferenceId
);

double? locVertAccuracy = null;
// VerticalAccuracy is since API 26
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
if (location.HasVerticalAccuracy)
{
locVertAccuracy = location.VerticalAccuracyMeters;
}
}


Windows.Devices.Geolocation.Geoposition geopos = new Windows.Devices.Geolocation.Geoposition(
new Windows.Devices.Geolocation.Geocoordinate(
latitude: location.Latitude,
longitude: location.Longitude,
altitude: location.Altitude,
timestamp: DateTimeOffset.FromUnixTimeMilliseconds(location.Time),
speed: location.HasSpeed ? location.Speed : 0,
point: geopoint,
accuracy: location.HasAccuracy ? location.Accuracy : 0,
altitudeAccuracy: locVertAccuracy,
heading: geoheading,
positionSource: posSource
)
);

return geopos;
}


private static DateTimeOffset FromUnixTime(long time)
{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
Expand Down

0 comments on commit 81fbb8d

Please sign in to comment.