2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2023-01-03 15:12:53 +01:00
|
|
|
|
// Copyright © 2023 Clemens Fischer
|
2012-11-22 21:42:29 +01:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using Windows.Foundation;
|
2022-04-27 18:00:40 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2021-06-14 21:41:37 +02:00
|
|
|
|
#if WINUI
|
|
|
|
|
|
using Microsoft.UI.Xaml.Controls;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Media;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Shapes;
|
|
|
|
|
|
#else
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using Windows.UI.Xaml.Controls;
|
|
|
|
|
|
using Windows.UI.Xaml.Media;
|
2013-04-12 19:59:16 +02:00
|
|
|
|
using Windows.UI.Xaml.Shapes;
|
2021-06-14 21:41:37 +02:00
|
|
|
|
#endif
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
public partial class MapGraticule
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
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
|
|
|
|
|
2013-04-12 19:59:16 +02:00
|
|
|
|
public MapGraticule()
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2014-07-22 20:02:30 +02:00
|
|
|
|
StrokeThickness = 0.5;
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
path.SetBinding(Shape.StrokeProperty, this.GetOrCreateBinding(StrokeProperty, nameof(Stroke)));
|
|
|
|
|
|
path.SetBinding(Shape.StrokeThicknessProperty, this.GetOrCreateBinding(StrokeThicknessProperty, nameof(StrokeThickness)));
|
|
|
|
|
|
path.SetBinding(Shape.StrokeDashArrayProperty, this.GetOrCreateBinding(StrokeDashArrayProperty, nameof(StrokeDashArray)));
|
|
|
|
|
|
path.SetBinding(Shape.StrokeDashOffsetProperty, this.GetOrCreateBinding(StrokeDashOffsetProperty, nameof(StrokeDashOffset)));
|
|
|
|
|
|
path.SetBinding(Shape.StrokeDashCapProperty, this.GetOrCreateBinding(StrokeDashCapProperty, nameof(StrokeDashCap)));
|
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() };
|
|
|
|
|
|
textBlock.SetBinding(TextBlock.FontSizeProperty, this.GetOrCreateBinding(FontSizeProperty, nameof(FontSize)));
|
|
|
|
|
|
textBlock.SetBinding(TextBlock.FontStyleProperty, this.GetOrCreateBinding(FontStyleProperty, nameof(FontStyle)));
|
|
|
|
|
|
textBlock.SetBinding(TextBlock.FontStretchProperty, this.GetOrCreateBinding(FontStretchProperty, nameof(FontStretch)));
|
|
|
|
|
|
textBlock.SetBinding(TextBlock.FontWeightProperty, this.GetOrCreateBinding(FontWeightProperty, nameof(FontWeight)));
|
|
|
|
|
|
textBlock.SetBinding(TextBlock.ForegroundProperty, this.GetOrCreateBinding(ForegroundProperty, nameof(Foreground)));
|
|
|
|
|
|
|
|
|
|
|
|
if (FontFamily != null)
|
2018-03-06 22:22:58 +01:00
|
|
|
|
{
|
2022-04-27 18:00:40 +02:00
|
|
|
|
textBlock.SetBinding(TextBlock.FontFamilyProperty, this.GetOrCreateBinding(FontFamilyProperty, nameof(FontFamily)));
|
|
|
|
|
|
}
|
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(),
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|