XAML-Map-Control/MapControl/WinUI/MapPolypoint.WinUI.cs

101 lines
3.1 KiB
C#
Raw Normal View History

2024-05-26 09:49:00 +02:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
#if UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
#else
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
#endif
namespace MapControl
{
/// <summary>
/// Base class of MapPolyline and MapPolygon.
/// </summary>
public class MapPolypoint : MapPath
{
public static readonly DependencyProperty FillRuleProperty =
DependencyPropertyHelper.Register<MapPolygon, FillRule>(nameof(FillRule), FillRule.EvenOdd,
(polypoint, oldValue, newValue) => ((PathGeometry)polypoint.Data).FillRule = newValue);
public FillRule FillRule
{
get => (FillRule)GetValue(FillRuleProperty);
set => SetValue(FillRuleProperty, value);
}
protected MapPolypoint()
{
Data = new PathGeometry();
}
protected void DataCollectionPropertyChanged(IEnumerable oldValue, IEnumerable newValue)
{
if (oldValue is INotifyCollectionChanged oldCollection)
{
oldCollection.CollectionChanged -= DataCollectionChanged;
}
if (newValue is INotifyCollectionChanged newCollection)
{
newCollection.CollectionChanged += DataCollectionChanged;
}
UpdateData();
}
protected void DataCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdateData();
}
protected void UpdateData(IEnumerable<Location> locations, bool closed)
{
2024-05-26 12:39:40 +02:00
var pathFigures = ((PathGeometry)Data).Figures;
pathFigures.Clear();
2024-05-26 09:49:00 +02:00
2024-07-15 13:41:52 +02:00
if (ParentMap != null && locations != null)
2024-05-26 09:49:00 +02:00
{
2024-07-15 13:41:52 +02:00
var longitudeOffset = GetLongitudeOffset(Location ?? locations.FirstOrDefault());
2024-05-26 09:49:00 +02:00
AddPolylinePoints(pathFigures, locations, longitudeOffset, closed);
}
}
2024-05-26 12:39:40 +02:00
private void AddPolylinePoints(PathFigureCollection pathFigures, IEnumerable<Location> locations, double longitudeOffset, bool closed)
2024-05-26 09:49:00 +02:00
{
2024-07-15 09:09:15 +02:00
var points = locations
.Select(location => LocationToView(location, longitudeOffset))
.Where(point => point.HasValue)
.Select(point => point.Value);
2024-05-26 09:49:00 +02:00
2024-07-15 13:41:52 +02:00
if (points.Any())
2024-07-15 09:09:15 +02:00
{
var figure = new PathFigure
2024-05-26 09:49:00 +02:00
{
2024-07-15 09:09:15 +02:00
StartPoint = points.First(),
2024-07-15 13:41:52 +02:00
IsClosed = closed,
2024-07-15 09:09:15 +02:00
IsFilled = true
};
2024-05-26 09:49:00 +02:00
2024-07-15 09:09:15 +02:00
var polyline = new PolyLineSegment();
2024-05-26 14:15:02 +02:00
2024-07-15 09:09:15 +02:00
foreach (var point in points.Skip(1))
2024-05-26 09:49:00 +02:00
{
2024-07-15 09:09:15 +02:00
polyline.Points.Add(point);
}
2024-05-26 09:49:00 +02:00
2024-07-15 09:09:15 +02:00
figure.Segments.Add(polyline);
pathFigures.Add(figure);
}
}
2024-05-26 09:49:00 +02:00
}
}