// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2018 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Media;
namespace MapControl
{
///
/// A polygon defined by a collection of Locations.
///
public class MapPolygon : MapShape
{
public static readonly DependencyProperty LocationsProperty = DependencyProperty.Register(
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.
///
[TypeConverter(typeof(LocationCollectionConverter))]
public IEnumerable Locations
{
get { return (IEnumerable)GetValue(LocationsProperty); }
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()
{
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);
figures.Add(new PathFigure(points.First(), new PathSegment[] { polyline }, true));
}
}
}
}