XAML-Map-Control/MapControl/Location.cs

84 lines
2.3 KiB
C#
Raw Normal View History

// XAML Map Control - http://xamlmapcontrol.codeplex.com/
2012-07-07 17:19:10 +02:00
// Copyright © 2012 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
2012-05-04 12:52:20 +02:00
using System.Globalization;
namespace MapControl
{
/// <summary>
/// A geographic location given as latitude and longitude.
/// </summary>
public partial class Location : IEquatable<Location>
2012-05-04 12:52:20 +02:00
{
private double latitude;
private double longitude;
internal double Y;
2012-05-04 12:52:20 +02:00
public Location()
{
}
public Location(double lat, double lon)
2012-05-04 12:52:20 +02:00
{
Latitude = lat;
Longitude = lon;
2012-05-04 12:52:20 +02:00
}
public double Latitude
{
get { return latitude; }
set
{
latitude = Math.Min(Math.Max(value, -90d), 90d);
Y = double.NaN;
}
}
public double Longitude
{
get { return longitude; }
set { longitude = value; }
}
public bool Equals(Location other)
{
return other != null && other.latitude == latitude && other.longitude == longitude;
}
2012-05-04 12:52:20 +02:00
public override bool Equals(object obj)
2012-05-04 12:52:20 +02:00
{
return Equals(obj as Location);
2012-05-04 12:52:20 +02:00
}
public override int GetHashCode()
2012-05-04 12:52:20 +02:00
{
return latitude.GetHashCode() ^ longitude.GetHashCode();
2012-05-04 12:52:20 +02: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
{
var tokens = s.Split(new char[] { ',' });
if (tokens.Length != 2)
{
throw new FormatException("Location string must be a comma-separated pair of double values");
}
return new Location(
double.Parse(tokens[0], NumberStyles.Float, CultureInfo.InvariantCulture),
double.Parse(tokens[1], NumberStyles.Float, CultureInfo.InvariantCulture));
2012-05-04 12:52:20 +02:00
}
public static double NormalizeLongitude(double longitude)
2012-05-04 12:52:20 +02:00
{
return (longitude >= -180d && longitude <= 180d) ? longitude : ((longitude + 180d) % 360d + 360d) % 360d - 180d;
2012-05-04 12:52:20 +02:00
}
}
}