Version 4.4.1: MapPolygon for UWP in progress

This commit is contained in:
ClemensF 2018-02-14 18:10:53 +01:00
parent 2f828fbe2d
commit 7246cbd002
8 changed files with 109 additions and 141 deletions

View file

@ -3,7 +3,6 @@
// Licensed under the Microsoft Public License (Ms-PL)
using System.Collections.Generic;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
@ -18,11 +17,6 @@ namespace MapControl
nameof(Locations), typeof(IEnumerable<Location>), typeof(MapPolygon),
new PropertyMetadata(null, (o, e) => ((MapPolygon)o).LocationsPropertyChanged(e)));
public MapPolygon()
: base(new PathGeometry())
{
}
/// <summary>
/// Gets or sets the Locations that define the polyline points.
/// </summary>
@ -34,22 +28,12 @@ namespace MapControl
protected override void UpdateData()
{
((PathGeometry)Data).Figures.Clear();
var figures = ((PathGeometry)Data).Figures;
figures.Clear();
if (ParentMap != null && Locations != null && Locations.Count() >= 2)
if (ParentMap != null)
{
var locations = Locations;
var offset = GetLongitudeOffset();
if (offset != 0d)
{
locations = locations.Select(loc => new Location(loc.Latitude, loc.Longitude + offset));
}
var points = locations.Select(loc => LocationToViewportPoint(loc)).ToList();
points.Add(points[0]);
CreatePolylineFigures(points);
AddPolylineFigures(figures, Locations, true);
}
}
}

View file

@ -3,7 +3,6 @@
// Licensed under the Microsoft Public License (Ms-PL)
using System.Collections.Generic;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
@ -18,11 +17,6 @@ namespace MapControl
nameof(Locations), typeof(IEnumerable<Location>), typeof(MapPolyline),
new PropertyMetadata(null, (o, e) => ((MapPolyline)o).LocationsPropertyChanged(e)));
public MapPolyline()
: base(new PathGeometry())
{
}
/// <summary>
/// Gets or sets the Locations that define the polyline points.
/// </summary>
@ -34,21 +28,12 @@ namespace MapControl
protected override void UpdateData()
{
((PathGeometry)Data).Figures.Clear();
var figures = ((PathGeometry)Data).Figures;
figures.Clear();
if (ParentMap != null && Locations != null && Locations.Count() >= 2)
if (ParentMap != null)
{
var locations = Locations;
var offset = GetLongitudeOffset();
if (offset != 0d)
{
locations = locations.Select(loc => new Location(loc.Latitude, loc.Longitude + offset));
}
var points = locations.Select(loc => LocationToViewportPoint(loc)).ToList();
CreatePolylineFigures(points);
AddPolylineFigures(figures, Locations, false);
}
}
}

View file

@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
@ -13,11 +14,6 @@ namespace MapControl
{
public abstract partial class MapShape : Path
{
private void ParentMapChanged()
{
UpdateData();
}
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
{
UpdateData();
@ -25,17 +21,16 @@ namespace MapControl
protected void LocationsPropertyChanged(DependencyPropertyChangedEventArgs e)
{
var oldCollection = e.OldValue as INotifyCollectionChanged;
var newCollection = e.NewValue as INotifyCollectionChanged;
INotifyCollectionChanged collection;
if (oldCollection != null)
if ((collection = e.OldValue as INotifyCollectionChanged) != null)
{
oldCollection.CollectionChanged -= LocationCollectionChanged;
collection.CollectionChanged -= LocationCollectionChanged;
}
if (newCollection != null)
if ((collection = e.NewValue as INotifyCollectionChanged) != null)
{
newCollection.CollectionChanged += LocationCollectionChanged;
collection.CollectionChanged += LocationCollectionChanged;
}
UpdateData();
@ -46,36 +41,56 @@ namespace MapControl
UpdateData();
}
protected void CreatePolylineFigures(IList<Point> points)
protected void AddPolylineFigures(PathFigureCollection figures, IEnumerable<Location> locations, bool closed)
{
var viewport = new Rect(0, 0, ParentMap.RenderSize.Width, ParentMap.RenderSize.Height);
var figures = ((PathGeometry)Data).Figures;
PathFigure figure = null;
PolyLineSegment segment = null;
for (int i = 1; i < points.Count; i++)
if (locations != null && locations.Count() >= 2)
{
var p1 = points[i - 1];
var p2 = points[i];
var inside = Intersections.GetIntersections(ref p1, ref p2, viewport);
var viewport = new Rect(0, 0, ParentMap.RenderSize.Width, ParentMap.RenderSize.Height);
var offset = GetLongitudeOffset();
if (inside)
if (offset != 0d)
{
if (figure == null)
{
figure = new PathFigure { StartPoint = p1, IsClosed = false, IsFilled = false };
segment = new PolyLineSegment();
figure.Segments.Add(segment);
figures.Add(figure);
}
segment.Points.Add(p2);
locations = locations.Select(loc => new Location(loc.Latitude, loc.Longitude + offset));
}
if (!inside || p2 != points[i])
var points = locations.Select(loc => LocationToViewportPoint(loc)).ToList();
if (closed)
{
figure = null;
points.Add(points[0]);
}
PathFigure figure = null;
PolyLineSegment segment = null;
for (int i = 1; i < points.Count; i++)
{
var p1 = points[i - 1];
var p2 = points[i];
var inside = Intersections.GetIntersections(ref p1, ref p2, viewport);
if (inside)
{
if (figure == null)
{
figure = new PathFigure
{
StartPoint = p1,
IsClosed = false,
IsFilled = false
};
segment = new PolyLineSegment();
figure.Segments.Add(segment);
figures.Add(figure);
}
segment.Points.Add(p2);
}
if (!inside || p2 != points[i])
{
figure = null;
}
}
}
}