2012-11-22 21:42:29 +01:00
|
|
|
|
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
|
2016-02-23 20:07:30 +01:00
|
|
|
|
// © 2016 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()
|
|
|
|
|
|
{
|
2015-11-11 19:48:50 +01:00
|
|
|
|
IsHitTestVisibleProperty.OverrideMetadata(
|
2013-04-12 19:59:16 +02:00
|
|
|
|
typeof(MapGraticule), new FrameworkPropertyMetadata(false));
|
|
|
|
|
|
|
2015-11-11 19:48:50 +01:00
|
|
|
|
StrokeThicknessProperty.OverrideMetadata(
|
2015-08-25 20:04:33 +02:00
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ParentMap != null)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2013-04-12 19:59:16 +02:00
|
|
|
|
var bounds = ParentMap.ViewportTransform.Inverse.TransformBounds(new Rect(ParentMap.RenderSize));
|
2013-11-21 21:16:29 +01:00
|
|
|
|
var start = ParentMap.MapTransform.Transform(new Point(bounds.X, bounds.Y));
|
|
|
|
|
|
var end = ParentMap.MapTransform.Transform(new Point(bounds.X + bounds.Width, bounds.Y + bounds.Height));
|
2015-11-11 19:48:50 +01:00
|
|
|
|
var lineDistance = GetLineDistance();
|
|
|
|
|
|
var labelFormat = GetLabelFormat(lineDistance);
|
|
|
|
|
|
var latLabelStart = Math.Ceiling(start.Latitude / lineDistance) * lineDistance;
|
|
|
|
|
|
var lonLabelStart = Math.Ceiling(start.Longitude / lineDistance) * lineDistance;
|
|
|
|
|
|
var latLabels = new List<Label>((int)((end.Latitude - latLabelStart) / lineDistance) + 1);
|
|
|
|
|
|
var lonLabels = new List<Label>((int)((end.Longitude - lonLabelStart) / lineDistance) + 1);
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2015-11-11 19:48:50 +01:00
|
|
|
|
for (var lat = latLabelStart; lat <= end.Latitude; lat += lineDistance)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2015-08-25 20:04:33 +02:00
|
|
|
|
latLabels.Add(new Label(lat, new FormattedText(
|
2015-11-11 19:48:50 +01:00
|
|
|
|
GetLabelText(lat, labelFormat, "NS"),
|
2015-08-25 20:04:33 +02:00
|
|
|
|
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, Typeface, FontSize, Foreground)));
|
2013-10-22 18:18:47 +02:00
|
|
|
|
|
2013-11-21 21:16:29 +01:00
|
|
|
|
drawingContext.DrawLine(Pen,
|
|
|
|
|
|
ParentMap.LocationToViewportPoint(new Location(lat, start.Longitude)),
|
|
|
|
|
|
ParentMap.LocationToViewportPoint(new Location(lat, end.Longitude)));
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-11 19:48:50 +01:00
|
|
|
|
for (var lon = lonLabelStart; lon <= end.Longitude; lon += lineDistance)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2015-08-25 20:04:33 +02:00
|
|
|
|
lonLabels.Add(new Label(lon, new FormattedText(
|
2015-11-11 19:48:50 +01:00
|
|
|
|
GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW"),
|
2015-08-25 20:04:33 +02:00
|
|
|
|
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, Typeface, FontSize, Foreground)));
|
2013-10-22 18:18:47 +02:00
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
drawingContext.DrawLine(Pen,
|
2013-11-21 21:16:29 +01:00
|
|
|
|
ParentMap.LocationToViewportPoint(new Location(start.Latitude, lon)),
|
|
|
|
|
|
ParentMap.LocationToViewportPoint(new Location(end.Latitude, lon)));
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-08-25 20:04:33 +02:00
|
|
|
|
foreach (var latLabel in latLabels)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2015-08-25 20:04:33 +02:00
|
|
|
|
foreach (var lonLabel in lonLabels)
|
2013-10-22 18:18:47 +02:00
|
|
|
|
{
|
2015-08-25 20:04:33 +02:00
|
|
|
|
var position = ParentMap.LocationToViewportPoint(new Location(latLabel.Position, lonLabel.Position));
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|