2025-02-27 18:46:32 +01:00
|
|
|
|
using Windows.Foundation;
|
2022-04-27 18:00:40 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#if UWP
|
2024-05-26 20:32:29 +02:00
|
|
|
|
using Windows.UI.Xaml;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using Windows.UI.Xaml.Controls;
|
2024-05-26 21:25:36 +02:00
|
|
|
|
using Windows.UI.Xaml.Data;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using Windows.UI.Xaml.Media;
|
2013-04-12 19:59:16 +02:00
|
|
|
|
using Windows.UI.Xaml.Shapes;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#else
|
2024-05-26 20:32:29 +02:00
|
|
|
|
using Microsoft.UI.Xaml;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
using Microsoft.UI.Xaml.Controls;
|
2024-05-26 21:25:36 +02:00
|
|
|
|
using Microsoft.UI.Xaml.Data;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
using Microsoft.UI.Xaml.Media;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Shapes;
|
2021-06-14 21:41:37 +02:00
|
|
|
|
#endif
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2024-05-26 20:32:29 +02:00
|
|
|
|
public partial class MapGraticule : MapPanel
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2024-05-26 20:32:29 +02:00
|
|
|
|
public static readonly DependencyProperty ForegroundProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<MapGraticule, Brush>(nameof(Foreground));
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty FontFamilyProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<MapGraticule, FontFamily>(nameof(FontFamily));
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty FontSizeProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<MapGraticule, double>(nameof(FontSize), 12d);
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty StrokeThicknessProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<MapGraticule, double>(nameof(StrokeThickness), 0.5);
|
|
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
private readonly Path path = new Path { Data = new PathGeometry() };
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2024-05-26 20:32:29 +02:00
|
|
|
|
public Brush Foreground
|
|
|
|
|
|
{
|
|
|
|
|
|
get => (Brush)GetValue(ForegroundProperty);
|
|
|
|
|
|
set => SetValue(ForegroundProperty, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public FontFamily FontFamily
|
|
|
|
|
|
{
|
|
|
|
|
|
get => (FontFamily)GetValue(FontFamilyProperty);
|
|
|
|
|
|
set => SetValue(FontFamilyProperty, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public double FontSize
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2024-05-26 20:32:29 +02:00
|
|
|
|
get => (double)GetValue(FontSizeProperty);
|
|
|
|
|
|
set => SetValue(FontSizeProperty, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public double StrokeThickness
|
|
|
|
|
|
{
|
|
|
|
|
|
get => (double)GetValue(StrokeThicknessProperty);
|
|
|
|
|
|
set => SetValue(StrokeThicknessProperty, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void SetParentMap(MapBase map)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (map != null && Foreground == null)
|
|
|
|
|
|
{
|
2024-05-26 21:25:36 +02:00
|
|
|
|
SetBinding(ForegroundProperty,
|
|
|
|
|
|
new Binding { Source = map, Path = new PropertyPath(nameof(Foreground)) });
|
2024-05-26 20:32:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
base.SetParentMap(map);
|
2016-08-08 20:36:02 +02:00
|
|
|
|
}
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2017-02-05 18:35:30 +01:00
|
|
|
|
protected override void OnViewportChanged(ViewportChangedEventArgs e)
|
2016-08-08 20:36:02 +02:00
|
|
|
|
{
|
2022-04-28 18:34:30 +02:00
|
|
|
|
var labels = DrawGraticule(((PathGeometry)path.Data).Figures);
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
if (Children.Count == 0)
|
|
|
|
|
|
{
|
2024-05-26 21:25:36 +02:00
|
|
|
|
path.SetBinding(Shape.StrokeProperty,
|
|
|
|
|
|
new Binding { Source = this, Path = new PropertyPath(nameof(Foreground)) });
|
|
|
|
|
|
|
|
|
|
|
|
path.SetBinding(Shape.StrokeThicknessProperty,
|
|
|
|
|
|
new Binding { Source = this, Path = new PropertyPath(nameof(StrokeThickness)) });
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
Children.Add(path);
|
|
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
var childrenCount = 1;
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
foreach (var label in labels)
|
|
|
|
|
|
{
|
|
|
|
|
|
TextBlock textBlock;
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
if (childrenCount < Children.Count)
|
2018-03-06 22:22:58 +01:00
|
|
|
|
{
|
2022-04-27 18:00:40 +02:00
|
|
|
|
textBlock = (TextBlock)Children[childrenCount];
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
textBlock = new TextBlock { RenderTransform = new MatrixTransform() };
|
2024-05-26 21:25:36 +02:00
|
|
|
|
|
|
|
|
|
|
textBlock.SetBinding(TextBlock.FontSizeProperty,
|
|
|
|
|
|
new Binding { Source = this, Path = new PropertyPath(nameof(FontSize)) });
|
|
|
|
|
|
|
|
|
|
|
|
textBlock.SetBinding(TextBlock.ForegroundProperty,
|
|
|
|
|
|
new Binding { Source = this, Path = new PropertyPath(nameof(Foreground)) });
|
2022-04-27 18:00:40 +02:00
|
|
|
|
|
|
|
|
|
|
if (FontFamily != null)
|
2018-03-06 22:22:58 +01:00
|
|
|
|
{
|
2024-05-26 21:25:36 +02:00
|
|
|
|
textBlock.SetBinding(TextBlock.FontFamilyProperty,
|
|
|
|
|
|
new Binding { Source = this, Path = new PropertyPath(nameof(FontFamily)) });
|
2022-04-27 18:00:40 +02:00
|
|
|
|
}
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
Children.Add(textBlock);
|
2018-03-06 22:22:58 +01:00
|
|
|
|
}
|
2013-11-18 19:49:17 +01:00
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
textBlock.Text = label.LatitudeText + "\n" + label.LongitudeText;
|
|
|
|
|
|
textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
|
2013-11-18 19:49:17 +01:00
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
var matrix = new Matrix(1, 0, 0, 1, 0, 0);
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
matrix.Translate(StrokeThickness / 2d + 2d, -textBlock.DesiredSize.Height / 2d);
|
|
|
|
|
|
matrix.Rotate(label.Rotation);
|
2022-04-28 18:34:30 +02:00
|
|
|
|
matrix.Translate(label.X, label.Y);
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
((MatrixTransform)textBlock.RenderTransform).Matrix = matrix;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
childrenCount++;
|
|
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
while (Children.Count > childrenCount)
|
|
|
|
|
|
{
|
|
|
|
|
|
Children.RemoveAt(Children.Count - 1);
|
|
|
|
|
|
}
|
2014-07-01 18:57:44 +02:00
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
base.OnViewportChanged(e);
|
|
|
|
|
|
}
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2022-04-28 18:34:30 +02:00
|
|
|
|
private static PathFigure CreatePolylineFigure(IEnumerable<Point> points)
|
2022-04-27 18:00:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
var figure = new PathFigure
|
|
|
|
|
|
{
|
|
|
|
|
|
StartPoint = points.First(),
|
2024-05-29 12:02:55 +02:00
|
|
|
|
IsClosed = false,
|
2022-04-27 18:00:40 +02:00
|
|
|
|
IsFilled = false
|
|
|
|
|
|
};
|
2020-03-28 21:53:38 +01:00
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
var polyline = new PolyLineSegment();
|
2020-03-28 21:53:38 +01:00
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
foreach (var p in points.Skip(1))
|
2014-07-09 21:27:28 +02:00
|
|
|
|
{
|
2022-04-27 18:00:40 +02:00
|
|
|
|
polyline.Points.Add(p);
|
2014-07-09 21:27:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
figure.Segments.Add(polyline);
|
|
|
|
|
|
return figure;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|