2012-11-22 21:42:29 +01:00
|
|
|
|
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
|
2014-01-10 20:11:39 +01:00
|
|
|
|
// Copyright © 2014 Clemens Fischer
|
2012-10-25 08:42:51 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
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()
|
|
|
|
|
|
{
|
2013-04-12 19:59:16 +02:00
|
|
|
|
UIElement.IsHitTestVisibleProperty.OverrideMetadata(
|
|
|
|
|
|
typeof(MapScale), new FrameworkPropertyMetadata(false));
|
|
|
|
|
|
|
2012-10-25 08:42:51 +02:00
|
|
|
|
FrameworkElement.MinWidthProperty.OverrideMetadata(
|
|
|
|
|
|
typeof(MapScale), new FrameworkPropertyMetadata(100d));
|
|
|
|
|
|
|
|
|
|
|
|
FrameworkElement.HorizontalAlignmentProperty.OverrideMetadata(
|
|
|
|
|
|
typeof(MapScale), new FrameworkPropertyMetadata(HorizontalAlignment.Right));
|
|
|
|
|
|
|
|
|
|
|
|
FrameworkElement.VerticalAlignmentProperty.OverrideMetadata(
|
|
|
|
|
|
typeof(MapScale), new FrameworkPropertyMetadata(VerticalAlignment.Bottom));
|
|
|
|
|
|
|
|
|
|
|
|
MapOverlay.StrokeStartLineCapProperty.OverrideMetadata(
|
|
|
|
|
|
typeof(MapScale), new FrameworkPropertyMetadata(PenLineCap.Round));
|
|
|
|
|
|
|
|
|
|
|
|
MapOverlay.StrokeEndLineCapProperty.OverrideMetadata(
|
|
|
|
|
|
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;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
size.Height = FontSize + 2d * 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;
|
|
|
|
|
|
var text = length >= 1000d ? string.Format("{0:0} km", length / 1000d) : string.Format("{0:0} m", length);
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
drawingContext.DrawGlyphRun(Foreground,
|
2014-07-01 18:57:44 +02:00
|
|
|
|
GlyphRunText.Create(text, Typeface, FontSize),
|
|
|
|
|
|
new Point(size.Width / 2d, y1 - StrokeThickness - 1d),
|
|
|
|
|
|
HorizontalAlignment.Center, VerticalAlignment.Center);
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
2012-10-25 08:42:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnViewportChanged()
|
|
|
|
|
|
{
|
|
|
|
|
|
InvalidateMeasure();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|