2012-11-22 21:42:29 +01:00
|
|
|
|
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
|
2015-01-20 17:52:02 +01:00
|
|
|
|
// © 2015 Clemens Fischer
|
2012-10-25 08:42:51 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2015-08-25 20:04:33 +02:00
|
|
|
|
using System.Globalization;
|
2012-10-25 08:42:51 +02:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Draws a map scale overlay.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class MapScale : MapOverlay
|
|
|
|
|
|
{
|
|
|
|
|
|
public static readonly DependencyProperty PaddingProperty = Control.PaddingProperty.AddOwner(
|
2013-04-12 19:59:16 +02:00
|
|
|
|
typeof(MapScale), new FrameworkPropertyMetadata(new Thickness(2d)));
|
2012-10-25 08:42:51 +02:00
|
|
|
|
|
|
|
|
|
|
private double length;
|
|
|
|
|
|
private Size size;
|
|
|
|
|
|
|
|
|
|
|
|
static MapScale()
|
|
|
|
|
|
{
|
2015-11-11 19:48:50 +01:00
|
|
|
|
IsHitTestVisibleProperty.OverrideMetadata(
|
2013-04-12 19:59:16 +02:00
|
|
|
|
typeof(MapScale), new FrameworkPropertyMetadata(false));
|
|
|
|
|
|
|
2015-11-11 19:48:50 +01:00
|
|
|
|
MinWidthProperty.OverrideMetadata(
|
2012-10-25 08:42:51 +02:00
|
|
|
|
typeof(MapScale), new FrameworkPropertyMetadata(100d));
|
|
|
|
|
|
|
2015-11-11 19:48:50 +01:00
|
|
|
|
HorizontalAlignmentProperty.OverrideMetadata(
|
2012-10-25 08:42:51 +02:00
|
|
|
|
typeof(MapScale), new FrameworkPropertyMetadata(HorizontalAlignment.Right));
|
|
|
|
|
|
|
2015-11-11 19:48:50 +01:00
|
|
|
|
VerticalAlignmentProperty.OverrideMetadata(
|
2012-10-25 08:42:51 +02:00
|
|
|
|
typeof(MapScale), new FrameworkPropertyMetadata(VerticalAlignment.Bottom));
|
|
|
|
|
|
|
2015-11-11 19:48:50 +01:00
|
|
|
|
StrokeStartLineCapProperty.OverrideMetadata(
|
2012-10-25 08:42:51 +02:00
|
|
|
|
typeof(MapScale), new FrameworkPropertyMetadata(PenLineCap.Round));
|
|
|
|
|
|
|
2015-11-11 19:48:50 +01:00
|
|
|
|
StrokeEndLineCapProperty.OverrideMetadata(
|
2012-10-25 08:42:51 +02:00
|
|
|
|
typeof(MapScale), new FrameworkPropertyMetadata(PenLineCap.Round));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Thickness Padding
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (Thickness)GetValue(PaddingProperty); }
|
|
|
|
|
|
set { SetValue(PaddingProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override Size MeasureOverride(Size availableSize)
|
|
|
|
|
|
{
|
2013-04-12 19:59:16 +02:00
|
|
|
|
if (ParentMap != null && ParentMap.CenterScale > 0d)
|
2012-10-25 08:42:51 +02:00
|
|
|
|
{
|
2013-04-12 19:59:16 +02:00
|
|
|
|
length = MinWidth / ParentMap.CenterScale;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
var magnitude = Math.Pow(10d, Math.Floor(Math.Log10(length)));
|
|
|
|
|
|
|
|
|
|
|
|
if (length / magnitude < 2d)
|
|
|
|
|
|
{
|
|
|
|
|
|
length = 2d * magnitude;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (length / magnitude < 5d)
|
|
|
|
|
|
{
|
|
|
|
|
|
length = 5d * magnitude;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
length = 10d * magnitude;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-04-12 19:59:16 +02:00
|
|
|
|
size.Width = length * ParentMap.CenterScale + StrokeThickness + Padding.Left + Padding.Right;
|
2015-08-25 20:04:33 +02:00
|
|
|
|
size.Height = FontSize * FontFamily.LineSpacing + StrokeThickness + Padding.Top + Padding.Bottom;
|
2012-10-25 08:42:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
size.Width = size.Height = 0d;
|
2012-10-25 08:42:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return size;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnRender(DrawingContext drawingContext)
|
|
|
|
|
|
{
|
2013-04-12 19:59:16 +02:00
|
|
|
|
if (ParentMap != null)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
|
|
|
|
|
var x1 = Padding.Left + StrokeThickness / 2d;
|
|
|
|
|
|
var x2 = size.Width - Padding.Right - StrokeThickness / 2d;
|
|
|
|
|
|
var y1 = size.Height / 2d;
|
|
|
|
|
|
var y2 = size.Height - Padding.Bottom - StrokeThickness / 2d;
|
2015-08-25 20:04:33 +02:00
|
|
|
|
var text = new FormattedText(
|
|
|
|
|
|
length >= 1000d ? string.Format("{0:0} km", length / 1000d) : string.Format("{0:0} m", length),
|
|
|
|
|
|
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, Typeface, FontSize, Foreground);
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2013-04-12 19:59:16 +02:00
|
|
|
|
drawingContext.DrawRectangle(Background ?? ParentMap.Background, null, new Rect(size));
|
2012-11-22 21:42:29 +01:00
|
|
|
|
drawingContext.DrawLine(Pen, new Point(x1, y1), new Point(x1, y2));
|
|
|
|
|
|
drawingContext.DrawLine(Pen, new Point(x2, y1), new Point(x2, y2));
|
|
|
|
|
|
drawingContext.DrawLine(Pen, new Point(x1, y2), new Point(x2, y2));
|
2015-08-25 20:04:33 +02:00
|
|
|
|
drawingContext.DrawText(text, new Point((size.Width - text.Width) / 2d, 0d));
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
2012-10-25 08:42:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnViewportChanged()
|
|
|
|
|
|
{
|
|
|
|
|
|
InvalidateMeasure();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|