Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devices.Sensors.Location: rework argument handling in constructors #19459

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions src/Essentials/src/Types/Location.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,40 +49,45 @@ public Location()
/// <summary>
/// Initializes a new instance of the <see cref="Location"/> class with the specified latitude and longitude.
/// </summary>
/// <param name="latitude">Default latitude for location.</param>
/// <param name="longitude">Default longitude for location.</param>
public Location(double latitude, double longitude)
/// <param name="latitude">Latitude in degrees. Must be in the interval [-90, 90].</param>
/// <param name="longitude">Longitude in degrees. Will be projected to the interval (-180, 180].</param>
public Location(double latitude, double longitude) : this(latitude, longitude, DateTimeOffset.UtcNow)
{
Latitude = Math.Min(Math.Max(latitude, -90.0), 90.0);
Longitude = Math.Min(Math.Max(longitude, -180.0), 180.0);
Timestamp = DateTimeOffset.UtcNow;
}

/// <summary>
/// Initializes a new instance of the <see cref="Location"/> class with the specified latitude, longitude, and timestamp.
/// </summary>
/// <param name="latitude">Default latitude for location.</param>
/// <param name="longitude">Default longitude for location.</param>
/// <param name="latitude">Latitude in degrees. Must be in the interval [-90, 90].</param>
/// <param name="longitude">Longitude in degrees. Will be projected to the interval (-180, 180].</param>
/// <param name="timestamp">UTC timestamp for the location.</param>
public Location(double latitude, double longitude, DateTimeOffset timestamp)
{
Latitude = latitude;
// check if latitude is in [-90, 90]
if (Math.Abs(latitude) > 90)
throw new ArgumentOutOfRangeException(nameof(latitude));
else
Latitude = latitude;

// make sure that longitude is in (-180, 180]
Longitude = longitude;
while (Longitude > 180)
Longitude -= 360;
while (Longitude <= -180)
Longitude += 360;

Timestamp = timestamp;
}

/// <summary>
/// Initializes a new instance of the <see cref="Location"/> class with the specified latitude, longitude, and altitude.
/// </summary>
/// <param name="latitude">Default latitude for location.</param>
/// <param name="longitude">Default longitude for location.</param>
/// <param name="altitude">Default altitude for location.</param>
public Location(double latitude, double longitude, double altitude)
/// <param name="latitude">Latitude in degrees. Must be in the interval [-90, 90].</param>
/// <param name="longitude">Longitude in degrees. Will be projected to the interval (-180, 180].</param>
/// <param name="altitude">Altitude in meters.</param>
public Location(double latitude, double longitude, double altitude) : this(latitude, longitude)
{
Latitude = latitude;
Longitude = longitude;
Altitude = altitude;
Timestamp = DateTimeOffset.UtcNow;
}

/// <summary>
Expand Down
Loading