From 85cc01f086a742c1139944fe8d3caeeea4440a3c Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Sun, 6 Nov 2022 15:27:39 +0100 Subject: [PATCH] Add MapItem.MapTransform property --- MapControl/Shared/MapItemsControl.cs | 33 +++++++++++++++++++++++ MapControl/Shared/MapPanel.cs | 2 +- SampleApps/UniversalApp/MainPage.xaml | 22 ++++++++++++--- SampleApps/WinUiApp/MainWindow.xaml | 22 ++++++++++++--- SampleApps/WpfApplication/MainWindow.xaml | 6 +++++ 5 files changed, 76 insertions(+), 9 deletions(-) diff --git a/MapControl/Shared/MapItemsControl.cs b/MapControl/Shared/MapItemsControl.cs index 3275762b..a23333db 100644 --- a/MapControl/Shared/MapItemsControl.cs +++ b/MapControl/Shared/MapItemsControl.cs @@ -8,15 +8,19 @@ using Windows.Foundation; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Data; +using Microsoft.UI.Xaml.Media; #elif UWP using Windows.Foundation; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Data; +using Windows.UI.Xaml.Media; #else using System.Windows; using System.Windows.Controls; using System.Windows.Data; +using System.Windows.Markup; +using System.Windows.Media; #endif namespace MapControl @@ -43,6 +47,35 @@ namespace MapControl get => (Location)GetValue(LocationProperty); set => SetValue(LocationProperty, value); } + + /// + /// Gets a Transform for scaling and rotating geometries + /// in map coordinates (meters) to view coordinates (pixels). + /// + public Transform MapTransform + { + get => mapTransform ?? (mapTransform = new MatrixTransform()); + } + + private MatrixTransform mapTransform; + + protected override Size ArrangeOverride(Size bounds) + { + if (mapTransform != null) // property in use, e.g. as source of a Binding + { + var parentMap = (VisualTreeHelper.GetParent(this) as MapPanel)?.ParentMap; + + if (parentMap != null && Location != null) + { + var scale = parentMap.GetScale(Location); + var matrix = new Matrix(scale.X, 0d, 0d, scale.Y, 0d, 0d); + matrix.Rotate(parentMap.ViewTransform.Rotation); + mapTransform.Matrix = matrix; + } + } + + return base.ArrangeOverride(bounds); + } } /// diff --git a/MapControl/Shared/MapPanel.cs b/MapControl/Shared/MapPanel.cs index aeaf0e7f..87c24107 100644 --- a/MapControl/Shared/MapPanel.cs +++ b/MapControl/Shared/MapPanel.cs @@ -98,7 +98,7 @@ namespace MapControl } /// - /// Gets the position of an element with Location in view coordinates, + /// Gets the view position of an element with Location /// or null when the element has no Location. /// public static Point? GetViewPosition(FrameworkElement element) diff --git a/SampleApps/UniversalApp/MainPage.xaml b/SampleApps/UniversalApp/MainPage.xaml index 10ac3536..8ca39945 100644 --- a/SampleApps/UniversalApp/MainPage.xaml +++ b/SampleApps/UniversalApp/MainPage.xaml @@ -26,26 +26,40 @@ - + - + - + - + + + + + + diff --git a/SampleApps/WinUiApp/MainWindow.xaml b/SampleApps/WinUiApp/MainWindow.xaml index 3f5a7b22..c3b5c077 100644 --- a/SampleApps/WinUiApp/MainWindow.xaml +++ b/SampleApps/WinUiApp/MainWindow.xaml @@ -33,26 +33,40 @@ - + - + - + - + + + + + + diff --git a/SampleApps/WpfApplication/MainWindow.xaml b/SampleApps/WpfApplication/MainWindow.xaml index f7a540df..3af07971 100644 --- a/SampleApps/WpfApplication/MainWindow.xaml +++ b/SampleApps/WpfApplication/MainWindow.xaml @@ -48,6 +48,12 @@ + + + + +