MapMultiPolygon

This commit is contained in:
ClemensFischer 2024-07-16 21:29:25 +02:00
parent 5b9ad68c57
commit dbd32361b5
11 changed files with 217 additions and 61 deletions

View file

@ -20,6 +20,10 @@
<Reference Include="System.Net.Http" />
</ItemGroup>
<ItemGroup>
<Compile Remove="..\Shared\PolygonCollection.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />

View file

@ -1,38 +0,0 @@
// 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.Generic;
using System.Windows;
namespace MapControl
{
/// <summary>
/// A multi-polygon defined by a collection of collections of Locations.
/// Allows to draw filled polygons with holes.
///
/// A PolygonCollection (with ObservableCollection of Location elements) may be used
/// for the Polygons property if collection changes of the property itself and its
/// elements are both supposed to trigger UI updates.
/// </summary>
public class MapMultiPolygon : MapPolypoint
{
public static readonly DependencyProperty PolygonsProperty =
DependencyPropertyHelper.Register<MapMultiPolygon, IEnumerable<IEnumerable<Location>>>(nameof(Polygons), null,
(polygon, oldValue, newValue) => polygon.DataCollectionPropertyChanged(oldValue, newValue));
/// <summary>
/// Gets or sets the Locations that define the multi-polygon points.
/// </summary>
public IEnumerable<IEnumerable<Location>> Polygons
{
get => (IEnumerable<IEnumerable<Location>>)GetValue(PolygonsProperty);
set => SetValue(PolygonsProperty, value);
}
protected override void UpdateData()
{
UpdateData(Polygons);
}
}
}

View file

@ -77,9 +77,9 @@ namespace MapControl
{
var longitudeOffset = GetLongitudeOffset(Location);
foreach (var polygon in polygons)
foreach (var locations in polygons)
{
AddPolylinePoints(context, polygon, longitudeOffset, true);
AddPolylinePoints(context, locations, longitudeOffset, true);
}
}
}
@ -94,8 +94,27 @@ namespace MapControl
if (points.Any())
{
context.BeginFigure(points.First(), true, closed);
context.PolyLineTo(points.Skip(1).ToList(), true, true);
var start = points.First();
var polyline = points.Skip(1).ToList();
var minX = start.X;
var maxX = start.X;
var minY = start.Y;
var maxY = start.Y;
foreach (var point in polyline)
{
minX = Math.Min(minX, point.X);
maxX = Math.Max(maxX, point.X);
minY = Math.Min(minY, point.Y);
maxY = Math.Max(maxY, point.Y);
}
if (maxX >= 0 && minX <= ParentMap.ActualWidth &&
maxY >= 0 && minY <= ParentMap.ActualHeight)
{
context.BeginFigure(start, true, closed);
context.PolyLineTo(polyline, true, true);
}
}
}
}

View file

@ -27,9 +27,9 @@ namespace MapControl
protected override void InsertItem(int index, IEnumerable<Location> polygon)
{
if (polygon is INotifyCollectionChanged observablePolygon)
if (polygon is INotifyCollectionChanged addedPolygon)
{
CollectionChangedEventManager.AddListener(observablePolygon, this);
CollectionChangedEventManager.AddListener(addedPolygon, this);
}
base.InsertItem(index, polygon);
@ -37,9 +37,14 @@ namespace MapControl
protected override void SetItem(int index, IEnumerable<Location> polygon)
{
if (this[index] is INotifyCollectionChanged observablePolygon)
if (this[index] is INotifyCollectionChanged removedPolygon)
{
CollectionChangedEventManager.RemoveListener(observablePolygon, this);
CollectionChangedEventManager.RemoveListener(removedPolygon, this);
}
if (polygon is INotifyCollectionChanged addedPolygon)
{
CollectionChangedEventManager.AddListener(addedPolygon, this);
}
base.SetItem(index, polygon);
@ -47,9 +52,9 @@ namespace MapControl
protected override void RemoveItem(int index)
{
if (this[index] is INotifyCollectionChanged observablePolygon)
if (this[index] is INotifyCollectionChanged removedPolygon)
{
CollectionChangedEventManager.RemoveListener(observablePolygon, this);
CollectionChangedEventManager.RemoveListener(removedPolygon, this);
}
base.RemoveItem(index);
@ -57,9 +62,9 @@ namespace MapControl
protected override void ClearItems()
{
foreach (var observablePolygon in this.OfType<INotifyCollectionChanged>())
foreach (var polygon in this.OfType<INotifyCollectionChanged>())
{
CollectionChangedEventManager.RemoveListener(observablePolygon, this);
CollectionChangedEventManager.RemoveListener(polygon, this);
}
base.ClearItems();