2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2022-01-14 20:22:56 +01:00
|
|
|
|
// © 2022 Clemens Fischer
|
2012-11-22 21:42:29 +01:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
2013-10-22 18:18:47 +02:00
|
|
|
|
using System.Collections.Generic;
|
2015-08-25 20:04:33 +02:00
|
|
|
|
using System.Globalization;
|
2022-03-04 22:28:18 +01:00
|
|
|
|
using System.Linq;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
public partial class MapGraticule
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2013-04-12 19:59:16 +02:00
|
|
|
|
static MapGraticule()
|
|
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
StrokeThicknessProperty.OverrideMetadata(typeof(MapGraticule), new FrameworkPropertyMetadata(0.5));
|
2013-04-12 19:59:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
private readonly PathGeometry pathGeometry = new PathGeometry();
|
|
|
|
|
|
|
2017-02-05 18:35:30 +01:00
|
|
|
|
protected override void OnViewportChanged(ViewportChangedEventArgs e)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2013-04-12 19:59:16 +02:00
|
|
|
|
InvalidateVisual();
|
|
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2013-04-12 19:59:16 +02:00
|
|
|
|
protected override void OnRender(DrawingContext drawingContext)
|
|
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
var projection = ParentMap?.MapProjection;
|
|
|
|
|
|
|
2018-02-11 19:46:31 +01:00
|
|
|
|
if (projection != null)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2022-04-27 18:00:40 +02:00
|
|
|
|
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
|
|
|
|
|
|
var pixelsPerDip = VisualTreeHelper.GetDpi(this).PixelsPerDip;
|
|
|
|
|
|
var pathGeometry = new PathGeometry();
|
|
|
|
|
|
var labels = new List<Label>();
|
2018-02-11 19:46:31 +01:00
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
SetLineDistance();
|
2022-03-04 22:28:18 +01:00
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
if (projection.Type <= MapProjectionType.NormalCylindrical)
|
2022-03-04 22:28:18 +01:00
|
|
|
|
{
|
2022-04-27 18:00:40 +02:00
|
|
|
|
DrawCylindricalGraticule(pathGeometry.Figures, labels);
|
2022-03-04 22:28:18 +01:00
|
|
|
|
}
|
2022-04-27 18:00:40 +02:00
|
|
|
|
else
|
2022-03-04 22:28:18 +01:00
|
|
|
|
{
|
2022-04-27 18:00:40 +02:00
|
|
|
|
DrawGraticule(pathGeometry.Figures, labels);
|
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-27 18:00:40 +02:00
|
|
|
|
foreach (var label in labels)
|
2022-03-04 22:28:18 +01:00
|
|
|
|
{
|
2022-04-27 18:00:40 +02:00
|
|
|
|
var latText = new FormattedText(label.LatitudeText,
|
|
|
|
|
|
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip);
|
2022-03-04 22:28:18 +01:00
|
|
|
|
|
2022-04-27 18:00:40 +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-27 18:00:40 +02:00
|
|
|
|
var x = label.Position.X + StrokeThickness / 2d + 2d;
|
|
|
|
|
|
var y1 = label.Position.Y - StrokeThickness / 2d - latText.Height;
|
|
|
|
|
|
var y2 = label.Position.Y + StrokeThickness / 2d;
|
2022-03-04 22:28:18 +01:00
|
|
|
|
|
2022-04-27 18:00:40 +02:00
|
|
|
|
drawingContext.PushTransform(new RotateTransform(label.Rotation, label.Position.X, label.Position.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-27 18:00:40 +02:00
|
|
|
|
private static PathFigure CreatePolylineFigure(ICollection<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;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|