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-05-04 12:52:20 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
2013-06-07 15:09:59 +02:00
|
|
|
|
using System;
|
2015-08-09 20:04:44 +02:00
|
|
|
|
#if NETFX_CORE
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using Windows.Foundation;
|
|
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
|
using Windows.UI.Xaml.Media;
|
|
|
|
|
|
#else
|
2012-04-25 22:02:53 +02:00
|
|
|
|
using System.Windows;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
#endif
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// <summary>
|
2015-08-09 20:04:44 +02:00
|
|
|
|
/// Arranges child elements on a Map at positions specified by the attached property Location.
|
|
|
|
|
|
/// The Location value is transformed to a viewport position and assigned as TranslateTransform
|
|
|
|
|
|
/// to the RenderTransform of the element, either directly or as last child of a TransformGroup.
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// </summary>
|
2014-07-01 18:57:44 +02:00
|
|
|
|
public partial class MapPanel : PanelBase, IMapElement
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-10 09:41:59 +02:00
|
|
|
|
public static readonly DependencyProperty LocationProperty = DependencyProperty.RegisterAttached(
|
2012-11-22 21:42:29 +01:00
|
|
|
|
"Location", typeof(Location), typeof(MapPanel), new PropertyMetadata(null, LocationPropertyChanged));
|
2012-08-10 09:41:59 +02:00
|
|
|
|
|
2012-05-04 12:52:20 +02:00
|
|
|
|
public static Location GetLocation(UIElement element)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-05-04 12:52:20 +02:00
|
|
|
|
return (Location)element.GetValue(LocationProperty);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-05-04 12:52:20 +02:00
|
|
|
|
public static void SetLocation(UIElement element, Location value)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
|
|
|
|
|
element.SetValue(LocationProperty, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-04-12 19:59:16 +02:00
|
|
|
|
private MapBase parentMap;
|
|
|
|
|
|
|
2014-07-22 20:02:30 +02:00
|
|
|
|
public MapBase ParentMap
|
2013-04-12 19:59:16 +02:00
|
|
|
|
{
|
|
|
|
|
|
get { return parentMap; }
|
2014-11-19 21:11:14 +01:00
|
|
|
|
set { SetParentMapOverride(value); }
|
2014-07-22 20:02:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-04-25 22:02:53 +02:00
|
|
|
|
protected override Size ArrangeOverride(Size finalSize)
|
|
|
|
|
|
{
|
2014-10-19 21:50:23 +02:00
|
|
|
|
foreach (UIElement element in Children)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-12-07 17:04:44 +01:00
|
|
|
|
var location = GetLocation(element);
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2012-12-07 17:04:44 +01:00
|
|
|
|
if (location != null)
|
|
|
|
|
|
{
|
2014-06-11 23:03:13 +02:00
|
|
|
|
ArrangeElementWithLocation(element);
|
2012-12-07 17:04:44 +01:00
|
|
|
|
SetViewportPosition(element, parentMap, location);
|
|
|
|
|
|
}
|
2014-06-11 23:03:13 +02:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
ArrangeElementWithoutLocation(element, finalSize);
|
|
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return finalSize;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
protected virtual void SetParentMapOverride(MapBase map)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (parentMap != null && parentMap != this)
|
|
|
|
|
|
{
|
|
|
|
|
|
parentMap.ViewportChanged -= OnViewportChanged;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
parentMap = map;
|
|
|
|
|
|
|
|
|
|
|
|
if (parentMap != null && parentMap != this)
|
|
|
|
|
|
{
|
|
|
|
|
|
parentMap.ViewportChanged += OnViewportChanged;
|
|
|
|
|
|
OnViewportChanged();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-06-07 15:09:59 +02:00
|
|
|
|
protected virtual void OnViewportChanged()
|
|
|
|
|
|
{
|
2014-10-19 21:50:23 +02:00
|
|
|
|
foreach (UIElement element in Children)
|
2013-06-07 15:09:59 +02:00
|
|
|
|
{
|
|
|
|
|
|
var location = GetLocation(element);
|
|
|
|
|
|
|
|
|
|
|
|
if (location != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
SetViewportPosition(element, parentMap, location);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnViewportChanged(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
OnViewportChanged();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
private static void ParentMapPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
|
2012-08-10 09:41:59 +02:00
|
|
|
|
{
|
2013-04-12 19:59:16 +02:00
|
|
|
|
var mapElement = obj as IMapElement;
|
2012-08-10 09:41:59 +02:00
|
|
|
|
|
2013-04-12 19:59:16 +02:00
|
|
|
|
if (mapElement != null)
|
2012-08-10 09:41:59 +02:00
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
mapElement.ParentMap = e.NewValue as MapBase;
|
2012-08-10 09:41:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-22 20:21:31 +02:00
|
|
|
|
private static void LocationPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
var element = obj as UIElement;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2012-08-09 23:41:47 +02:00
|
|
|
|
if (element != null)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2013-05-25 08:53:50 +02:00
|
|
|
|
var location = e.NewValue as Location;
|
|
|
|
|
|
|
2014-06-11 23:03:13 +02:00
|
|
|
|
if (location == null)
|
2013-05-25 08:53:50 +02:00
|
|
|
|
{
|
2014-06-11 23:03:13 +02:00
|
|
|
|
ArrangeElementWithoutLocation(element, Size.Empty);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (e.OldValue == null)
|
|
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
ArrangeElementWithLocation(element); // arrange element when Location was null before
|
2013-05-25 08:53:50 +02:00
|
|
|
|
}
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
SetViewportPosition(element, GetParentMap(element), location);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-12 19:22:49 +02:00
|
|
|
|
private static void SetViewportPosition(UIElement element, MapBase parentMap, Location location)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2015-03-15 10:11:19 +01:00
|
|
|
|
var viewportPosition = new Point();
|
2012-08-09 23:41:47 +02:00
|
|
|
|
|
|
|
|
|
|
if (parentMap != null && location != null)
|
|
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
var mapPosition = parentMap.MapTransform.Transform(location, parentMap.Center.Longitude); // nearest to center longitude
|
2015-03-15 10:11:19 +01:00
|
|
|
|
|
2013-11-18 19:49:17 +01:00
|
|
|
|
viewportPosition = parentMap.ViewportTransform.Transform(mapPosition);
|
2015-03-15 10:11:19 +01:00
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
if ((bool)element.GetValue(FrameworkElement.UseLayoutRoundingProperty))
|
2015-03-15 10:11:19 +01:00
|
|
|
|
{
|
|
|
|
|
|
viewportPosition.X = Math.Round(viewportPosition.X);
|
|
|
|
|
|
viewportPosition.Y = Math.Round(viewportPosition.Y);
|
|
|
|
|
|
}
|
2012-08-09 23:41:47 +02:00
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2012-12-12 17:03:33 +01:00
|
|
|
|
var translateTransform = element.RenderTransform as TranslateTransform;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2012-12-12 17:03:33 +01:00
|
|
|
|
if (translateTransform == null)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-12-12 17:03:33 +01:00
|
|
|
|
var transformGroup = element.RenderTransform as TransformGroup;
|
2012-11-26 19:17:12 +01:00
|
|
|
|
|
2012-12-12 17:03:33 +01:00
|
|
|
|
if (transformGroup == null)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-12-12 01:44:37 +01:00
|
|
|
|
translateTransform = new TranslateTransform();
|
2012-12-12 17:03:33 +01:00
|
|
|
|
element.RenderTransform = translateTransform;
|
2012-12-12 01:44:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2012-12-12 17:03:33 +01:00
|
|
|
|
if (transformGroup.Children.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
translateTransform = transformGroup.Children[transformGroup.Children.Count - 1] as TranslateTransform;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (translateTransform == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
translateTransform = new TranslateTransform();
|
|
|
|
|
|
transformGroup.Children.Add(translateTransform);
|
|
|
|
|
|
}
|
2012-12-12 01:44:37 +01:00
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
2012-12-12 01:44:37 +01:00
|
|
|
|
|
|
|
|
|
|
translateTransform.X = viewportPosition.X;
|
|
|
|
|
|
translateTransform.Y = viewportPosition.Y;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
2012-12-07 17:04:44 +01:00
|
|
|
|
|
2014-06-11 23:03:13 +02:00
|
|
|
|
private static void ArrangeElementWithLocation(UIElement element)
|
2012-12-07 17:04:44 +01:00
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
var rect = new Rect(new Point(), element.DesiredSize);
|
2013-05-25 08:53:50 +02:00
|
|
|
|
var frameworkElement = element as FrameworkElement;
|
|
|
|
|
|
|
|
|
|
|
|
if (frameworkElement != null)
|
2012-12-07 17:04:44 +01:00
|
|
|
|
{
|
2014-06-11 23:03:13 +02:00
|
|
|
|
switch (frameworkElement.HorizontalAlignment)
|
2013-05-25 08:53:50 +02:00
|
|
|
|
{
|
2014-06-11 23:03:13 +02:00
|
|
|
|
case HorizontalAlignment.Center:
|
|
|
|
|
|
rect.X = -rect.Width / 2d;
|
|
|
|
|
|
break;
|
2012-12-07 17:04:44 +01:00
|
|
|
|
|
2014-06-11 23:03:13 +02:00
|
|
|
|
case HorizontalAlignment.Right:
|
|
|
|
|
|
rect.X = -rect.Width;
|
|
|
|
|
|
break;
|
2012-12-07 17:04:44 +01:00
|
|
|
|
|
2014-06-11 23:03:13 +02:00
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2012-12-07 17:04:44 +01:00
|
|
|
|
|
2014-06-11 23:03:13 +02:00
|
|
|
|
switch (frameworkElement.VerticalAlignment)
|
|
|
|
|
|
{
|
|
|
|
|
|
case VerticalAlignment.Center:
|
|
|
|
|
|
rect.Y = -rect.Height / 2d;
|
|
|
|
|
|
break;
|
2012-12-07 17:04:44 +01:00
|
|
|
|
|
2014-06-11 23:03:13 +02:00
|
|
|
|
case VerticalAlignment.Bottom:
|
|
|
|
|
|
rect.Y = -rect.Height;
|
|
|
|
|
|
break;
|
2012-12-07 17:04:44 +01:00
|
|
|
|
|
2014-06-11 23:03:13 +02:00
|
|
|
|
default:
|
|
|
|
|
|
break;
|
2013-05-25 08:53:50 +02:00
|
|
|
|
}
|
2014-06-11 23:03:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
element.Arrange(rect);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void ArrangeElementWithoutLocation(UIElement element, Size parentSize)
|
|
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
var rect = new Rect(new Point(), element.DesiredSize);
|
2014-06-11 23:03:13 +02:00
|
|
|
|
var frameworkElement = element as FrameworkElement;
|
|
|
|
|
|
|
|
|
|
|
|
if (frameworkElement != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (parentSize.IsEmpty)
|
2013-05-25 08:53:50 +02:00
|
|
|
|
{
|
2014-06-11 23:03:13 +02:00
|
|
|
|
var parent = frameworkElement.Parent as UIElement;
|
|
|
|
|
|
parentSize = parent != null ? parent.RenderSize : new Size();
|
|
|
|
|
|
}
|
2012-12-07 17:04:44 +01:00
|
|
|
|
|
2014-06-11 23:03:13 +02:00
|
|
|
|
switch (frameworkElement.HorizontalAlignment)
|
|
|
|
|
|
{
|
|
|
|
|
|
case HorizontalAlignment.Center:
|
|
|
|
|
|
rect.X = (parentSize.Width - rect.Width) / 2d;
|
|
|
|
|
|
break;
|
2012-12-07 17:04:44 +01:00
|
|
|
|
|
2014-06-11 23:03:13 +02:00
|
|
|
|
case HorizontalAlignment.Right:
|
|
|
|
|
|
rect.X = parentSize.Width - rect.Width;
|
|
|
|
|
|
break;
|
2012-12-07 17:04:44 +01:00
|
|
|
|
|
2014-06-11 23:03:13 +02:00
|
|
|
|
case HorizontalAlignment.Stretch:
|
|
|
|
|
|
rect.Width = parentSize.Width;
|
|
|
|
|
|
break;
|
2012-12-07 17:04:44 +01:00
|
|
|
|
|
2014-06-11 23:03:13 +02:00
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2012-12-07 17:04:44 +01:00
|
|
|
|
|
2014-06-11 23:03:13 +02:00
|
|
|
|
switch (frameworkElement.VerticalAlignment)
|
|
|
|
|
|
{
|
|
|
|
|
|
case VerticalAlignment.Center:
|
|
|
|
|
|
rect.Y = (parentSize.Height - rect.Height) / 2d;
|
|
|
|
|
|
break;
|
2012-12-07 17:04:44 +01:00
|
|
|
|
|
2014-06-11 23:03:13 +02:00
|
|
|
|
case VerticalAlignment.Bottom:
|
|
|
|
|
|
rect.Y = parentSize.Height - rect.Height;
|
|
|
|
|
|
break;
|
2012-12-07 17:04:44 +01:00
|
|
|
|
|
2014-06-11 23:03:13 +02:00
|
|
|
|
case VerticalAlignment.Stretch:
|
|
|
|
|
|
rect.Height = parentSize.Height;
|
|
|
|
|
|
break;
|
2012-12-07 17:04:44 +01:00
|
|
|
|
|
2014-06-11 23:03:13 +02:00
|
|
|
|
default:
|
|
|
|
|
|
break;
|
2013-05-25 08:53:50 +02:00
|
|
|
|
}
|
2012-12-07 17:04:44 +01:00
|
|
|
|
}
|
2013-05-25 08:53:50 +02:00
|
|
|
|
|
|
|
|
|
|
element.Arrange(rect);
|
2012-12-07 17:04:44 +01:00
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|