2013-04-12 19:59:16 +02:00
|
|
|
|
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
|
2013-05-07 18:12:25 +02:00
|
|
|
|
// Copyright © Clemens Fischer 2012-2013
|
2013-04-12 19:59:16 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
#if NETFX_CORE
|
|
|
|
|
|
using Windows.Foundation;
|
|
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
|
using Windows.UI.Xaml.Media;
|
|
|
|
|
|
#else
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2013-05-07 18:12:25 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Fills a rectangular area defined by South, North, West and East with a Brush.
|
|
|
|
|
|
/// </summary>
|
2013-10-13 16:32:51 +02:00
|
|
|
|
public class MapRectangle : MapPath
|
2013-04-12 19:59:16 +02:00
|
|
|
|
{
|
2013-10-13 16:32:51 +02:00
|
|
|
|
private const double geometryScale = 1e6;
|
|
|
|
|
|
|
|
|
|
|
|
private static readonly ScaleTransform geometryScaleTransform = new ScaleTransform
|
|
|
|
|
|
{
|
|
|
|
|
|
ScaleX = 1d / geometryScale,
|
|
|
|
|
|
ScaleY = 1d / geometryScale
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static MapRectangle()
|
|
|
|
|
|
{
|
|
|
|
|
|
geometryScaleTransform.Freeze();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-04-12 19:59:16 +02:00
|
|
|
|
public static readonly DependencyProperty SouthProperty = DependencyProperty.Register(
|
|
|
|
|
|
"South", typeof(double), typeof(MapRectangle),
|
2013-05-25 08:53:50 +02:00
|
|
|
|
new PropertyMetadata(double.NaN, (o, e) => ((MapRectangle)o).UpdateData()));
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty NorthProperty = DependencyProperty.Register(
|
|
|
|
|
|
"North", typeof(double), typeof(MapRectangle),
|
2013-05-25 08:53:50 +02:00
|
|
|
|
new PropertyMetadata(double.NaN, (o, e) => ((MapRectangle)o).UpdateData()));
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty WestProperty = DependencyProperty.Register(
|
|
|
|
|
|
"West", typeof(double), typeof(MapRectangle),
|
2013-05-25 08:53:50 +02:00
|
|
|
|
new PropertyMetadata(double.NaN, (o, e) => ((MapRectangle)o).UpdateData()));
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty EastProperty = DependencyProperty.Register(
|
|
|
|
|
|
"East", typeof(double), typeof(MapRectangle),
|
2013-05-25 08:53:50 +02:00
|
|
|
|
new PropertyMetadata(double.NaN, (o, e) => ((MapRectangle)o).UpdateData()));
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2013-10-10 22:26:43 +02:00
|
|
|
|
public MapRectangle()
|
|
|
|
|
|
{
|
|
|
|
|
|
Data = new RectangleGeometry();
|
|
|
|
|
|
StrokeThickness = 0d;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-04-12 19:59:16 +02:00
|
|
|
|
public double South
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (double)GetValue(SouthProperty); }
|
|
|
|
|
|
set { SetValue(SouthProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public double North
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (double)GetValue(NorthProperty); }
|
|
|
|
|
|
set { SetValue(NorthProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public double West
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (double)GetValue(WestProperty); }
|
|
|
|
|
|
set { SetValue(WestProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public double East
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (double)GetValue(EastProperty); }
|
|
|
|
|
|
set { SetValue(EastProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-05-25 08:53:50 +02:00
|
|
|
|
protected override void UpdateData()
|
2013-04-12 19:59:16 +02:00
|
|
|
|
{
|
2013-10-13 16:32:51 +02:00
|
|
|
|
var geometry = (RectangleGeometry)Data;
|
|
|
|
|
|
|
2013-04-12 19:59:16 +02:00
|
|
|
|
if (ParentMap != null &&
|
|
|
|
|
|
!double.IsNaN(South) && !double.IsNaN(North) &&
|
|
|
|
|
|
!double.IsNaN(West) && !double.IsNaN(East) &&
|
|
|
|
|
|
South < North && West < East)
|
|
|
|
|
|
{
|
|
|
|
|
|
var p1 = ParentMap.MapTransform.Transform(new Location(South, West));
|
|
|
|
|
|
var p2 = ParentMap.MapTransform.Transform(new Location(North, East));
|
|
|
|
|
|
|
2013-10-13 16:32:51 +02:00
|
|
|
|
// Create a scaled RectangleGeometry due to inaccurate hit testing in WPF.
|
|
|
|
|
|
// See http://stackoverflow.com/a/19335624/1136211
|
|
|
|
|
|
|
|
|
|
|
|
geometry.Rect = new Rect(p1.X * geometryScale, p1.Y * geometryScale,
|
|
|
|
|
|
(p2.X - p1.X) * geometryScale, (p2.Y - p1.Y) * geometryScale);
|
|
|
|
|
|
|
|
|
|
|
|
var transform = new TransformGroup();
|
|
|
|
|
|
transform.Children.Add(geometryScaleTransform); // revert scaling
|
|
|
|
|
|
transform.Children.Add(ParentMap.ViewportTransform);
|
|
|
|
|
|
RenderTransform = transform;
|
2013-04-12 19:59:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2013-10-13 16:32:51 +02:00
|
|
|
|
geometry.ClearValue(RectangleGeometry.RectProperty);
|
|
|
|
|
|
ClearValue(RenderTransformProperty);
|
2013-04-12 19:59:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|