Changed struct Location to class

This commit is contained in:
ClemensFischer 2026-02-01 23:48:56 +01:00
parent 35820aa27e
commit 6429776853
11 changed files with 38 additions and 51 deletions

View file

@ -7,8 +7,8 @@ namespace MapControl
public static readonly AttachedProperty<bool> AutoCollapseProperty =
DependencyPropertyHelper.RegisterAttached<bool>("AutoCollapse", typeof(MapPanel));
public static readonly AttachedProperty<Location?> LocationProperty =
DependencyPropertyHelper.RegisterAttached<Location?>("Location", typeof(MapPanel));
public static readonly AttachedProperty<Location> LocationProperty =
DependencyPropertyHelper.RegisterAttached<Location>("Location", typeof(MapPanel));
public static readonly AttachedProperty<BoundingBox> BoundingBoxProperty =
DependencyPropertyHelper.RegisterAttached<BoundingBox>("BoundingBox", typeof(MapPanel));

View file

@ -11,24 +11,20 @@ namespace MapControl
#else
[System.ComponentModel.TypeConverter(typeof(LocationConverter))]
#endif
public readonly struct Location(double latitude, double longitude) : IEquatable<Location>
public class Location(double latitude, double longitude) : IEquatable<Location>
{
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();

View file

@ -43,7 +43,7 @@ namespace MapControl
DependencyPropertyHelper.Register<MapBase, MapProjection>(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);

View file

@ -19,7 +19,7 @@ namespace MapControl
public partial class MapContentControl : ContentControl
{
public static readonly DependencyProperty LocationProperty =
DependencyPropertyHelper.Register<MapContentControl, Location>(nameof(Location), default,
DependencyPropertyHelper.Register<MapContentControl, Location>(nameof(Location), null,
(control, oldValue, newValue) => MapPanel.SetLocation(control, newValue));
public static readonly DependencyProperty AutoCollapseProperty =

View file

@ -23,7 +23,7 @@ namespace MapControl
public partial class MapItem : ListBoxItem, IMapElement
{
public static readonly DependencyProperty LocationProperty =
DependencyPropertyHelper.Register<MapItem, Location>(nameof(Location), default,
DependencyPropertyHelper.Register<MapItem, Location>(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);
}

View file

@ -68,7 +68,7 @@ namespace MapControl
{
var location = MapPanel.GetLocation(ContainerFromItem(item));
return location.HasValue && predicate(location.Value);
return location!= null && predicate(location);
});
}

View file

@ -99,15 +99,15 @@ namespace MapControl
/// <summary>
/// Gets the Location of an element.
/// </summary>
public static Location? GetLocation(FrameworkElement element)
public static Location GetLocation(FrameworkElement element)
{
return (Location?)element.GetValue(LocationProperty);
return (Location)element.GetValue(LocationProperty);
}
/// <summary>
/// Sets the Location of an element.
/// </summary>
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))
{

View file

@ -17,7 +17,7 @@ namespace MapControl
public partial class MapPath : IMapElement
{
public static readonly DependencyProperty LocationProperty =
DependencyPropertyHelper.Register<MapPath, Location>(nameof(Location), default,
DependencyPropertyHelper.Register<MapPath, Location>(nameof(Location), null,
(path, oldValue, newValue) => path.UpdateData());
/// <summary>
@ -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));
}

View file

@ -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<Location> 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;
}
}
}

View file

@ -8,7 +8,7 @@ namespace MapControl
DependencyPropertyHelper.RegisterAttached< bool>("AutoCollapse", typeof(MapPanel));
public static readonly DependencyProperty LocationProperty =
DependencyPropertyHelper.RegisterAttached<Location?>("Location", typeof(MapPanel), null,
DependencyPropertyHelper.RegisterAttached<Location>("Location", typeof(MapPanel), null,
FrameworkPropertyMetadataOptions.AffectsParentArrange);
public static readonly DependencyProperty BoundingBoxProperty =

View file

@ -14,7 +14,7 @@ namespace MapControl
DependencyPropertyHelper.RegisterAttached<bool>("AutoCollapse", typeof(MapPanel));
public static readonly DependencyProperty LocationProperty =
DependencyPropertyHelper.RegisterAttached<Location?>("Location", typeof(MapPanel), null,
DependencyPropertyHelper.RegisterAttached<Location>("Location", typeof(MapPanel), null,
(element, oldValue, newValue) => (element.Parent as MapPanel)?.InvalidateArrange());
public static readonly DependencyProperty BoundingBoxProperty =