Added properties MapPanel.ViewportPosition and MapPolyline.IsClosed, removed MapPolygon class.

This commit is contained in:
ClemensF 2012-11-26 19:17:12 +01:00
parent 841683f198
commit b0ecd850a2
13 changed files with 93 additions and 66 deletions

View file

@ -10,8 +10,11 @@ namespace MapControl
{ {
public partial class Map public partial class Map
{ {
partial void Initialize() private Point? mousePosition;
public Map()
{ {
MouseWheelZoomChange = 1d;
MouseWheel += OnMouseWheel; MouseWheel += OnMouseWheel;
MouseLeftButtonDown += OnMouseLeftButtonDown; MouseLeftButtonDown += OnMouseLeftButtonDown;
MouseLeftButtonUp += OnMouseLeftButtonUp; MouseLeftButtonUp += OnMouseLeftButtonUp;
@ -26,7 +29,7 @@ namespace MapControl
private void OnMouseWheel(object sender, MouseWheelEventArgs e) private void OnMouseWheel(object sender, MouseWheelEventArgs e)
{ {
ZoomMap(e.GetPosition(this), TargetZoomLevel + mouseWheelZoom * Math.Sign(e.Delta)); ZoomMap(e.GetPosition(this), TargetZoomLevel + MouseWheelZoomChange * Math.Sign(e.Delta));
} }
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)

View file

@ -11,8 +11,11 @@ namespace MapControl
{ {
public partial class Map public partial class Map
{ {
partial void Initialize() private Point? mousePosition;
public Map()
{ {
MouseWheelZoomChange = 1d;
ManipulationMode = ManipulationModes.All; ManipulationMode = ManipulationModes.All;
ManipulationDelta += OnManipulationDelta; ManipulationDelta += OnManipulationDelta;
PointerWheelChanged += OnPointerWheelChanged; PointerWheelChanged += OnPointerWheelChanged;
@ -34,7 +37,7 @@ namespace MapControl
private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e) private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e)
{ {
var point = e.GetCurrentPoint(this); var point = e.GetCurrentPoint(this);
ZoomMap(point.Position, TargetZoomLevel + mouseWheelZoom * Math.Sign(point.Properties.MouseWheelDelta)); ZoomMap(point.Position, TargetZoomLevel + MouseWheelZoomChange * Math.Sign(point.Properties.MouseWheelDelta));
} }
private void OnPointerPressed(object sender, PointerRoutedEventArgs e) private void OnPointerPressed(object sender, PointerRoutedEventArgs e)

View file

@ -2,12 +2,6 @@
// Copyright © 2012 Clemens Fischer // Copyright © 2012 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL) // Licensed under the Microsoft Public License (Ms-PL)
#if WINRT
using Windows.Foundation;
#else
using System.Windows;
#endif
namespace MapControl namespace MapControl
{ {
/// <summary> /// <summary>
@ -15,20 +9,9 @@ namespace MapControl
/// </summary> /// </summary>
public partial class Map : MapBase public partial class Map : MapBase
{ {
private double mouseWheelZoom = 1d; /// <summary>
private Point? mousePosition; /// Gets or sets the amount by which the ZoomLevel property changes during a MouseWheel event.
/// </summary>
public Map() public double MouseWheelZoomChange { get; set; }
{
Initialize();
}
partial void Initialize();
public double MouseWheelZoom
{
get { return mouseWheelZoom; }
set { mouseWheelZoom = value; }
}
} }
} }

View file

@ -124,30 +124,49 @@ namespace MapControl
/// </summary> /// </summary>
public event EventHandler ViewportChanged; public event EventHandler ViewportChanged;
/// <summary>
/// Gets or sets the map foreground Brush.
/// </summary>
public Brush Foreground public Brush Foreground
{ {
get { return (Brush)GetValue(ForegroundProperty); } get { return (Brush)GetValue(ForegroundProperty); }
set { SetValue(ForegroundProperty, value); } set { SetValue(ForegroundProperty, value); }
} }
/// <summary>
/// Gets or sets a Brush that (when not null) is used as value of the
/// Foreground property when TileLayer.HasDarkBackground is false.
/// </summary>
public Brush LightForeground public Brush LightForeground
{ {
get { return (Brush)GetValue(LightForegroundProperty); } get { return (Brush)GetValue(LightForegroundProperty); }
set { SetValue(LightForegroundProperty, value); } set { SetValue(LightForegroundProperty, value); }
} }
/// <summary>
/// Gets or sets a Brush that (when not null) is used as value of the
/// Foreground property when TileLayer.HasDarkBackground is true.
/// </summary>
public Brush DarkForeground public Brush DarkForeground
{ {
get { return (Brush)GetValue(DarkForegroundProperty); } get { return (Brush)GetValue(DarkForegroundProperty); }
set { SetValue(DarkForegroundProperty, value); } set { SetValue(DarkForegroundProperty, value); }
} }
/// <summary>
/// Gets or sets a Brush that (when not null) is used as value of the
/// Background property when TileLayer.HasDarkBackground is false.
/// </summary>
public Brush LightBackground public Brush LightBackground
{ {
get { return (Brush)GetValue(LightBackgroundProperty); } get { return (Brush)GetValue(LightBackgroundProperty); }
set { SetValue(LightBackgroundProperty, value); } set { SetValue(LightBackgroundProperty, value); }
} }
/// <summary>
/// Gets or sets a Brush that (when not null) is used as value of the
/// Background property when TileLayer.HasDarkBackground is true.
/// </summary>
public Brush DarkBackground public Brush DarkBackground
{ {
get { return (Brush)GetValue(DarkBackgroundProperty); } get { return (Brush)GetValue(DarkBackgroundProperty); }

View file

@ -83,7 +83,6 @@
<Compile Include="MapOverlay.Silverlight.WinRT.cs" /> <Compile Include="MapOverlay.Silverlight.WinRT.cs" />
<Compile Include="MapPanel.cs" /> <Compile Include="MapPanel.cs" />
<Compile Include="MapPanel.Silverlight.WinRT.cs" /> <Compile Include="MapPanel.Silverlight.WinRT.cs" />
<Compile Include="MapPolygon.cs" />
<Compile Include="MapPolyline.cs" /> <Compile Include="MapPolyline.cs" />
<Compile Include="MapPolyline.Silverlight.cs" /> <Compile Include="MapPolyline.Silverlight.cs" />
<Compile Include="MapTransform.cs" /> <Compile Include="MapTransform.cs" />

View file

@ -21,6 +21,9 @@ namespace MapControl
typeof(MapItem), new FrameworkPropertyMetadata(typeof(MapItem))); typeof(MapItem), new FrameworkPropertyMetadata(typeof(MapItem)));
} }
/// <summary>
/// Gets a value that indicates if the MapItem is the CurrentItem of the containing items collection.
/// </summary>
public bool IsCurrent public bool IsCurrent
{ {
get { return (bool)GetValue(IsCurrentProperty); } get { return (bool)GetValue(IsCurrentProperty); }

View file

@ -33,6 +33,10 @@ namespace MapControl
Items.CurrentChanged += OnCurrentItemChanged; Items.CurrentChanged += OnCurrentItemChanged;
} }
/// <summary>
/// Gets or sets a Geometry that selects all items that lie inside its fill area,
/// i.e. where Geometry.FillContains returns true for the item's viewport position.
/// </summary>
public Geometry SelectionGeometry public Geometry SelectionGeometry
{ {
get { return (Geometry)GetValue(SelectionGeometryProperty); } get { return (Geometry)GetValue(SelectionGeometryProperty); }
@ -99,10 +103,11 @@ namespace MapControl
private bool IsItemInGeometry(object item, Geometry geometry) private bool IsItemInGeometry(object item, Geometry geometry)
{ {
var container = ContainerFromItem(item); var container = ContainerFromItem(item);
Point? viewportPosition;
return container != null && return container != null &&
container.RenderTransform != null && (viewportPosition = MapPanel.GetViewportPosition(container)).HasValue &&
geometry.FillContains(new Point(container.RenderTransform.Value.OffsetX, container.RenderTransform.Value.OffsetY)); geometry.FillContains(viewportPosition.Value);
} }
} }
} }

View file

@ -31,6 +31,9 @@ namespace MapControl
public static readonly DependencyProperty LocationProperty = DependencyProperty.RegisterAttached( public static readonly DependencyProperty LocationProperty = DependencyProperty.RegisterAttached(
"Location", typeof(Location), typeof(MapPanel), new PropertyMetadata(null, LocationPropertyChanged)); "Location", typeof(Location), typeof(MapPanel), new PropertyMetadata(null, LocationPropertyChanged));
public static readonly DependencyProperty ViewportPositionProperty = DependencyProperty.RegisterAttached(
"ViewportPosition", typeof(Point?), typeof(MapPanel), null);
public static Location GetLocation(UIElement element) public static Location GetLocation(UIElement element)
{ {
return (Location)element.GetValue(LocationProperty); return (Location)element.GetValue(LocationProperty);
@ -41,6 +44,11 @@ namespace MapControl
element.SetValue(LocationProperty, value); element.SetValue(LocationProperty, value);
} }
public static Point? GetViewportPosition(UIElement element)
{
return (Point?)element.GetValue(ViewportPositionProperty);
}
protected override Size MeasureOverride(Size availableSize) protected override Size MeasureOverride(Size availableSize)
{ {
foreach (UIElement element in Children) foreach (UIElement element in Children)
@ -174,21 +182,28 @@ namespace MapControl
private static void SetViewportPosition(UIElement element, MapBase parentMap, Location location) private static void SetViewportPosition(UIElement element, MapBase parentMap, Location location)
{ {
Transform transform = null; Point? viewportPosition = null;
if (parentMap != null && location != null) if (parentMap != null && location != null)
{ {
Point position = parentMap.LocationToViewportPoint(location); viewportPosition = parentMap.LocationToViewportPoint(location);
transform = new TranslateTransform { X = position.X, Y = position.Y }; element.SetValue(ViewportPositionProperty, viewportPosition);
}
else
{
element.ClearValue(ViewportPositionProperty);
} }
var transformGroup = element.RenderTransform as TransformGroup; var transformGroup = element.RenderTransform as TransformGroup;
if (transformGroup != null) if (transformGroup != null)
{ {
if (transform == null) var transform = new TranslateTransform();
if (viewportPosition.HasValue)
{ {
transform = new TranslateTransform(); transform.X = viewportPosition.Value.X;
transform.Y = viewportPosition.Value.Y;
} }
var transformIndex = transformGroup.Children.Count - 1; var transformIndex = transformGroup.Children.Count - 1;
@ -203,9 +218,13 @@ namespace MapControl
transformGroup.Children.Add(transform); transformGroup.Children.Add(transform);
} }
} }
else if (transform != null) else if (viewportPosition.HasValue)
{ {
element.RenderTransform = transform; element.RenderTransform = new TranslateTransform
{
X = viewportPosition.Value.X,
Y = viewportPosition.Value.Y
};
} }
else else
{ {

View file

@ -1,14 +0,0 @@
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
// Copyright © 2012 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
namespace MapControl
{
public class MapPolygon : MapPolyline
{
protected override bool IsClosed
{
get { return true; }
}
}
}

View file

@ -17,23 +17,37 @@ namespace MapControl
{ {
public static readonly DependencyProperty LocationsProperty = DependencyProperty.Register( public static readonly DependencyProperty LocationsProperty = DependencyProperty.Register(
"Locations", typeof(LocationCollection), typeof(MapPolyline), "Locations", typeof(LocationCollection), typeof(MapPolyline),
new PropertyMetadata(null, LocationsPropertyChanged)); 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()));
protected PathGeometry Geometry = new PathGeometry(); protected PathGeometry Geometry = new PathGeometry();
/// <summary>
/// Gets or sets the locations that define the polyline points.
/// </summary>
public LocationCollection Locations public LocationCollection Locations
{ {
get { return (LocationCollection)GetValue(LocationsProperty); } get { return (LocationCollection)GetValue(LocationsProperty); }
set { SetValue(LocationsProperty, value); } set { SetValue(LocationsProperty, value); }
} }
protected virtual bool IsClosed /// <summary>
/// Gets or sets a value that indicates if the polyline is closed, i.e. is a polygon.
/// </summary>
public bool IsClosed
{ {
get { return false; } get { return (bool)GetValue(IsClosedProperty); }
set { SetValue(IsClosedProperty, value); }
} }
protected virtual void UpdateGeometry(MapBase parentMap, LocationCollection locations) protected virtual void UpdateGeometry()
{ {
var parentMap = MapPanel.GetParentMap(this);
var locations = Locations;
if (parentMap != null && locations != null && locations.Count > 0) if (parentMap != null && locations != null && locations.Count > 0)
{ {
var figure = new PathFigure var figure = new PathFigure
@ -62,13 +76,7 @@ namespace MapControl
void IMapElement.ParentMapChanged(MapBase oldParentMap, MapBase newParentMap) void IMapElement.ParentMapChanged(MapBase oldParentMap, MapBase newParentMap)
{ {
UpdateGeometry(newParentMap, Locations); UpdateGeometry();
}
private static void LocationsPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var polyline = (MapPolyline)obj;
polyline.UpdateGeometry(MapPanel.GetParentMap(polyline), (LocationCollection)e.NewValue);
} }
} }
} }

View file

@ -11,9 +11,11 @@ using System.Windows;
namespace MapControl namespace MapControl
{ {
/// <summary> /// <summary>
/// Defines a normal cylindrical projection. Latitude and longitude values in degrees /// Defines a normal cylindrical projection. Latitude and longitude values in degrees are
/// are transformed to cartesian coordinates with origin at latitude = 0 and longitude = 0. /// transformed to cartesian coordinates with origin at latitude = 0 and longitude = 0.
/// Longitude values are transformed identically to x values in the interval [-180 .. 180]. /// Longitude values are transformed identically to x values in the interval [-180 .. 180].
/// Latitude values in the interval [-MaxLatitude .. MaxLatitude] are transformed to y values in
/// the interval [-180 .. 180] according to the actual projection, e.g. the Mercator projection.
/// </summary> /// </summary>
public abstract class MapTransform public abstract class MapTransform
{ {

View file

@ -13,7 +13,7 @@ namespace MapControl
{ {
/// <summary> /// <summary>
/// Transforms latitude and longitude values in degrees to cartesian coordinates /// Transforms latitude and longitude values in degrees to cartesian coordinates
/// according to the Mercator transform. /// according to the Mercator projection.
/// </summary> /// </summary>
public class MercatorTransform : MapTransform public class MercatorTransform : MapTransform
{ {

View file

@ -153,9 +153,6 @@
<Compile Include="..\MapPanel.Silverlight.WinRT.cs"> <Compile Include="..\MapPanel.Silverlight.WinRT.cs">
<Link>MapPanel.Silverlight.WinRT.cs</Link> <Link>MapPanel.Silverlight.WinRT.cs</Link>
</Compile> </Compile>
<Compile Include="..\MapPolygon.cs">
<Link>MapPolygon.cs</Link>
</Compile>
<Compile Include="..\MapPolyline.cs"> <Compile Include="..\MapPolyline.cs">
<Link>MapPolyline.cs</Link> <Link>MapPolyline.cs</Link>
</Compile> </Compile>