2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
|
|
|
|
|
// © 2017 Clemens Fischer
|
2012-07-07 17:19:10 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2012-05-04 12:52:20 +02:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2013-11-05 18:00:03 +01:00
|
|
|
|
/// A geographic location with latitude and longitude values in degrees.
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// </summary>
|
2014-07-09 21:27:28 +02:00
|
|
|
|
public partial class Location : IEquatable<Location>
|
2012-05-04 12:52:20 +02:00
|
|
|
|
{
|
2012-05-06 11:28:47 +02:00
|
|
|
|
private double latitude;
|
|
|
|
|
|
private double longitude;
|
|
|
|
|
|
|
2012-05-04 12:52:20 +02:00
|
|
|
|
public Location()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-11-18 19:49:17 +01:00
|
|
|
|
public Location(double latitude, double longitude)
|
2012-05-04 12:52:20 +02:00
|
|
|
|
{
|
2013-11-21 21:16:29 +01:00
|
|
|
|
Latitude = latitude;
|
|
|
|
|
|
Longitude = longitude;
|
2012-05-04 12:52:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-05-06 11:28:47 +02:00
|
|
|
|
public double Latitude
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return latitude; }
|
2013-11-21 21:16:29 +01:00
|
|
|
|
set { latitude = Math.Min(Math.Max(value, -90d), 90d); }
|
2012-05-06 11:28:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public double Longitude
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return longitude; }
|
|
|
|
|
|
set { longitude = value; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-09 21:27:28 +02:00
|
|
|
|
public bool Equals(Location location)
|
|
|
|
|
|
{
|
2016-01-14 17:56:25 +01:00
|
|
|
|
return ReferenceEquals(this, location)
|
|
|
|
|
|
|| (location != null
|
2014-07-09 21:27:28 +02:00
|
|
|
|
&& location.latitude == latitude
|
2016-01-14 17:56:25 +01:00
|
|
|
|
&& location.longitude == longitude);
|
2014-07-09 21:27:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Equals(obj as Location);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
|
{
|
|
|
|
|
|
return latitude.GetHashCode() ^ longitude.GetHashCode();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
|
|
|
|
|
return string.Format(CultureInfo.InvariantCulture, "{0:F5},{1:F5}", latitude, longitude);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Location Parse(string s)
|
2012-05-04 12:52:20 +02:00
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
var values = s.Split(new char[] { ',' });
|
2015-04-21 19:17:32 +02:00
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
if (values.Length != 2)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
|
|
|
|
|
throw new FormatException("Location string must be a comma-separated pair of double values");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new Location(
|
2017-06-25 23:05:48 +02:00
|
|
|
|
double.Parse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture),
|
|
|
|
|
|
double.Parse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture));
|
2012-05-04 12:52:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-19 19:36:03 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Normalizes a longitude to a value in the interval [-180..180].
|
|
|
|
|
|
/// </summary>
|
2012-11-22 21:42:29 +01:00
|
|
|
|
public static double NormalizeLongitude(double longitude)
|
2012-05-04 12:52:20 +02:00
|
|
|
|
{
|
2013-12-20 17:05:10 +01:00
|
|
|
|
if (longitude < -180d)
|
2013-11-21 21:16:29 +01:00
|
|
|
|
{
|
2013-12-20 17:05:10 +01:00
|
|
|
|
longitude = ((longitude + 180d) % 360d) + 180d;
|
2013-11-21 21:16:29 +01:00
|
|
|
|
}
|
2013-12-20 17:05:10 +01:00
|
|
|
|
else if (longitude > 180d)
|
2013-11-21 21:16:29 +01:00
|
|
|
|
{
|
2013-12-20 17:05:10 +01:00
|
|
|
|
longitude = ((longitude - 180d) % 360d) - 180d;
|
2013-11-21 21:16:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return longitude;
|
2012-05-04 12:52:20 +02:00
|
|
|
|
}
|
2016-04-19 19:36:03 +02:00
|
|
|
|
|
|
|
|
|
|
internal static double NearestLongitude(double longitude, double referenceLongitude)
|
|
|
|
|
|
{
|
|
|
|
|
|
longitude = NormalizeLongitude(longitude);
|
|
|
|
|
|
|
|
|
|
|
|
if (longitude > referenceLongitude + 180d)
|
|
|
|
|
|
{
|
|
|
|
|
|
longitude -= 360d;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (longitude < referenceLongitude - 180d)
|
|
|
|
|
|
{
|
|
|
|
|
|
longitude += 360d;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return longitude;
|
|
|
|
|
|
}
|
2012-05-04 12:52:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|