2012-11-22 21:42:29 +01:00
|
|
|
|
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
|
2013-01-17 18:48:38 +01:00
|
|
|
|
// Copyright © 2013 Clemens Fischer
|
2012-11-22 21:42:29 +01:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2013-04-12 19:59:16 +02:00
|
|
|
|
public partial class MapGraticule : MapOverlay
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2013-04-12 19:59:16 +02:00
|
|
|
|
static MapGraticule()
|
|
|
|
|
|
{
|
|
|
|
|
|
UIElement.IsHitTestVisibleProperty.OverrideMetadata(
|
|
|
|
|
|
typeof(MapGraticule), new FrameworkPropertyMetadata(false));
|
|
|
|
|
|
|
|
|
|
|
|
MapOverlay.StrokeThicknessProperty.OverrideMetadata(
|
|
|
|
|
|
typeof(MapGraticule), new FrameworkPropertyMetadata(0.5));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnViewportChanged()
|
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));
|
|
|
|
|
|
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));
|
|
|
|
|
|
var minSpacing = MinLineSpacing * 360d / (Math.Pow(2d, ParentMap.ZoomLevel) * 256d);
|
2012-11-22 21:42:29 +01:00
|
|
|
|
var spacing = LineSpacings[LineSpacings.Length - 1];
|
|
|
|
|
|
|
|
|
|
|
|
if (spacing >= minSpacing)
|
|
|
|
|
|
{
|
|
|
|
|
|
spacing = LineSpacings.FirstOrDefault(s => s >= minSpacing);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var labelsStart = new Location(
|
|
|
|
|
|
Math.Ceiling(start.Latitude / spacing) * spacing,
|
|
|
|
|
|
Math.Ceiling(start.Longitude / spacing) * spacing);
|
|
|
|
|
|
|
|
|
|
|
|
for (var lat = labelsStart.Latitude; lat <= end.Latitude; lat += spacing)
|
|
|
|
|
|
{
|
|
|
|
|
|
drawingContext.DrawLine(Pen,
|
2013-04-12 19:59:16 +02:00
|
|
|
|
ParentMap.LocationToViewportPoint(new Location(lat, start.Longitude)),
|
|
|
|
|
|
ParentMap.LocationToViewportPoint(new Location(lat, end.Longitude)));
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (var lon = labelsStart.Longitude; lon <= end.Longitude; lon += spacing)
|
|
|
|
|
|
{
|
|
|
|
|
|
drawingContext.DrawLine(Pen,
|
2013-04-12 19:59:16 +02:00
|
|
|
|
ParentMap.LocationToViewportPoint(new Location(start.Latitude, lon)),
|
|
|
|
|
|
ParentMap.LocationToViewportPoint(new Location(end.Latitude, lon)));
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (Foreground != null && Foreground != Brushes.Transparent)
|
|
|
|
|
|
{
|
|
|
|
|
|
var format = spacing < 1d ? "{0} {1}°{2:00}'" : "{0} {1}°";
|
|
|
|
|
|
|
|
|
|
|
|
for (var lat = labelsStart.Latitude; lat <= end.Latitude; lat += spacing)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (var lon = labelsStart.Longitude; lon <= end.Longitude; lon += spacing)
|
|
|
|
|
|
{
|
|
|
|
|
|
var t = StrokeThickness / 2d;
|
2013-04-12 19:59:16 +02:00
|
|
|
|
var p = ParentMap.LocationToViewportPoint(new Location(lat, lon));
|
2012-11-22 21:42:29 +01:00
|
|
|
|
var latPos = new Point(p.X + t + 2d, p.Y - t - FontSize / 4d);
|
|
|
|
|
|
var lonPos = new Point(p.X + t + 2d, p.Y + t + FontSize);
|
|
|
|
|
|
var latLabel = CoordinateString(lat, format, "NS");
|
|
|
|
|
|
var lonLabel = CoordinateString(Location.NormalizeLongitude(lon), format, "EW");
|
|
|
|
|
|
|
2013-04-12 19:59:16 +02:00
|
|
|
|
drawingContext.PushTransform(new RotateTransform(ParentMap.Heading, p.X, p.Y));
|
2012-11-22 21:42:29 +01:00
|
|
|
|
drawingContext.DrawGlyphRun(Foreground, GlyphRunText.Create(latLabel, Typeface, FontSize, latPos));
|
|
|
|
|
|
drawingContext.DrawGlyphRun(Foreground, GlyphRunText.Create(lonLabel, Typeface, FontSize, lonPos));
|
|
|
|
|
|
drawingContext.Pop();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|