XAML-Map-Control/MapControl/Shared/MapPath.cs

116 lines
3.1 KiB
C#
Raw Normal View History

2025-12-19 07:38:06 +01:00
#if WPF
2024-05-22 11:25:32 +02:00
using System.Windows;
using System.Windows.Media;
2021-11-17 23:17:11 +01:00
#elif UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
2024-05-22 11:25:32 +02:00
#elif WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
2025-08-19 19:43:02 +02:00
#elif AVALONIA
using Avalonia;
using Avalonia.Media;
#endif
2026-04-13 17:14:49 +02:00
namespace MapControl;
/// <summary>
/// A path element with a Data property that holds a Geometry in view coordinates or
/// projected map coordinates that are relative to an origin Location.
/// </summary>
public partial class MapPath : IMapElement
{
2026-04-13 17:14:49 +02:00
public static readonly DependencyProperty LocationProperty =
DependencyPropertyHelper.Register<MapPath, Location>(nameof(Location), null,
(path, oldValue, newValue) => path.UpdateData());
/// <summary>
2026-04-13 17:14:49 +02:00
/// Gets or sets a Location that is either used as
/// - the origin point of a geometry specified in projected map coordinates (meters) or
/// - as an optional anchor point to constrain the view position of MapPaths with
/// multiple Locations (like MapPolyline or MapPolygon) to the visible map viewport,
/// as done for elements where the MapPanel.Location property is set.
/// </summary>
2026-04-13 17:14:49 +02:00
public Location Location
{
2026-04-13 17:14:49 +02:00
get => (Location)GetValue(LocationProperty);
set => SetValue(LocationProperty, value);
}
2026-04-13 17:14:49 +02:00
/// <summary>
/// Implements IMapElement.ParentMap.
/// </summary>
public MapBase ParentMap
{
get;
set
{
2026-04-13 17:14:49 +02:00
if (field != null)
{
2026-04-13 17:14:49 +02:00
field.ViewportChanged -= OnViewportChanged;
}
2026-04-13 17:14:49 +02:00
field = value;
2026-04-13 17:14:49 +02:00
if (field != null)
{
field.ViewportChanged += OnViewportChanged;
}
2020-05-13 18:17:28 +02:00
UpdateData();
}
2026-04-13 17:14:49 +02:00
}
2026-04-13 17:14:49 +02:00
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
{
UpdateData();
}
protected void SetDataTransform(Matrix matrix)
{
if (Data.Transform is MatrixTransform transform
#if WPF
2026-04-13 17:14:49 +02:00
&& !transform.IsFrozen
#endif
2026-04-13 17:14:49 +02:00
)
{
transform.Matrix = matrix;
}
2026-04-13 17:14:49 +02:00
else
{
2026-04-13 17:14:49 +02:00
Data.Transform = new MatrixTransform { Matrix = matrix };
}
2026-04-13 17:14:49 +02:00
}
2026-04-13 17:14:49 +02:00
protected virtual void UpdateData()
{
if (Data != null && ParentMap != null && Location != null)
{
2026-04-13 17:14:49 +02:00
SetDataTransform(ParentMap.GetMapToViewTransform(Location));
}
2026-04-13 17:14:49 +02:00
MapPanel.SetLocation(this, Location);
}
2026-04-13 17:14:49 +02:00
protected Point LocationToMap(Location location, double longitudeOffset)
{
var point = ParentMap.MapProjection.LocationToMap(location.Latitude, location.Longitude + longitudeOffset);
2026-04-13 17:14:49 +02:00
if (point.Y == double.PositiveInfinity)
{
2026-04-13 17:14:49 +02:00
point = new Point(point.X, 1e9);
2023-01-25 19:55:42 +01:00
}
2026-04-13 17:14:49 +02:00
else if (point.Y == double.NegativeInfinity)
{
point = new Point(point.X, -1e9);
}
return point;
}
protected Point LocationToView(Location location, double longitudeOffset)
{
return ParentMap.ViewTransform.MapToView(LocationToMap(location, longitudeOffset));
}
}