XAML-Map-Control/MapControl/WPF/MapGraticule.WPF.cs

74 lines
2.6 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2024-02-03 21:01:53 +01:00
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System.Collections.Generic;
using System.Globalization;
2022-03-04 22:28:18 +01:00
using System.Linq;
using System.Windows;
using System.Windows.Media;
namespace MapControl
{
public partial class MapGraticule
{
static MapGraticule()
{
StrokeThicknessProperty.OverrideMetadata(typeof(MapGraticule), new FrameworkPropertyMetadata(0.5));
}
protected override void OnViewportChanged(ViewportChangedEventArgs e)
{
InvalidateVisual();
}
protected override void OnRender(DrawingContext drawingContext)
{
2022-04-28 18:34:30 +02:00
if (ParentMap != null)
{
2022-04-27 18:00:40 +02:00
var pathGeometry = new PathGeometry();
2022-04-28 18:34:30 +02:00
var labels = DrawGraticule(pathGeometry.Figures);
2022-03-04 22:28:18 +01:00
2022-04-27 18:00:40 +02:00
drawingContext.DrawGeometry(null, CreatePen(), pathGeometry);
2022-03-04 22:28:18 +01:00
2022-04-28 18:34:30 +02:00
if (labels.Count > 0)
2022-03-04 22:28:18 +01:00
{
2022-04-28 18:34:30 +02:00
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
var pixelsPerDip = VisualTreeHelper.GetDpi(this).PixelsPerDip;
foreach (var label in labels)
{
var latText = new FormattedText(label.LatitudeText,
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip);
2022-03-04 22:28:18 +01:00
2022-04-28 18:34:30 +02:00
var lonText = new FormattedText(label.LongitudeText,
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip);
2022-03-04 22:28:18 +01:00
2022-04-28 18:34:30 +02:00
var x = label.X + StrokeThickness / 2d + 2d;
var y1 = label.Y - StrokeThickness / 2d - latText.Height;
var y2 = label.Y + StrokeThickness / 2d;
2022-03-04 22:28:18 +01:00
2022-04-28 18:34:30 +02:00
drawingContext.PushTransform(new RotateTransform(label.Rotation, label.X, label.Y));
drawingContext.DrawText(latText, new Point(x, y1));
drawingContext.DrawText(lonText, new Point(x, y2));
drawingContext.Pop();
}
2022-03-05 18:40:57 +01:00
}
2022-03-04 22:28:18 +01:00
}
}
2022-04-28 18:34:30 +02:00
private static PathFigure CreatePolylineFigure(IEnumerable<Point> points)
2022-03-04 22:28:18 +01:00
{
2022-04-27 18:00:40 +02:00
var figure = new PathFigure
2022-03-04 22:28:18 +01:00
{
2022-04-27 18:00:40 +02:00
StartPoint = points.First(),
IsFilled = false
};
2022-03-04 22:28:18 +01:00
2022-04-27 18:00:40 +02:00
figure.Segments.Add(new PolyLineSegment(points.Skip(1), true));
return figure;
}
}
}