diff --git a/MapControl/Shared/MapPath.cs b/MapControl/Shared/MapPath.cs index 7d666b5b..4ff3a054 100644 --- a/MapControl/Shared/MapPath.cs +++ b/MapControl/Shared/MapPath.cs @@ -2,12 +2,18 @@ // Copyright © 2023 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) +using System.Collections.Generic; +using System.Linq; #if WINUI using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Media; +using Microsoft.UI.Xaml.Shapes; #elif UWP using Windows.UI.Xaml; +using Windows.UI.Xaml.Media; #else using System.Windows; +using System.Windows.Media; #endif namespace MapControl @@ -78,25 +84,6 @@ namespace MapControl #region Methods used only by derived classes MapPolyline, MapPolygon and MapMultiPolygon - protected double GetLongitudeOffset(Location location) - { - var longitudeOffset = 0d; - - if (location != null && parentMap.MapProjection.Type <= MapProjectionType.NormalCylindrical) - { - var point = parentMap.LocationToView(location); - - if (point.HasValue && - (point.Value.X < 0d || point.Value.X > parentMap.RenderSize.Width || - point.Value.Y < 0d || point.Value.Y > parentMap.RenderSize.Height)) - { - longitudeOffset = parentMap.ConstrainedLongitude(location.Longitude) - location.Longitude; - } - } - - return longitudeOffset; - } - protected Point? LocationToMap(Location location, double longitudeOffset) { if (longitudeOffset != 0d) @@ -133,6 +120,56 @@ namespace MapControl return parentMap.ViewTransform.MapToView(point.Value); } + protected double GetLongitudeOffset(Location location) + { + var longitudeOffset = 0d; + + if (location != null && parentMap.MapProjection.Type <= MapProjectionType.NormalCylindrical) + { + var point = parentMap.LocationToView(location); + + if (point.HasValue && + (point.Value.X < 0d || point.Value.X > parentMap.RenderSize.Width || + point.Value.Y < 0d || point.Value.Y > parentMap.RenderSize.Height)) + { + longitudeOffset = parentMap.ConstrainedLongitude(location.Longitude) - location.Longitude; + } + } + + return longitudeOffset; + } + + protected PathFigureCollection GetPolylineFigures(IEnumerable locations, bool closed) + { + var pathFigures = new PathFigureCollection(); + + if (parentMap != null && locations != null) + { + var longitudeOffset = GetLongitudeOffset(Location ?? locations.FirstOrDefault()); + + AddPolylineLocations(pathFigures, locations, longitudeOffset, closed); + } + + return pathFigures; + } + + protected PathFigureCollection GetMultiPolygonFigures(IEnumerable> polygons) + { + var pathFigures = new PathFigureCollection(); + + if (parentMap != null && polygons != null) + { + var longitudeOffset = GetLongitudeOffset(Location); + + foreach (var polygon in polygons) + { + AddPolylineLocations(pathFigures, polygon, longitudeOffset, true); + } + } + + return pathFigures; + } + #endregion } } diff --git a/MapControl/Shared/MapPolygon.cs b/MapControl/Shared/MapPolygon.cs index f5ac11b3..4fea2c1d 100644 --- a/MapControl/Shared/MapPolygon.cs +++ b/MapControl/Shared/MapPolygon.cs @@ -3,7 +3,6 @@ // Licensed under the Microsoft Public License (Ms-PL) using System.Collections.Generic; -using System.Linq; #if WINUI using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Media; @@ -55,15 +54,7 @@ namespace MapControl protected override void UpdateData() { - var pathFigures = ((PathGeometry)Data).Figures; - pathFigures.Clear(); - - if (ParentMap != null && Locations != null) - { - var longitudeOffset = GetLongitudeOffset(Location ?? Locations.FirstOrDefault()); - - AddPolylineLocations(pathFigures, Locations, longitudeOffset, true); - } + ((PathGeometry)Data).Figures = GetPolylineFigures(Locations, true); } } } diff --git a/MapControl/Shared/MapPolyline.cs b/MapControl/Shared/MapPolyline.cs index b592e91f..208ba798 100644 --- a/MapControl/Shared/MapPolyline.cs +++ b/MapControl/Shared/MapPolyline.cs @@ -3,7 +3,6 @@ // Licensed under the Microsoft Public License (Ms-PL) using System.Collections.Generic; -using System.Linq; #if WINUI using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Media; @@ -55,15 +54,7 @@ namespace MapControl protected override void UpdateData() { - var pathFigures = ((PathGeometry)Data).Figures; - pathFigures.Clear(); - - if (ParentMap != null && Locations != null) - { - var longitudeOffset = GetLongitudeOffset(Location ?? Locations.FirstOrDefault()); - - AddPolylineLocations(pathFigures, Locations, longitudeOffset, false); - } + ((PathGeometry)Data).Figures = GetPolylineFigures(Locations, false); } } } diff --git a/MapControl/WPF/MapMultiPolygon.WPF.cs b/MapControl/WPF/MapMultiPolygon.WPF.cs index 614b1951..fcdd901a 100644 --- a/MapControl/WPF/MapMultiPolygon.WPF.cs +++ b/MapControl/WPF/MapMultiPolygon.WPF.cs @@ -48,18 +48,7 @@ namespace MapControl protected override void UpdateData() { - var pathFigures = ((PathGeometry)Data).Figures; - pathFigures.Clear(); - - if (ParentMap != null && Polygons != null) - { - var longitudeOffset = GetLongitudeOffset(Location); - - foreach (var polygon in Polygons) - { - AddPolylineLocations(pathFigures, polygon, longitudeOffset, true); - } - } + ((PathGeometry)Data).Figures = GetMultiPolygonFigures(Polygons); } } }