Location equality

This commit is contained in:
ClemensFischer 2026-01-25 11:57:14 +01:00
parent 1e13a3bffb
commit 7208cc71bd
9 changed files with 50 additions and 30 deletions

View file

@ -136,7 +136,7 @@ namespace MapControl
centerCts?.Cancel();
centerAnimation = CreateAnimation(CenterProperty, new Location(targetCenter.Latitude, CoerceLongitude(targetCenter.Longitude)));
centerAnimation = CreateAnimation(CenterProperty, new Location(targetCenter.Latitude, NearestLongitude(targetCenter.Longitude)));
using (centerCts = new CancellationTokenSource())
{

View file

@ -35,7 +35,11 @@ namespace MapControl
public double Latitude { get; }
public double Longitude { get; }
public bool Equals(double latitude, double longitude) => Latitude == latitude && Longitude == longitude;
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) => location != null && Equals(location.Latitude, location.Longitude);

View file

@ -45,7 +45,7 @@ namespace MapControl
public static readonly DependencyProperty ProjectionCenterProperty =
DependencyPropertyHelper.Register<MapBase, Location>(nameof(ProjectionCenter), null,
(map, oldValue, newValue) => map.ProjectionCenterPropertyChanged());
(map, oldValue, newValue) => map.ProjectionCenterPropertyChanged(newValue));
private Location transformCenter;
private Point viewCenter;
@ -365,7 +365,7 @@ namespace MapControl
return point.X >= 0d && point.Y >= 0d && point.X <= ActualWidth && point.Y <= ActualHeight;
}
internal double CoerceLongitude(double longitude)
internal double NearestLongitude(double longitude)
{
var offset = longitude - Center.Longitude;
@ -445,10 +445,23 @@ namespace MapControl
UpdateTransform(false, true);
}
private void ProjectionCenterPropertyChanged()
private void ProjectionCenterPropertyChanged(Location center)
{
ResetTransformCenter();
UpdateTransform();
if (!internalPropertyChange)
{
if (center != null)
{
var longitude = Location.NormalizeLongitude(center.Longitude);
if (!center.LongitudeEquals(longitude))
{
SetValueInternal(ProjectionCenterProperty, new Location(center.Latitude, longitude));
}
}
ResetTransformCenter();
UpdateTransform();
}
}
private void UpdateTransform(bool resetTransformCenter = false, bool projectionChanged = false)
@ -471,16 +484,19 @@ namespace MapControl
if (center != null)
{
var centerLat = center.Latitude;
var centerLon = Location.NormalizeLongitude(center.Longitude);
var latitude = center.Latitude;
var longitude = Location.NormalizeLongitude(center.Longitude);
if (centerLat < -maxLatitude || centerLat > maxLatitude)
if (latitude < -maxLatitude || latitude > maxLatitude)
{
centerLat = Math.Min(Math.Max(centerLat, -maxLatitude), maxLatitude);
latitude = Math.Min(Math.Max(latitude, -maxLatitude), maxLatitude);
resetTransformCenter = true;
}
center = new Location(centerLat, centerLon);
if (!center.Equals(latitude, longitude))
{
center = new Location(latitude, longitude);
}
SetValueInternal(CenterProperty, center);

View file

@ -205,12 +205,12 @@ namespace MapControl
if (parentMap.MapProjection.IsNormalCylindrical &&
position.HasValue && !parentMap.InsideViewBounds(position.Value))
{
var coercedPosition = parentMap.LocationToView(
location.Latitude, parentMap.CoerceLongitude(location.Longitude));
var nearestPosition = parentMap.LocationToView(
location.Latitude, parentMap.NearestLongitude(location.Longitude));
if (coercedPosition.HasValue)
if (nearestPosition.HasValue)
{
position = coercedPosition;
position = nearestPosition;
}
}
@ -229,12 +229,12 @@ namespace MapControl
if (location != null)
{
var coercedPosition = parentMap.LocationToView(
location.Latitude, parentMap.CoerceLongitude(location.Longitude));
var nearestPosition = parentMap.LocationToView(
location.Latitude, parentMap.NearestLongitude(location.Longitude));
if (coercedPosition.HasValue)
if (nearestPosition.HasValue)
{
position = coercedPosition.Value;
position = nearestPosition.Value;
}
}
}

View file

@ -82,7 +82,7 @@ namespace MapControl
if (position.HasValue && !ParentMap.InsideViewBounds(position.Value))
{
longitudeOffset = ParentMap.CoerceLongitude(location.Longitude) - location.Longitude;
longitudeOffset = ParentMap.NearestLongitude(location.Longitude) - location.Longitude;
}
}

View file

@ -71,7 +71,10 @@ namespace MapControl
if (value != null)
{
SetCenter(value);
var longitude = Location.NormalizeLongitude(value.Longitude);
SetCenter(value.LongitudeEquals(longitude) ? value : new Location(value.Latitude, longitude));
updateCenter = false;
}
}
@ -89,12 +92,9 @@ namespace MapControl
{
if (updateCenter)
{
var latitude = value.Latitude;
var longitude = Location.NormalizeLongitude(value.Longitude);
if (center == null || !center.Equals(latitude, longitude))
if (center == null || !center.Equals(value))
{
center = new Location(latitude, longitude);
center = value;
CenterChanged();
}
}

View file

@ -130,7 +130,7 @@ namespace MapControl
centerAnimation = new LocationAnimation
{
To = new Location(targetCenter.Latitude, CoerceLongitude(targetCenter.Longitude)),
To = new Location(targetCenter.Latitude, NearestLongitude(targetCenter.Longitude)),
Duration = AnimationDuration,
EasingFunction = AnimationEasingFunction
};

View file

@ -155,7 +155,7 @@ namespace MapControl
centerAnimation = new PointAnimation
{
From = new Windows.Foundation.Point(Center.Longitude, Center.Latitude),
To = new Windows.Foundation.Point(CoerceLongitude(targetCenter.Longitude), targetCenter.Latitude),
To = new Windows.Foundation.Point(NearestLongitude(targetCenter.Longitude), targetCenter.Latitude),
Duration = AnimationDuration,
EasingFunction = AnimationEasingFunction,
EnableDependentAnimation = true

View file

@ -37,7 +37,7 @@ namespace SampleApplication
public MainWindow()
{
var httpClient = new HttpClient(new HttpHandler()) { Timeout = TimeSpan.FromSeconds(10) };
httpClient.DefaultRequestHeaders.Add("User-Agent", "XAML Map Control Avalonia Sample Application");
httpClient.DefaultRequestHeaders.Add("User-Agent", "XAML Map Control WPF Sample Application");
ImageLoader.HttpClient = httpClient;
var loggerFactory = LoggerFactory.Create(builder => builder.AddDebug().SetMinimumLevel(LogLevel.Information));