diff --git a/MapControl/Avalonia/MapPanel.Avalonia.cs b/MapControl/Avalonia/MapPanel.Avalonia.cs index 2ab72cfe..a4778b13 100644 --- a/MapControl/Avalonia/MapPanel.Avalonia.cs +++ b/MapControl/Avalonia/MapPanel.Avalonia.cs @@ -7,8 +7,8 @@ namespace MapControl public static readonly AttachedProperty AutoCollapseProperty = DependencyPropertyHelper.RegisterAttached("AutoCollapse", typeof(MapPanel)); - public static readonly AttachedProperty LocationProperty = - DependencyPropertyHelper.RegisterAttached("Location", typeof(MapPanel)); + public static readonly AttachedProperty LocationProperty = + DependencyPropertyHelper.RegisterAttached("Location", typeof(MapPanel)); public static readonly AttachedProperty BoundingBoxProperty = DependencyPropertyHelper.RegisterAttached("BoundingBox", typeof(MapPanel)); diff --git a/MapControl/Shared/Location.cs b/MapControl/Shared/Location.cs index e73b80f5..f3b3a77c 100644 --- a/MapControl/Shared/Location.cs +++ b/MapControl/Shared/Location.cs @@ -11,24 +11,20 @@ namespace MapControl #else [System.ComponentModel.TypeConverter(typeof(LocationConverter))] #endif - public readonly struct Location(double latitude, double longitude) : IEquatable + public class Location(double latitude, double longitude) : IEquatable { public double Latitude { get; } = Math.Min(Math.Max(latitude, -90d), 90d); public double Longitude => longitude; - public static bool operator ==(Location loc1, Location loc2) => loc1.Equals(loc2); - - public static bool operator !=(Location loc1, Location loc2) => !loc1.Equals(loc2); - public bool LatitudeEquals(double latitude) => Math.Abs(Latitude - latitude) < 1e-9; public bool LongitudeEquals(double longitude) => Math.Abs(Longitude - longitude) < 1e-9; public bool Equals(double latitude, double longitude) => LatitudeEquals(latitude) && LongitudeEquals(longitude); - public bool Equals(Location location) => Equals(location.Latitude, location.Longitude); + public bool Equals(Location location) => location != null && Equals(location.Latitude, location.Longitude); - public override bool Equals(object obj) => obj is Location location && Equals(location); + public override bool Equals(object obj) => Equals(obj as Location); public override int GetHashCode() => Latitude.GetHashCode() ^ Longitude.GetHashCode(); diff --git a/MapControl/Shared/MapBase.cs b/MapControl/Shared/MapBase.cs index 82ba1fa1..24738e6a 100644 --- a/MapControl/Shared/MapBase.cs +++ b/MapControl/Shared/MapBase.cs @@ -43,7 +43,7 @@ namespace MapControl DependencyPropertyHelper.Register(nameof(MapProjection), new WebMercatorProjection(), (map, oldValue, newValue) => map.MapProjectionPropertyChanged(newValue)); - private Location? transformCenter; + private Location transformCenter; private Point viewCenter; private double centerLongitude; private double maxLatitude = 85.05112878; // default WebMercatorProjection @@ -326,8 +326,12 @@ namespace MapControl private Location CoerceCenterProperty(Location center) { - if (center.Latitude < -maxLatitude || center.Latitude > maxLatitude || - center.Longitude < -180d || center.Longitude > 180d) + if (center == null) + { + center = new Location(0d, 0d); + } + else if (center.Latitude < -maxLatitude || center.Latitude > maxLatitude || + center.Longitude < -180d || center.Longitude > 180d) { center = new Location( Math.Min(Math.Max(center.Latitude, -maxLatitude), maxLatitude), @@ -386,7 +390,7 @@ namespace MapControl ViewTransform.SetTransform(mapCenter, viewCenter, viewScale, -Heading); - if (transformCenter.HasValue) + if (transformCenter != null) { var center = ViewToLocation(new Point(ActualWidth / 2d, ActualHeight / 2d)); var latitude = center.Latitude; @@ -414,7 +418,7 @@ namespace MapControl { // Check if transform center has moved across 180° longitude. // - transformCenterChanged = Math.Abs(center.Longitude - transformCenter.Value.Longitude) > 180d; + transformCenterChanged = Math.Abs(center.Longitude - transformCenter.Longitude) > 180d; ResetTransformCenter(); mapCenter = MapProjection.LocationToMap(center); ViewTransform.SetTransform(mapCenter, viewCenter, viewScale, -Heading); diff --git a/MapControl/Shared/MapContentControl.cs b/MapControl/Shared/MapContentControl.cs index 5b524853..bf5616b0 100644 --- a/MapControl/Shared/MapContentControl.cs +++ b/MapControl/Shared/MapContentControl.cs @@ -19,7 +19,7 @@ namespace MapControl public partial class MapContentControl : ContentControl { public static readonly DependencyProperty LocationProperty = - DependencyPropertyHelper.Register(nameof(Location), default, + DependencyPropertyHelper.Register(nameof(Location), null, (control, oldValue, newValue) => MapPanel.SetLocation(control, newValue)); public static readonly DependencyProperty AutoCollapseProperty = diff --git a/MapControl/Shared/MapItem.cs b/MapControl/Shared/MapItem.cs index eeba061a..d961f831 100644 --- a/MapControl/Shared/MapItem.cs +++ b/MapControl/Shared/MapItem.cs @@ -23,7 +23,7 @@ namespace MapControl public partial class MapItem : ListBoxItem, IMapElement { public static readonly DependencyProperty LocationProperty = - DependencyPropertyHelper.Register(nameof(Location), default, + DependencyPropertyHelper.Register(nameof(Location), null, (item, oldValue, newValue) => { MapPanel.SetLocation(item, newValue); @@ -109,7 +109,7 @@ namespace MapControl private void UpdateMapTransform() { - if (MapTransform != null && ParentMap != null) + if (MapTransform != null && ParentMap != null && Location != null) { MapTransform.Matrix = ParentMap.GetMapToViewTransform(Location); } diff --git a/MapControl/Shared/MapItemsControl.cs b/MapControl/Shared/MapItemsControl.cs index edf47c8f..1744f832 100644 --- a/MapControl/Shared/MapItemsControl.cs +++ b/MapControl/Shared/MapItemsControl.cs @@ -68,7 +68,7 @@ namespace MapControl { var location = MapPanel.GetLocation(ContainerFromItem(item)); - return location.HasValue && predicate(location.Value); + return location!= null && predicate(location); }); } diff --git a/MapControl/Shared/MapPanel.cs b/MapControl/Shared/MapPanel.cs index b12b750e..fcdf93ce 100644 --- a/MapControl/Shared/MapPanel.cs +++ b/MapControl/Shared/MapPanel.cs @@ -99,15 +99,15 @@ namespace MapControl /// /// Gets the Location of an element. /// - public static Location? GetLocation(FrameworkElement element) + public static Location GetLocation(FrameworkElement element) { - return (Location?)element.GetValue(LocationProperty); + return (Location)element.GetValue(LocationProperty); } /// /// Sets the Location of an element. /// - public static void SetLocation(FrameworkElement element, Location? value) + public static void SetLocation(FrameworkElement element, Location value) { element.SetValue(LocationProperty, value); } @@ -261,9 +261,9 @@ namespace MapControl { var location = GetLocation(element); - if (location.HasValue) + if (location != null) { - var position = SetViewPosition(element, GetViewPosition(location.Value)); + var position = SetViewPosition(element, GetViewPosition(location)); if (GetAutoCollapse(element)) { diff --git a/MapControl/Shared/MapPath.cs b/MapControl/Shared/MapPath.cs index ef6dd13b..2e2b5519 100644 --- a/MapControl/Shared/MapPath.cs +++ b/MapControl/Shared/MapPath.cs @@ -17,7 +17,7 @@ namespace MapControl public partial class MapPath : IMapElement { public static readonly DependencyProperty LocationProperty = - DependencyPropertyHelper.Register(nameof(Location), default, + DependencyPropertyHelper.Register(nameof(Location), null, (path, oldValue, newValue) => path.UpdateData()); /// @@ -64,7 +64,7 @@ namespace MapControl protected virtual void UpdateData() { - if (ParentMap != null && Data != null) + if (Data != null && ParentMap != null && Location != null) { SetDataTransform(ParentMap.GetMapToViewTransform(Location)); } diff --git a/MapControl/Shared/MapPolypoint.cs b/MapControl/Shared/MapPolypoint.cs index 31c67796..412c9af5 100644 --- a/MapControl/Shared/MapPolypoint.cs +++ b/MapControl/Shared/MapPolypoint.cs @@ -40,7 +40,6 @@ namespace MapControl protected MapPolypoint() { Data = new PolypointGeometry(); - Location = new Location(0d, double.NaN); } protected void DataCollectionPropertyChanged(IEnumerable oldValue, IEnumerable newValue) @@ -65,32 +64,20 @@ namespace MapControl protected double GetLongitudeOffset(IEnumerable locations) { - if (!ParentMap.MapProjection.IsNormalCylindrical) + var longitudeOffset = 0d; + + if (ParentMap.MapProjection.IsNormalCylindrical) { - return 0d; + var location = Location ?? locations?.FirstOrDefault(); + + if (location != null && + !ParentMap.InsideViewBounds(ParentMap.LocationToView(location))) + { + longitudeOffset = ParentMap.NearestLongitude(location.Longitude) - location.Longitude; + } } - Location location; - - if (!double.IsNaN(Location.Longitude)) - { - location = Location; - } - else if (locations != null && locations.Any()) - { - location = locations.First(); - } - else - { - return 0d; - } - - if (ParentMap.InsideViewBounds(ParentMap.LocationToView(location))) - { - return 0d; - } - - return ParentMap.NearestLongitude(location.Longitude) - location.Longitude; + return longitudeOffset; } } } diff --git a/MapControl/WPF/MapPanel.WPF.cs b/MapControl/WPF/MapPanel.WPF.cs index 71ef2537..42da0b77 100644 --- a/MapControl/WPF/MapPanel.WPF.cs +++ b/MapControl/WPF/MapPanel.WPF.cs @@ -8,7 +8,7 @@ namespace MapControl DependencyPropertyHelper.RegisterAttached< bool>("AutoCollapse", typeof(MapPanel)); public static readonly DependencyProperty LocationProperty = - DependencyPropertyHelper.RegisterAttached("Location", typeof(MapPanel), null, + DependencyPropertyHelper.RegisterAttached("Location", typeof(MapPanel), null, FrameworkPropertyMetadataOptions.AffectsParentArrange); public static readonly DependencyProperty BoundingBoxProperty = diff --git a/MapControl/WinUI/MapPanel.WinUI.cs b/MapControl/WinUI/MapPanel.WinUI.cs index 5a352b77..0841b3de 100644 --- a/MapControl/WinUI/MapPanel.WinUI.cs +++ b/MapControl/WinUI/MapPanel.WinUI.cs @@ -14,7 +14,7 @@ namespace MapControl DependencyPropertyHelper.RegisterAttached("AutoCollapse", typeof(MapPanel)); public static readonly DependencyProperty LocationProperty = - DependencyPropertyHelper.RegisterAttached("Location", typeof(MapPanel), null, + DependencyPropertyHelper.RegisterAttached("Location", typeof(MapPanel), null, (element, oldValue, newValue) => (element.Parent as MapPanel)?.InvalidateArrange()); public static readonly DependencyProperty BoundingBoxProperty =