Avalonia MapPolygon, MapPolyline

This commit is contained in:
ClemensFischer 2024-05-25 23:03:40 +02:00
parent bfa70de0cd
commit cf08a2baae
9 changed files with 40 additions and 25 deletions

View file

@ -28,8 +28,6 @@
<Compile Remove="..\Shared\MapGraticule.cs" />
<Compile Remove="..\Shared\MapItem.cs" />
<Compile Remove="..\Shared\MapItemsControl.cs" />
<Compile Remove="..\Shared\MapPolyline.cs" />
<Compile Remove="..\Shared\MapPolygon.cs" />
<Compile Remove="..\Shared\ViewTransform.cs" />
</ItemGroup>

View file

@ -4,7 +4,6 @@
using Avalonia.Controls.Shapes;
using Avalonia.Media;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
@ -21,7 +20,7 @@ namespace MapControl
public static readonly StyledProperty<Geometry> DataProperty =
DependencyPropertyHelper.AddOwner<MapPath, Geometry>(Path.DataProperty,
(path, oldValue, newValue) => path.DataPropertyChanged(oldValue, newValue));
(path, oldValue, newValue) => path.UpdateData());
public Geometry Data
{
@ -31,16 +30,6 @@ namespace MapControl
protected override Geometry CreateDefiningGeometry() => Data;
private void DataPropertyChanged(Geometry oldData, Geometry newData)
{
// Check if data is actually a new Geometry.
//
if (newData != null && !ReferenceEquals(newData, oldData))
{
UpdateData();
}
}
private void SetMapTransform(Matrix matrix)
{
if (Data.Transform is MatrixTransform transform)

View file

@ -141,14 +141,18 @@ namespace MapControl
return longitudeOffset;
}
protected void AddPolylinePoints(PathFigureCollection pathFigures, IEnumerable<Location> locations, bool closed)
protected PathFigureCollection GetPathFigures(IEnumerable<Location> locations, bool closed)
{
var pathFigures = new PathFigureCollection();
if (parentMap != null && locations != null)
{
var longitudeOffset = GetLongitudeOffset(Location ?? locations.FirstOrDefault());
AddPolylinePoints(pathFigures, locations, longitudeOffset, closed);
}
return pathFigures;
}
protected void AddMultiPolygonPoints(PathFigureCollection pathFigures, IEnumerable<IEnumerable<Location>> polygons)

View file

@ -12,6 +12,9 @@ using Windows.UI.Xaml.Media;
#elif WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
#elif AVALONIA
using Avalonia.Media;
using DependencyProperty = Avalonia.AvaloniaProperty;
#endif
namespace MapControl
@ -32,7 +35,7 @@ namespace MapControl
/// <summary>
/// Gets or sets the Locations that define the polygon points.
/// </summary>
#if WPF
#if WPF || AVALONIA
[System.ComponentModel.TypeConverter(typeof(LocationCollectionConverter))]
#endif
public IEnumerable<Location> Locations
@ -54,9 +57,10 @@ namespace MapControl
protected override void UpdateData()
{
var figures = ((PathGeometry)Data).Figures;
figures.Clear();
AddPolylinePoints(figures, Locations, true);
((PathGeometry)Data).Figures = GetPathFigures(Locations, true);
#if AVALONIA
InvalidateGeometry();
#endif
}
}
}

View file

@ -12,6 +12,9 @@ using Windows.UI.Xaml.Media;
#elif WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
#elif AVALONIA
using Avalonia.Media;
using DependencyProperty = Avalonia.AvaloniaProperty;
#endif
namespace MapControl
@ -32,7 +35,7 @@ namespace MapControl
/// <summary>
/// Gets or sets the Locations that define the polyline points.
/// </summary>
#if WPF
#if WPF || AVALONIA
[System.ComponentModel.TypeConverter(typeof(LocationCollectionConverter))]
#endif
public IEnumerable<Location> Locations
@ -54,9 +57,10 @@ namespace MapControl
protected override void UpdateData()
{
var figures = ((PathGeometry)Data).Figures;
figures.Clear();
AddPolylinePoints(figures, Locations, false);
((PathGeometry)Data).Figures = GetPathFigures(Locations, false);
#if AVALONIA
InvalidateGeometry();
#endif
}
}
}

View file

@ -7,11 +7,9 @@ using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
#if UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
#else
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Shapes;
#endif

View file

@ -18,6 +18,12 @@
</map:MapPath.Data>
</map:MapPath>
<map:MapPolygon Stroke="Yellow" StrokeThickness="2">
<map:MapPolygon.Locations>
<map:LocationCollection>53.45,8.1 53.45,8.3 53.55,8.3 53.55,8.1</map:LocationCollection>
</map:MapPolygon.Locations>
</map:MapPolygon>
<map:Pushpin AutoCollapse="True" Location="53.5,8.2" Content="N 53°30' E 8°12'"/>
</map:Map>

View file

@ -142,6 +142,12 @@
</map:MapPath.Data>
</map:MapPath>
<map:MapPolygon Stroke="Yellow" StrokeThickness="2">
<map:MapPolygon.Locations>
<map:LocationCollection>53.45,8.1 53.45,8.3 53.55,8.3 53.55,8.1</map:LocationCollection>
</map:MapPolygon.Locations>
</map:MapPolygon>
<map:Pushpin Location="53.5,8.2" AutoCollapse="True" Content="N 53°30' E 8°12'"/>
</map:Map>

View file

@ -142,6 +142,12 @@
<map:Pushpin AutoCollapse="True" Location="35,33" Content="Cyprus"/>
<map:Pushpin AutoCollapse="True" Location="28.25,-16.5" Content="Tenerife"/>
<map:MapPolygon Stroke="Yellow" StrokeThickness="2">
<map:MapPolygon.Locations>
<map:LocationCollection>53.45,8.1 53.45,8.3 53.55,8.3 53.55,8.1</map:LocationCollection>
</map:MapPolygon.Locations>
</map:MapPolygon>
<map:MapPath Location="53.5,8.2" Stroke="Blue" StrokeThickness="3" Fill="#1F007F00">
<map:MapPath.Data>
<EllipseGeometry RadiusX="1852" RadiusY="1852"/>