XAML-Map-Control/MapControl/MapPolyline.cs

83 lines
2.7 KiB
C#
Raw Normal View History

// XAML Map Control - http://xamlmapcontrol.codeplex.com/
2012-05-04 12:52:20 +02:00
// Copyright © 2012 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System.Linq;
#if WINRT
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
#else
2012-04-25 22:02:53 +02:00
using System.Windows;
using System.Windows.Media;
#endif
2012-04-25 22:02:53 +02:00
namespace MapControl
{
public partial class MapPolyline : IMapElement
2012-04-25 22:02:53 +02:00
{
2012-05-04 12:52:20 +02:00
public static readonly DependencyProperty LocationsProperty = DependencyProperty.Register(
"Locations", typeof(LocationCollection), typeof(MapPolyline),
new PropertyMetadata(null, (o, e) => ((MapPolyline)o).UpdateGeometry()));
public static readonly DependencyProperty IsClosedProperty = DependencyProperty.Register(
"IsClosed", typeof(bool), typeof(MapPolyline),
new PropertyMetadata(false, (o, e) => ((MapPolyline)o).UpdateGeometry()));
2012-04-25 22:02:53 +02:00
protected PathGeometry Geometry = new PathGeometry();
2012-04-25 22:02:53 +02:00
/// <summary>
/// Gets or sets the locations that define the polyline points.
/// </summary>
2012-05-04 12:52:20 +02:00
public LocationCollection Locations
{
get { return (LocationCollection)GetValue(LocationsProperty); }
set { SetValue(LocationsProperty, value); }
}
/// <summary>
/// Gets or sets a value that indicates if the polyline is closed, i.e. is a polygon.
/// </summary>
public bool IsClosed
2012-05-04 12:52:20 +02:00
{
get { return (bool)GetValue(IsClosedProperty); }
set { SetValue(IsClosedProperty, value); }
2012-04-25 22:02:53 +02:00
}
2012-05-04 12:52:20 +02:00
protected virtual void UpdateGeometry()
2012-05-04 12:52:20 +02:00
{
var parentMap = MapPanel.GetParentMap(this);
var locations = Locations;
if (parentMap != null && locations != null && locations.Count > 0)
2012-05-04 12:52:20 +02:00
{
var figure = new PathFigure
{
StartPoint = parentMap.MapTransform.Transform(locations[0]),
IsClosed = IsClosed,
IsFilled = IsClosed
};
2012-05-04 12:52:20 +02:00
if (locations.Count > 1)
2012-05-04 12:52:20 +02:00
{
var segment = new PolyLineSegment();
2012-05-04 12:52:20 +02:00
foreach (Location location in locations.Skip(1))
{
segment.Points.Add(parentMap.MapTransform.Transform(location));
}
2012-10-25 08:42:51 +02:00
figure.Segments.Add(segment);
}
2012-10-25 08:42:51 +02:00
Geometry.Figures.Add(figure);
Geometry.Transform = parentMap.ViewportTransform;
2012-10-25 08:42:51 +02:00
}
}
void IMapElement.ParentMapChanged(MapBase oldParentMap, MapBase newParentMap)
2012-10-25 08:42:51 +02:00
{
UpdateGeometry();
2012-10-25 08:42:51 +02:00
}
2012-04-25 22:02:53 +02:00
}
}