2012-11-22 21:42:29 +01:00
|
|
|
|
// 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)
|
|
|
|
|
|
|
2013-01-03 17:07:59 +01:00
|
|
|
|
using System;
|
2012-05-04 12:52:20 +02:00
|
|
|
|
using System.Linq;
|
2012-12-15 18:54:40 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Collections.Specialized;
|
2012-12-09 22:18:31 +01:00
|
|
|
|
#if NETFX_CORE
|
2012-11-22 21:42:29 +01:00
|
|
|
|
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;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
#endif
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
public partial class MapPolyline : IMapElement
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-11-26 19:17:12 +01:00
|
|
|
|
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
|
|
|
|
|
2012-12-15 18:54:40 +01:00
|
|
|
|
protected readonly PathGeometry Geometry = new PathGeometry();
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2012-11-26 19:17:12 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the locations that define the polyline points.
|
|
|
|
|
|
/// </summary>
|
2012-12-15 18:54:40 +01:00
|
|
|
|
public IEnumerable<Location> Locations
|
2012-05-04 12:52:20 +02:00
|
|
|
|
{
|
2012-12-15 18:54:40 +01:00
|
|
|
|
get { return (IEnumerable<Location>)GetValue(LocationsProperty); }
|
2012-05-04 12:52:20 +02:00
|
|
|
|
set { SetValue(LocationsProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-26 19:17:12 +01:00
|
|
|
|
/// <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
|
|
|
|
{
|
2012-11-26 19:17:12 +01: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
|
|
|
|
|
2012-12-15 18:54:40 +01:00
|
|
|
|
private void UpdateGeometry()
|
2012-05-04 12:52:20 +02:00
|
|
|
|
{
|
2012-11-26 19:17:12 +01:00
|
|
|
|
var parentMap = MapPanel.GetParentMap(this);
|
|
|
|
|
|
var locations = Locations;
|
2012-12-15 18:54:40 +01:00
|
|
|
|
Location first;
|
2012-11-26 19:17:12 +01:00
|
|
|
|
|
2012-12-15 18:54:40 +01:00
|
|
|
|
Geometry.Figures.Clear();
|
|
|
|
|
|
|
|
|
|
|
|
if (parentMap != null && locations != null && (first = locations.FirstOrDefault()) != null)
|
2012-05-04 12:52:20 +02:00
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
var figure = new PathFigure
|
|
|
|
|
|
{
|
2012-12-15 18:54:40 +01:00
|
|
|
|
StartPoint = parentMap.MapTransform.Transform(first),
|
2012-11-22 21:42:29 +01:00
|
|
|
|
IsClosed = IsClosed,
|
|
|
|
|
|
IsFilled = IsClosed
|
|
|
|
|
|
};
|
2012-05-04 12:52:20 +02:00
|
|
|
|
|
2012-12-15 18:54:40 +01:00
|
|
|
|
var segment = new PolyLineSegment();
|
2012-05-04 12:52:20 +02:00
|
|
|
|
|
2012-12-15 18:54:40 +01:00
|
|
|
|
foreach (var location in locations.Skip(1))
|
|
|
|
|
|
{
|
|
|
|
|
|
segment.Points.Add(parentMap.MapTransform.Transform(location));
|
|
|
|
|
|
}
|
2012-10-25 08:42:51 +02:00
|
|
|
|
|
2012-12-15 18:54:40 +01:00
|
|
|
|
if (segment.Points.Count > 0)
|
|
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
figure.Segments.Add(segment);
|
|
|
|
|
|
}
|
2012-10-25 08:42:51 +02:00
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
Geometry.Figures.Add(figure);
|
|
|
|
|
|
Geometry.Transform = parentMap.ViewportTransform;
|
2012-10-25 08:42:51 +02:00
|
|
|
|
}
|
2012-12-15 18:54:40 +01:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Geometry.Transform = null;
|
|
|
|
|
|
}
|
2012-10-25 08:42:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
void IMapElement.ParentMapChanged(MapBase oldParentMap, MapBase newParentMap)
|
2012-10-25 08:42:51 +02:00
|
|
|
|
{
|
2012-11-26 19:17:12 +01:00
|
|
|
|
UpdateGeometry();
|
2012-10-25 08:42:51 +02:00
|
|
|
|
}
|
2012-12-15 18:54:40 +01:00
|
|
|
|
|
|
|
|
|
|
private void LocationCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
UpdateGeometry();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void LocationsPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
var mapPolyline = (MapPolyline)obj;
|
2012-12-15 19:11:54 +01:00
|
|
|
|
var oldCollection = e.OldValue as INotifyCollectionChanged;
|
|
|
|
|
|
var newCollection = e.NewValue as INotifyCollectionChanged;
|
2012-12-15 18:54:40 +01:00
|
|
|
|
|
2012-12-15 19:11:54 +01:00
|
|
|
|
if (oldCollection != null)
|
2012-12-15 18:54:40 +01:00
|
|
|
|
{
|
2012-12-15 19:11:54 +01:00
|
|
|
|
oldCollection.CollectionChanged -= mapPolyline.LocationCollectionChanged;
|
2012-12-15 18:54:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-12-15 19:11:54 +01:00
|
|
|
|
if (newCollection != null)
|
2012-12-15 18:54:40 +01:00
|
|
|
|
{
|
2012-12-15 19:11:54 +01:00
|
|
|
|
newCollection.CollectionChanged += mapPolyline.LocationCollectionChanged;
|
2012-12-15 18:54:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mapPolyline.UpdateGeometry();
|
|
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|