XAML-Map-Control/MapControl/MapPolyline.cs

83 lines
2.5 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, LocationsPropertyChanged));
2012-04-25 22:02:53 +02:00
protected PathGeometry Geometry = new PathGeometry();
2012-04-25 22:02:53 +02:00
2012-10-25 08:42:51 +02:00
public MapPolyline()
{
MapPanel.AddParentMapHandlers(this);
Initialize();
2012-04-25 22:02:53 +02:00
}
partial void Initialize();
2012-04-25 22:02:53 +02:00
2012-05-04 12:52:20 +02:00
public LocationCollection Locations
{
get { return (LocationCollection)GetValue(LocationsProperty); }
set { SetValue(LocationsProperty, value); }
}
protected virtual bool IsClosed
2012-05-04 12:52:20 +02:00
{
get { return false; }
2012-04-25 22:02:53 +02:00
}
2012-05-04 12:52:20 +02:00
protected virtual void UpdateGeometry(MapBase parentMap, LocationCollection locations)
2012-05-04 12:52:20 +02:00
{
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(newParentMap, Locations);
2012-10-25 08:42:51 +02:00
}
private static void LocationsPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
2012-10-25 08:42:51 +02:00
{
var polyline = (MapPolyline)obj;
polyline.UpdateGeometry(MapPanel.GetParentMap(polyline), (LocationCollection)e.NewValue);
2012-10-25 08:42:51 +02:00
}
2012-04-25 22:02:53 +02:00
}
}