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

150 lines
4.7 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2023-01-03 15:12:53 +01:00
// Copyright © 2023 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
2021-06-14 21:41:37 +02:00
#if WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
2021-11-17 23:17:11 +01:00
#elif UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
#else
using System.Windows;
using System.Windows.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
{
public static readonly DependencyProperty LocationProperty = DependencyProperty.Register(
nameof(Location), typeof(Location), typeof(MapPath),
2020-05-13 18:17:28 +02:00
new PropertyMetadata(null, (o, e) => ((MapPath)o).UpdateData()));
private MapBase parentMap;
/// <summary>
/// Gets or sets a Location that is used as
2022-11-05 17:32:29 +01:00
/// - either the origin point of a geometry specified in projected map coordinates (meters)
/// - or as an optional value 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);
}
public MapBase ParentMap
{
2022-08-06 10:40:59 +02:00
get => parentMap;
set
{
if (parentMap != null)
{
parentMap.ViewportChanged -= OnViewportChanged;
}
parentMap = value;
if (parentMap != null)
{
parentMap.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();
}
2020-05-13 18:17:28 +02:00
protected virtual void UpdateData()
{
2020-06-03 00:07:07 +02:00
MapPanel.SetLocation(this, Location);
2022-11-02 23:51:15 +01:00
if (parentMap != null && Location != null && Data != null)
{
2020-06-03 00:07:07 +02:00
var scale = parentMap.GetScale(Location);
2022-11-03 20:10:17 +01:00
var matrix = new Matrix(scale.X, 0d, 0d, scale.Y, 0d, 0d);
matrix.Rotate(parentMap.ViewTransform.Rotation);
2020-05-13 18:17:28 +02:00
2022-11-03 20:10:17 +01:00
if (Data.Transform is MatrixTransform transform)
{
transform.Matrix = matrix;
}
else
{
Data.Transform = new MatrixTransform { Matrix = matrix };
}
2020-05-13 18:17:28 +02:00
}
}
#region Methods used only by derived classes MapPolyline, MapPolygon and MapMultiPolygon
2020-05-13 18:17:28 +02:00
protected double GetLongitudeOffset(Location location)
{
2020-05-13 18:17:28 +02:00
var longitudeOffset = 0d;
2022-03-05 18:40:57 +01:00
if (location != null && parentMap.MapProjection.Type <= MapProjectionType.NormalCylindrical)
{
var point = parentMap.LocationToView(location);
2020-05-13 18:17:28 +02:00
if (point.HasValue &&
(point.Value.X < 0d || point.Value.X > parentMap.RenderSize.Width ||
point.Value.Y < 0d || point.Value.Y > parentMap.RenderSize.Height))
{
2020-05-13 18:17:28 +02:00
longitudeOffset = parentMap.ConstrainedLongitude(location.Longitude) - location.Longitude;
}
}
2020-05-13 18:17:28 +02:00
return longitudeOffset;
}
protected Point? LocationToMap(Location location, double longitudeOffset)
{
if (longitudeOffset != 0d)
{
location = new Location(location.Latitude, location.Longitude + longitudeOffset);
}
var point = parentMap.MapProjection.LocationToMap(location);
2022-12-06 18:07:43 +01:00
if (point.HasValue)
{
2022-12-06 18:07:43 +01:00
if (point.Value.Y == double.PositiveInfinity)
{
point = new Point(point.Value.X, 1e9);
}
else if (point.Value.Y == double.NegativeInfinity)
{
point = new Point(point.Value.X, -1e9);
}
}
return point;
}
protected Point? LocationToView(Location location, double longitudeOffset)
{
var point = LocationToMap(location, longitudeOffset);
if (!point.HasValue)
{
return null;
}
return parentMap.ViewTransform.MapToView(point.Value);
}
2020-05-13 18:17:28 +02:00
#endregion
}
}