mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-01-07 17:20:55 +01:00
Create MapItemsImageLayer.WPF.cs
This commit is contained in:
parent
91a1958e15
commit
926e2eb745
88
MapControl/WPF/MapItemsImageLayer.WPF.cs
Normal file
88
MapControl/WPF/MapItemsImageLayer.WPF.cs
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// © 2020 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public interface IMapDrawingItem
|
||||
{
|
||||
IEnumerable<Location> Locations { get; }
|
||||
|
||||
Drawing GetDrawing(IList<Point> positions, double scale, double rotation);
|
||||
}
|
||||
|
||||
public class MapItemsImageLayer : MapImageLayer
|
||||
{
|
||||
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
|
||||
nameof(ItemsSource), typeof(IEnumerable<IMapDrawingItem>), typeof(MapItemsImageLayer));
|
||||
|
||||
public IEnumerable<IMapDrawingItem> ItemsSource
|
||||
{
|
||||
get { return (IEnumerable<IMapDrawingItem>)GetValue(ItemsSourceProperty); }
|
||||
set { SetValue(ItemsSourceProperty, value); }
|
||||
}
|
||||
|
||||
protected override async Task<ImageSource> GetImageAsync()
|
||||
{
|
||||
ImageSource image = null;
|
||||
var projection = ParentMap?.MapProjection;
|
||||
var items = ItemsSource;
|
||||
|
||||
if (projection != null && items != null)
|
||||
{
|
||||
image = await Task.Run(() => GetImage(projection, items));
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
private ImageSource GetImage(MapProjection projection, IEnumerable<IMapDrawingItem> items)
|
||||
{
|
||||
var scale = ParentMap.ViewTransform.Scale;
|
||||
var rotation = ParentMap.ViewTransform.Rotation;
|
||||
var mapRect = projection.BoundingBoxToRect(BoundingBox);
|
||||
var imageRect = new Rect(0, 0, scale * mapRect.Width, scale * mapRect.Height);
|
||||
var drawings = new DrawingGroup();
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
var positions = item.Locations.Select(l => projection.LocationToMap(l)).ToList();
|
||||
|
||||
if (positions.Any(p => mapRect.Contains(p)))
|
||||
{
|
||||
for (int i = 0; i < positions.Count; i++)
|
||||
{
|
||||
positions[i] = new Point(scale * (positions[i].X - mapRect.X),
|
||||
imageRect.Height - scale * (positions[i].Y - mapRect.Y));
|
||||
}
|
||||
|
||||
drawings.Children.Add(item.GetDrawing(positions, scale, rotation));
|
||||
}
|
||||
}
|
||||
|
||||
var drawingBrush = new DrawingBrush
|
||||
{
|
||||
Drawing = drawings,
|
||||
ViewboxUnits = BrushMappingMode.Absolute,
|
||||
Viewbox = imageRect,
|
||||
};
|
||||
|
||||
var drawing = new GeometryDrawing
|
||||
{
|
||||
Geometry = new RectangleGeometry(imageRect),
|
||||
Brush = drawingBrush
|
||||
};
|
||||
|
||||
var image = new DrawingImage(drawing);
|
||||
image.Freeze();
|
||||
|
||||
return image;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue