2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2020-01-09 19:40:10 +01:00
|
|
|
|
// © 2020 Clemens Fischer
|
2012-11-22 21:42:29 +01:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2013-10-22 18:18:47 +02:00
|
|
|
|
using System.Collections.Generic;
|
2015-08-25 20:04:33 +02:00
|
|
|
|
using System.Globalization;
|
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-11-21 21:16:29 +01:00
|
|
|
|
private class Label
|
2013-10-22 18:18:47 +02:00
|
|
|
|
{
|
2013-11-21 21:16:29 +01:00
|
|
|
|
public readonly double Position;
|
2015-08-25 20:04:33 +02:00
|
|
|
|
public readonly FormattedText Text;
|
2013-10-22 18:18:47 +02:00
|
|
|
|
|
2015-08-25 20:04:33 +02:00
|
|
|
|
public Label(double position, FormattedText text)
|
2013-10-22 18:18:47 +02:00
|
|
|
|
{
|
2013-11-21 21:16:29 +01:00
|
|
|
|
Position = position;
|
2013-10-22 18:18:47 +02:00
|
|
|
|
Text = text;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-04-12 19:59:16 +02:00
|
|
|
|
static MapGraticule()
|
|
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
IsHitTestVisibleProperty.OverrideMetadata(typeof(MapGraticule), new FrameworkPropertyMetadata(false));
|
|
|
|
|
|
StrokeThicknessProperty.OverrideMetadata(typeof(MapGraticule), new FrameworkPropertyMetadata(0.5));
|
2013-04-12 19:59:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2018-02-11 19:46:31 +01:00
|
|
|
|
var lineDistance = GetLineDistance();
|
|
|
|
|
|
var labelFormat = GetLabelFormat(lineDistance);
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2018-12-20 21:55:12 +01:00
|
|
|
|
if (projection.IsNormalCylindrical)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2020-03-26 19:08:20 +01:00
|
|
|
|
DrawCylindricalGraticule(drawingContext, lineDistance, labelFormat);
|
2018-02-11 19:46:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
private void DrawCylindricalGraticule(DrawingContext drawingContext, double lineDistance, string labelFormat)
|
2018-02-11 19:46:31 +01:00
|
|
|
|
{
|
2020-03-26 19:08:20 +01:00
|
|
|
|
var boundingBox = ParentMap.ViewportRectToBoundingBox(new Rect(ParentMap.RenderSize));
|
2018-12-20 21:55:12 +01:00
|
|
|
|
var latLabelStart = Math.Ceiling(boundingBox.South / lineDistance) * lineDistance;
|
|
|
|
|
|
var lonLabelStart = Math.Ceiling(boundingBox.West / lineDistance) * lineDistance;
|
|
|
|
|
|
var latLabels = new List<Label>((int)((boundingBox.North - latLabelStart) / lineDistance) + 1);
|
|
|
|
|
|
var lonLabels = new List<Label>((int)((boundingBox.East - lonLabelStart) / lineDistance) + 1);
|
|
|
|
|
|
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
|
2020-01-28 22:09:29 +01:00
|
|
|
|
var pixelsPerDip = VisualTreeHelper.GetDpi(this).PixelsPerDip;
|
2018-12-20 21:55:12 +01:00
|
|
|
|
var pen = CreatePen();
|
|
|
|
|
|
|
|
|
|
|
|
for (var lat = latLabelStart; lat <= boundingBox.North; lat += lineDistance)
|
2018-02-11 19:46:31 +01:00
|
|
|
|
{
|
2018-12-20 21:55:12 +01:00
|
|
|
|
latLabels.Add(new Label(lat, new FormattedText(
|
|
|
|
|
|
GetLabelText(lat, labelFormat, "NS"),
|
2020-01-28 22:09:29 +01:00
|
|
|
|
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip)));
|
2017-10-27 17:15:18 +02:00
|
|
|
|
|
2018-12-20 21:55:12 +01:00
|
|
|
|
drawingContext.DrawLine(pen,
|
2020-03-26 19:08:20 +01:00
|
|
|
|
ParentMap.LocationToViewportPoint(new Location(lat, boundingBox.West)),
|
|
|
|
|
|
ParentMap.LocationToViewportPoint(new Location(lat, boundingBox.East)));
|
2018-12-20 21:55:12 +01:00
|
|
|
|
}
|
2018-02-11 19:46:31 +01:00
|
|
|
|
|
2018-12-20 21:55:12 +01:00
|
|
|
|
for (var lon = lonLabelStart; lon <= boundingBox.East; lon += lineDistance)
|
|
|
|
|
|
{
|
|
|
|
|
|
lonLabels.Add(new Label(lon, new FormattedText(
|
|
|
|
|
|
GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW"),
|
2020-01-28 22:09:29 +01:00
|
|
|
|
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip)));
|
2018-02-11 19:46:31 +01:00
|
|
|
|
|
2018-12-20 21:55:12 +01:00
|
|
|
|
drawingContext.DrawLine(pen,
|
2020-03-26 19:08:20 +01:00
|
|
|
|
ParentMap.LocationToViewportPoint(new Location(boundingBox.South, lon)),
|
|
|
|
|
|
ParentMap.LocationToViewportPoint(new Location(boundingBox.North, lon)));
|
2018-12-20 21:55:12 +01:00
|
|
|
|
}
|
2018-02-11 19:46:31 +01:00
|
|
|
|
|
2018-12-20 21:55:12 +01:00
|
|
|
|
foreach (var latLabel in latLabels)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var lonLabel in lonLabels)
|
2018-02-11 19:46:31 +01:00
|
|
|
|
{
|
2020-03-26 19:08:20 +01:00
|
|
|
|
var position = ParentMap.LocationToViewportPoint(new Location(latLabel.Position, lonLabel.Position));
|
2018-12-20 21:55:12 +01:00
|
|
|
|
|
|
|
|
|
|
drawingContext.PushTransform(new RotateTransform(ParentMap.Heading, position.X, position.Y));
|
|
|
|
|
|
drawingContext.DrawText(latLabel.Text,
|
|
|
|
|
|
new Point(position.X + StrokeThickness / 2d + 2d, position.Y - StrokeThickness / 2d - latLabel.Text.Height));
|
|
|
|
|
|
drawingContext.DrawText(lonLabel.Text,
|
|
|
|
|
|
new Point(position.X + StrokeThickness / 2d + 2d, position.Y + StrokeThickness / 2d));
|
|
|
|
|
|
drawingContext.Pop();
|
2013-10-22 18:18:47 +02:00
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|