Fixed MapItem.MapTransform implementation

This commit is contained in:
ClemensFischer 2023-01-19 17:16:02 +01:00
parent d7593cbe8c
commit fd13503613
11 changed files with 229 additions and 127 deletions

View file

@ -236,6 +236,18 @@ namespace MapControl
return ViewTransform.Scale * MapProjection.GetRelativeScale(location);
}
/// <summary>
/// Gets a transform Matrix for scaling and rotating objects that are anchored
/// at a Location from map coordinates (i.e. meters) to view coordinates.
/// </summary>
public Matrix GetMapTransform(Location location)
{
var scale = GetScale(location);
var matrix = new Matrix(scale.X, 0d, 0d, scale.Y, 0d, 0d);
matrix.Rotate(ViewTransform.Rotation);
return matrix;
}
/// <summary>
/// Transforms a Location in geographic coordinates to a Point in view coordinates.
/// </summary>

View file

@ -0,0 +1,105 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// Copyright © 2023 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
#if WINUI
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
#elif UWP
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
#else
using System.Windows.Controls;
using System.Windows.Media;
#endif
namespace MapControl
{
/// <summary>
/// Container class for an item in a MapItemsControl.
/// </summary>
public partial class MapItem : ListBoxItem, IMapElement
{
private MapBase parentMap;
private MatrixTransform mapTransform;
/// <summary>
/// Gets/sets MapPanel.AutoCollapse.
/// </summary>
public bool AutoCollapse
{
get => (bool)GetValue(AutoCollapseProperty);
set => SetValue(AutoCollapseProperty, value);
}
/// <summary>
/// Gets/sets MapPanel.Location.
/// </summary>
public Location Location
{
get => (Location)GetValue(LocationProperty);
set => SetValue(LocationProperty, value);
}
/// <summary>
/// Implements IMapElement.ParentMap.
/// </summary>
public MapBase ParentMap
{
get => parentMap;
set
{
if (parentMap != null)
{
parentMap.ViewportChanged -= OnViewportChanged;
}
parentMap = value;
if (parentMap != null && mapTransform != null)
{
// Attach ViewportChanged handler only if MapTransform is actually used.
//
parentMap.ViewportChanged += OnViewportChanged;
UpdateMapTransform(Location);
}
}
}
/// <summary>
/// Gets a Transform for scaling and rotating geometries
/// in map coordinates (meters) to view coordinates (pixels).
/// </summary>
public Transform MapTransform
{
get
{
if (mapTransform == null)
{
mapTransform = new MatrixTransform();
if (parentMap != null)
{
parentMap.ViewportChanged += OnViewportChanged;
UpdateMapTransform(Location);
}
}
return mapTransform;
}
}
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
{
UpdateMapTransform(Location);
}
private void UpdateMapTransform(Location location)
{
if (mapTransform != null && parentMap != null && location != null)
{
mapTransform.Matrix = parentMap.GetMapTransform(location);
}
}
}
}

View file

@ -8,77 +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.Media;
#endif
namespace MapControl
{
/// <summary>
/// Container class for an item in a MapItemsControl.
/// </summary>
public partial class MapItem : ListBoxItem
{
/// <summary>
/// Gets/sets MapPanel.AutoCollapse.
/// </summary>
public bool AutoCollapse
{
get => (bool)GetValue(AutoCollapseProperty);
set => SetValue(AutoCollapseProperty, value);
}
/// <summary>
/// Gets/sets MapPanel.Location.
/// </summary>
public Location Location
{
get => (Location)GetValue(LocationProperty);
set => SetValue(LocationProperty, value);
}
/// <summary>
/// Gets a Transform for scaling and rotating geometries
/// in map coordinates (meters) to view coordinates (pixels).
/// </summary>
public Transform MapTransform => mapTransform ?? (mapTransform = new MatrixTransform());
private MatrixTransform mapTransform;
protected override Size ArrangeOverride(Size bounds)
{
var size = base.ArrangeOverride(bounds);
// If the MapTransform property is used, update its Matrix property
// (after calling base.ArrangeOverride to avoid rendering issues).
//
if (mapTransform != null)
{
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 size;
}
}
/// <summary>
/// Manages a collection of selectable items on a Map.
/// </summary>

View file

@ -46,6 +46,9 @@ namespace MapControl
private MapBase parentMap;
/// <summary>
/// Implements IMapElement.ParentMap.
/// </summary>
public MapBase ParentMap
{
get => parentMap;

View file

@ -40,6 +40,9 @@ namespace MapControl
set => SetValue(LocationProperty, value);
}
/// <summary>
/// Implements IMapElement.ParentMap.
/// </summary>
public MapBase ParentMap
{
get => parentMap;
@ -72,9 +75,7 @@ namespace MapControl
if (parentMap != null && Location != null && Data != null)
{
var scale = parentMap.GetScale(Location);
var matrix = new Matrix(scale.X, 0d, 0d, scale.Y, 0d, 0d);
matrix.Rotate(parentMap.ViewTransform.Rotation);
var matrix = parentMap.GetMapTransform(Location);
if (Data.Transform is MatrixTransform transform)
{

View file

@ -165,6 +165,9 @@ namespace MapControl
private set => SetValue(LoadingProgressProperty, value);
}
/// <summary>
/// Implements IMapElement.ParentMap.
/// </summary>
public MapBase ParentMap
{
get => parentMap;