From 2f828fbe2dad3a67c55f3e9d2006ac640e915116 Mon Sep 17 00:00:00 2001 From: ClemensF Date: Tue, 13 Feb 2018 20:11:16 +0100 Subject: [PATCH] Version 4.4.1: MapPolygon for UWP in progress --- MapControl/Shared/MapShape.cs | 11 ++++++++--- MapControl/UWP/MapPolygon.UWP.cs | 10 +++++++--- MapControl/UWP/MapPolyline.UWP.cs | 7 ++++++- MapControl/WPF/MapMultiPolygon.WPF.cs | 12 +++++++++--- MapControl/WPF/MapPolygon.WPF.cs | 24 ++++++++++++++++++++++-- MapControl/WPF/MapPolyline.WPF.cs | 10 ++++++++-- MapControl/WPF/MapShape.WPF.cs | 20 ++++---------------- 7 files changed, 64 insertions(+), 30 deletions(-) diff --git a/MapControl/Shared/MapShape.cs b/MapControl/Shared/MapShape.cs index e45d236d..7d1dac23 100644 --- a/MapControl/Shared/MapShape.cs +++ b/MapControl/Shared/MapShape.cs @@ -63,9 +63,9 @@ namespace MapControl } } - protected MapShape() + protected MapShape(Geometry data) { - Data = new PathGeometry(); + Data = data; MapPanel.InitMapElement(this); } @@ -88,13 +88,18 @@ namespace MapControl return point; } + protected Point LocationToViewportPoint(Location location) + { + return parentMap.MapProjection.ViewportTransform.Transform(LocationToPoint(location)); + } + protected double GetLongitudeOffset() { var longitudeOffset = 0d; if (parentMap.MapProjection.IsContinuous && Location != null) { - var viewportPosition = parentMap.MapProjection.LocationToViewportPoint(Location); + var viewportPosition = LocationToViewportPoint(Location); if (viewportPosition.X < 0d || viewportPosition.X > parentMap.RenderSize.Width || viewportPosition.Y < 0d || viewportPosition.Y > parentMap.RenderSize.Height) diff --git a/MapControl/UWP/MapPolygon.UWP.cs b/MapControl/UWP/MapPolygon.UWP.cs index a6c0d27a..a024159d 100644 --- a/MapControl/UWP/MapPolygon.UWP.cs +++ b/MapControl/UWP/MapPolygon.UWP.cs @@ -18,6 +18,11 @@ namespace MapControl nameof(Locations), typeof(IEnumerable), typeof(MapPolygon), new PropertyMetadata(null, (o, e) => ((MapPolygon)o).LocationsPropertyChanged(e))); + public MapPolygon() + : base(new PathGeometry()) + { + } + /// /// Gets or sets the Locations that define the polyline points. /// @@ -29,8 +34,7 @@ namespace MapControl protected override void UpdateData() { - var figures = ((PathGeometry)Data).Figures; - figures.Clear(); + ((PathGeometry)Data).Figures.Clear(); if (ParentMap != null && Locations != null && Locations.Count() >= 2) { @@ -42,7 +46,7 @@ namespace MapControl locations = locations.Select(loc => new Location(loc.Latitude, loc.Longitude + offset)); } - var points = locations.Select(loc => ParentMap.MapProjection.LocationToViewportPoint(loc)).ToList(); + var points = locations.Select(loc => LocationToViewportPoint(loc)).ToList(); points.Add(points[0]); CreatePolylineFigures(points); diff --git a/MapControl/UWP/MapPolyline.UWP.cs b/MapControl/UWP/MapPolyline.UWP.cs index d37afc67..e373f845 100644 --- a/MapControl/UWP/MapPolyline.UWP.cs +++ b/MapControl/UWP/MapPolyline.UWP.cs @@ -18,6 +18,11 @@ namespace MapControl nameof(Locations), typeof(IEnumerable), typeof(MapPolyline), new PropertyMetadata(null, (o, e) => ((MapPolyline)o).LocationsPropertyChanged(e))); + public MapPolyline() + : base(new PathGeometry()) + { + } + /// /// Gets or sets the Locations that define the polyline points. /// @@ -41,7 +46,7 @@ namespace MapControl locations = locations.Select(loc => new Location(loc.Latitude, loc.Longitude + offset)); } - var points = locations.Select(loc => ParentMap.MapProjection.LocationToViewportPoint(loc)).ToList(); + var points = locations.Select(loc => LocationToViewportPoint(loc)).ToList(); CreatePolylineFigures(points); } diff --git a/MapControl/WPF/MapMultiPolygon.WPF.cs b/MapControl/WPF/MapMultiPolygon.WPF.cs index f75c30d4..872d7744 100644 --- a/MapControl/WPF/MapMultiPolygon.WPF.cs +++ b/MapControl/WPF/MapMultiPolygon.WPF.cs @@ -17,12 +17,17 @@ namespace MapControl /// for the Polygons property if collection changes of the property itself and its /// elements are both supposed to trigger a UI update. /// - public class MapMultiPolygon : MapShape, IWeakEventListener + public class MapMultiPolygon : MapShape { public static readonly DependencyProperty PolygonsProperty = DependencyProperty.Register( nameof(Polygons), typeof(IEnumerable>), typeof(MapMultiPolygon), new PropertyMetadata(null, (o, e) => ((MapMultiPolygon)o).DataCollectionPropertyChanged(e))); + public MapMultiPolygon() + : base(new PathGeometry()) + { + } + /// /// Gets or sets the Locations that define the multi-polygon points. /// @@ -34,7 +39,8 @@ namespace MapControl protected override void UpdateData() { - Data.Figures.Clear(); + var figures = ((PathGeometry)Data).Figures; + figures.Clear(); if (ParentMap != null && Polygons != null) { @@ -43,7 +49,7 @@ namespace MapControl var points = polygon.Select(loc => LocationToPoint(loc)); var polyline = new PolyLineSegment(points.Skip(1), true); - Data.Figures.Add(new PathFigure(points.First(), new PathSegment[] { polyline }, true)); + figures.Add(new PathFigure(points.First(), new PathSegment[] { polyline }, true)); } } } diff --git a/MapControl/WPF/MapPolygon.WPF.cs b/MapControl/WPF/MapPolygon.WPF.cs index eea01ffb..94b49605 100644 --- a/MapControl/WPF/MapPolygon.WPF.cs +++ b/MapControl/WPF/MapPolygon.WPF.cs @@ -19,6 +19,16 @@ namespace MapControl nameof(Locations), typeof(IEnumerable), typeof(MapPolygon), new PropertyMetadata(null, (o, e) => ((MapPolygon)o).DataCollectionPropertyChanged(e))); + public static readonly DependencyProperty FillRuleProperty = DependencyProperty.Register( + nameof(FillRule), typeof(FillRule), typeof(MapPolygon), + new FrameworkPropertyMetadata(FillRule.EvenOdd, FrameworkPropertyMetadataOptions.AffectsRender, + (o, e) => ((PathGeometry)((MapPolygon)o).Data).FillRule = (FillRule)e.NewValue)); + + public MapPolygon() + : base(new PathGeometry()) + { + } + /// /// Gets or sets the Locations that define the polygon points. /// @@ -29,16 +39,26 @@ namespace MapControl set { SetValue(LocationsProperty, value); } } + /// + /// Gets or sets the FillRule of the PathGeometry that represents the polyline. + /// + public FillRule FillRule + { + get { return (FillRule)GetValue(FillRuleProperty); } + set { SetValue(FillRuleProperty, value); } + } + protected override void UpdateData() { - Data.Figures.Clear(); + var figures = ((PathGeometry)Data).Figures; + figures.Clear(); if (ParentMap != null && Locations != null && Locations.Count() >= 2) { var points = Locations.Select(loc => LocationToPoint(loc)); var polyline = new PolyLineSegment(points.Skip(1), true); - Data.Figures.Add(new PathFigure(points.First(), new PathSegment[] { polyline }, true)); + figures.Add(new PathFigure(points.First(), new PathSegment[] { polyline }, true)); } } } diff --git a/MapControl/WPF/MapPolyline.WPF.cs b/MapControl/WPF/MapPolyline.WPF.cs index c413d7b8..3653b980 100644 --- a/MapControl/WPF/MapPolyline.WPF.cs +++ b/MapControl/WPF/MapPolyline.WPF.cs @@ -19,6 +19,11 @@ namespace MapControl nameof(Locations), typeof(IEnumerable), typeof(MapPolyline), new PropertyMetadata(null, (o, e) => ((MapPolyline)o).DataCollectionPropertyChanged(e))); + public MapPolyline() + : base(new PathGeometry()) + { + } + /// /// Gets or sets the Locations that define the polyline points. /// @@ -31,14 +36,15 @@ namespace MapControl protected override void UpdateData() { - Data.Figures.Clear(); + var figures = ((PathGeometry)Data).Figures; + figures.Clear(); if (ParentMap != null && Locations != null && Locations.Count() >= 2) { var points = Locations.Select(loc => LocationToPoint(loc)); var polyline = new PolyLineSegment(points.Skip(1), true); - Data.Figures.Add(new PathFigure(points.First(), new PathSegment[] { polyline }, false)); + figures.Add(new PathFigure(points.First(), new PathSegment[] { polyline }, false)); } } } diff --git a/MapControl/WPF/MapShape.WPF.cs b/MapControl/WPF/MapShape.WPF.cs index aee38ff0..8ac781bc 100644 --- a/MapControl/WPF/MapShape.WPF.cs +++ b/MapControl/WPF/MapShape.WPF.cs @@ -12,21 +12,7 @@ namespace MapControl { public abstract partial class MapShape : Shape, IWeakEventListener { - public static readonly DependencyProperty FillRuleProperty = DependencyProperty.Register( - nameof(FillRule), typeof(FillRule), typeof(MapShape), - new FrameworkPropertyMetadata(FillRule.EvenOdd, FrameworkPropertyMetadataOptions.AffectsRender, - (o, e) => ((MapShape)o).Data.FillRule = (FillRule)e.NewValue)); - - /// - /// Gets or sets the FillRule of the StreamGeometry that represents the polyline. - /// - public FillRule FillRule - { - get { return (FillRule)GetValue(FillRuleProperty); } - set { SetValue(FillRuleProperty, value); } - } - - protected PathGeometry Data { get; } + protected Geometry Data { get; } protected override Geometry DefiningGeometry { @@ -38,7 +24,9 @@ namespace MapControl if (parentMap != null) { var transform = new TransformGroup(); - transform.Children.Add(new TranslateTransform(GetLongitudeOffset() * parentMap.MapProjection.TrueScale, 0d)); + var offsetX = GetLongitudeOffset() * parentMap.MapProjection.TrueScale; + + transform.Children.Add(new TranslateTransform(offsetX, 0d)); transform.Children.Add(parentMap.MapProjection.ViewportTransform); Data.Transform = transform;