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

117 lines
3.4 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
namespace MapControl
{
/// <summary>
/// A path element with a Data property that holds a Geometry in view coordinates or
2022-11-05 17:32:29 +01:00
/// projected map coordinates that are relative to an origin Location.
/// </summary>
public partial class MapPath : IMapElement
{
2024-05-23 18:08:14 +02:00
public static readonly DependencyProperty LocationProperty =
2026-02-01 23:48:56 +01:00
DependencyPropertyHelper.Register<MapPath, Location>(nameof(Location), null,
2024-05-23 18:08:14 +02:00
(path, oldValue, newValue) => path.UpdateData());
/// <summary>
/// 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>
public Location Location
{
2022-08-06 10:40:59 +02:00
get => (Location)GetValue(LocationProperty);
set => SetValue(LocationProperty, value);
}
/// <summary>
/// Implements IMapElement.ParentMap.
/// </summary>
public MapBase ParentMap
{
2025-12-27 21:24:01 +01:00
get;
set
{
2025-12-27 21:24:01 +01:00
if (field != null)
{
2025-12-27 21:24:01 +01:00
field.ViewportChanged -= OnViewportChanged;
}
2025-12-27 21:24:01 +01:00
field = value;
2025-12-27 21:24:01 +01:00
if (field != null)
{
2025-12-27 21:24:01 +01:00
field.ViewportChanged += OnViewportChanged;
}
2020-05-13 18:17:28 +02:00
UpdateData();
}
}
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
{
2020-05-13 18:17:28 +02:00
UpdateData();
}
protected void SetDataTransform(Matrix matrix)
{
if (Data.Transform is MatrixTransform transform
#if WPF
&& !transform.IsFrozen
#endif
)
{
transform.Matrix = matrix;
}
else
{
Data.Transform = new MatrixTransform { Matrix = matrix };
}
}
2020-05-13 18:17:28 +02:00
protected virtual void UpdateData()
{
2026-02-01 23:48:56 +01:00
if (Data != null && ParentMap != null && Location != null)
{
SetDataTransform(ParentMap.GetMapToViewTransform(Location));
2020-05-13 18:17:28 +02:00
}
2023-01-22 16:05:16 +01:00
MapPanel.SetLocation(this, Location);
}
protected Point LocationToMap(Location location, double longitudeOffset)
{
2025-12-27 21:24:01 +01:00
var point = ParentMap.MapProjection.LocationToMap(location.Latitude, location.Longitude + longitudeOffset);
if (point.Y == double.PositiveInfinity)
{
point = new Point(point.X, 1e9);
}
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));
2023-01-25 19:55:42 +01:00
}
}
}