From cc690c54459c3c6a7b33f3fabcc3df99a7c2a266 Mon Sep 17 00:00:00 2001 From: ClemensF Date: Sun, 6 May 2012 11:28:47 +0200 Subject: [PATCH] Added Location.NormalizeLongitude and fixed LocationAnimation --- MapControl/Location.cs | 25 +++++++++++++++++++++---- MapControl/LocationAnimation.cs | 4 +++- MapControl/Map.cs | 2 +- MapControl/MapGraticule.cs | 2 +- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/MapControl/Location.cs b/MapControl/Location.cs index ed2ccb56..7035698d 100644 --- a/MapControl/Location.cs +++ b/MapControl/Location.cs @@ -11,6 +11,9 @@ namespace MapControl [TypeConverter(typeof(LocationConverter))] public class Location { + private double latitude; + private double longitude; + public Location() { } @@ -21,8 +24,22 @@ namespace MapControl Longitude = longitude; } - public double Latitude { get; set; } - public double Longitude { get; set; } + public double Latitude + { + get { return latitude; } + set { latitude = Math.Min(Math.Max(value, -90d), 90d); } + } + + public double Longitude + { + get { return longitude; } + set { longitude = value; } + } + + public override string ToString() + { + return string.Format(CultureInfo.InvariantCulture, "{0:0.00000},{1:0.00000}", latitude, longitude); + } public static Location Parse(string source) { @@ -30,9 +47,9 @@ namespace MapControl return new Location(p.X, p.Y); } - public override string ToString() + public static double NormalizeLongitude(double longitude) { - return string.Format(CultureInfo.InvariantCulture, "{0:0.00000},{1:0.00000}", Latitude, Longitude); + return ((longitude + 180d) % 360d + 360d) % 360d - 180d; } } diff --git a/MapControl/LocationAnimation.cs b/MapControl/LocationAnimation.cs index 9f7178e4..83642e7a 100644 --- a/MapControl/LocationAnimation.cs +++ b/MapControl/LocationAnimation.cs @@ -42,9 +42,11 @@ namespace MapControl progress = EasingFunction.Ease(progress); } + double deltaLongitude = progress * Location.NormalizeLongitude(To.Longitude - From.Longitude); + return new Location( (1d - progress) * From.Latitude + progress * To.Latitude, - (1d - progress) * From.Longitude + progress * To.Longitude); + Location.NormalizeLongitude(From.Longitude + deltaLongitude)); } } } diff --git a/MapControl/Map.cs b/MapControl/Map.cs index e9339e20..f2832dab 100644 --- a/MapControl/Map.cs +++ b/MapControl/Map.cs @@ -574,7 +574,7 @@ namespace MapControl private Location CoerceCenter(Location location) { location.Latitude = Math.Min(Math.Max(location.Latitude, -MapTransform.MaxLatitude), MapTransform.MaxLatitude); - location.Longitude = ((location.Longitude + 180d) % 360d + 360d) % 360d - 180d; + location.Longitude = Location.NormalizeLongitude(location.Longitude); return location; } diff --git a/MapControl/MapGraticule.cs b/MapControl/MapGraticule.cs index 53d5e7f2..8ed45fe4 100644 --- a/MapControl/MapGraticule.cs +++ b/MapControl/MapGraticule.cs @@ -177,7 +177,7 @@ namespace MapControl Point latPos = new Point(p.X + t + 2d, p.Y - t - FontSize / 4d); Point lonPos = new Point(p.X + t + 2d, p.Y + t + FontSize); string latString = CoordinateString(lat, format, "NS"); - string lonString = CoordinateString(((lon + 180d) % 360d + 360d) % 360d - 180d, format, "EW"); + string lonString = CoordinateString(Location.NormalizeLongitude(lon), format, "EW"); drawingContext.PushTransform(new RotateTransform(parentMap.Heading, p.X, p.Y)); drawingContext.DrawGlyphRun(Foreground, GlyphRunText.Create(latString, typeface, FontSize, latPos));