XAML-Map-Control/MapControl/Shared/MapScale.cs

117 lines
3.8 KiB
C#
Raw Normal View History

2017-08-23 23:33:27 +02:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2022-01-14 20:22:56 +01:00
// © 2022 Clemens Fischer
2017-08-23 23:33:27 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
2021-07-05 00:04:07 +02:00
using System.Globalization;
2021-06-14 21:41:37 +02:00
#if WINUI
using Windows.Foundation;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Shapes;
2021-11-17 23:17:11 +01:00
#elif UWP
2017-08-23 23:33:27 +02:00
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
#else
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
#endif
namespace MapControl
{
/// <summary>
/// Draws a map scale overlay.
/// </summary>
public class MapScale : MapOverlay
{
public static readonly DependencyProperty PaddingProperty = DependencyProperty.Register(
nameof(Padding), typeof(Thickness), typeof(MapScale), new PropertyMetadata(new Thickness(4)));
2017-08-23 23:33:27 +02:00
private readonly Polyline line = new Polyline();
private readonly TextBlock label = new TextBlock
{
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
TextAlignment = TextAlignment.Center
};
2017-08-23 23:33:27 +02:00
public MapScale()
{
MinWidth = 100d;
2021-12-01 20:39:42 +01:00
Children.Add(line);
Children.Add(label);
}
protected override void SetParentMap(MapBase map)
{
base.SetParentMap(map);
2021-12-01 20:15:48 +01:00
line.SetBinding(Shape.StrokeProperty, this.GetOrCreateBinding(StrokeProperty, nameof(Stroke)));
line.SetBinding(Shape.StrokeThicknessProperty, this.GetOrCreateBinding(StrokeThicknessProperty, nameof(StrokeThickness)));
2021-11-17 23:17:11 +01:00
#if WINUI || UWP
2021-12-01 20:15:48 +01:00
label.SetBinding(TextBlock.ForegroundProperty, this.GetOrCreateBinding(ForegroundProperty, nameof(Foreground)));
#endif
2017-08-23 23:33:27 +02:00
}
public Thickness Padding
{
get { return (Thickness)GetValue(PaddingProperty); }
set { SetValue(PaddingProperty, value); }
}
protected override Size MeasureOverride(Size availableSize)
{
var size = new Size();
if (ParentMap != null)
2017-08-23 23:33:27 +02:00
{
var scale = ParentMap.GetScale(ParentMap.Center).X;
var length = MinWidth / scale;
2017-08-23 23:33:27 +02:00
var magnitude = Math.Pow(10d, Math.Floor(Math.Log10(length)));
2021-07-05 00:04:07 +02:00
length = length / magnitude < 2d ? 2d * magnitude
: length / magnitude < 5d ? 5d * magnitude
: 10d * magnitude;
2017-08-23 23:33:27 +02:00
size.Width = length * scale + StrokeThickness + Padding.Left + Padding.Right;
2017-08-23 23:33:27 +02:00
size.Height = 1.25 * FontSize + StrokeThickness + Padding.Top + Padding.Bottom;
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;
line.Points = new PointCollection
{
new Point(x1, y1),
new Point(x1, y2),
new Point(x2, y2),
new Point(x2, y1)
};
2017-08-23 23:33:27 +02:00
line.Measure(size);
2021-07-05 00:04:07 +02:00
label.Text = length >= 1000d
? string.Format(CultureInfo.InvariantCulture, "{0:0} km", length / 1000d)
: string.Format(CultureInfo.InvariantCulture, "{0:0} m", length);
2017-08-23 23:33:27 +02:00
label.Width = size.Width;
label.Height = size.Height;
label.Measure(size);
}
return size;
}
protected override void OnViewportChanged(ViewportChangedEventArgs e)
{
InvalidateMeasure();
}
}
}