2012-05-04 12:52:20 +02:00
|
|
|
|
// WPF MapControl - http://wpfmapcontrol.codeplex.com/
|
|
|
|
|
|
// Copyright © 2012 Clemens Fischer
|
|
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2012-08-08 20:42:06 +02:00
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
|
using System.Linq;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
using System.Windows.Media.Animation;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// <summary>
|
2012-08-13 19:16:59 +02:00
|
|
|
|
/// The main map control. Draws map content provided by the TileLayers or the BaseTileLayer property.
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// The visible map area is defined by the Center and ZoomLevel properties. The map can be rotated
|
|
|
|
|
|
/// by an angle that is given by the Heading property.
|
|
|
|
|
|
/// Map is a MapPanel and hence can contain map overlays like other MapPanels or MapItemsControls.
|
|
|
|
|
|
/// </summary>
|
2012-04-25 22:02:53 +02:00
|
|
|
|
public partial class Map : MapPanel
|
|
|
|
|
|
{
|
|
|
|
|
|
public const double MeterPerDegree = 1852d * 60d;
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty FontSizeProperty = Control.FontSizeProperty.AddOwner(typeof(Map));
|
|
|
|
|
|
public static readonly DependencyProperty FontFamilyProperty = Control.FontFamilyProperty.AddOwner(typeof(Map));
|
|
|
|
|
|
public static readonly DependencyProperty FontStyleProperty = Control.FontStyleProperty.AddOwner(typeof(Map));
|
|
|
|
|
|
public static readonly DependencyProperty FontWeightProperty = Control.FontWeightProperty.AddOwner(typeof(Map));
|
|
|
|
|
|
public static readonly DependencyProperty FontStretchProperty = Control.FontStretchProperty.AddOwner(typeof(Map));
|
|
|
|
|
|
public static readonly DependencyProperty ForegroundProperty = Control.ForegroundProperty.AddOwner(typeof(Map));
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty LightForegroundProperty = DependencyProperty.Register(
|
|
|
|
|
|
"LightForeground", typeof(Brush), typeof(Map));
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty DarkForegroundProperty = DependencyProperty.Register(
|
|
|
|
|
|
"DarkForeground", typeof(Brush), typeof(Map));
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty LightBackgroundProperty = DependencyProperty.Register(
|
|
|
|
|
|
"LightBackground", typeof(Brush), typeof(Map));
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty DarkBackgroundProperty = DependencyProperty.Register(
|
|
|
|
|
|
"DarkBackground", typeof(Brush), typeof(Map));
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty TileLayersProperty = DependencyProperty.Register(
|
|
|
|
|
|
"TileLayers", typeof(TileLayerCollection), typeof(Map), new FrameworkPropertyMetadata(
|
2012-08-08 20:42:06 +02:00
|
|
|
|
(o, e) => ((Map)o).TileLayersPropertyChanged((TileLayerCollection)e.OldValue, (TileLayerCollection)e.NewValue),
|
2012-06-13 17:33:23 +02:00
|
|
|
|
(o, v) => ((Map)o).CoerceTileLayersProperty((TileLayerCollection)v)));
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2012-08-13 19:16:59 +02:00
|
|
|
|
public static readonly DependencyProperty BaseTileLayerProperty = DependencyProperty.Register(
|
|
|
|
|
|
"BaseTileLayer", typeof(TileLayer), typeof(Map), new FrameworkPropertyMetadata(
|
|
|
|
|
|
(o, e) => ((Map)o).BaseTileLayerPropertyChanged((TileLayer)e.NewValue),
|
|
|
|
|
|
(o, v) => ((Map)o).CoerceBaseTileLayerProperty((TileLayer)v)));
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty TileOpacityProperty = DependencyProperty.Register(
|
|
|
|
|
|
"TileOpacity", typeof(double), typeof(Map), new FrameworkPropertyMetadata(1d,
|
|
|
|
|
|
(o, e) => ((Map)o).tileContainer.Opacity = (double)e.NewValue));
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty CenterProperty = DependencyProperty.Register(
|
2012-05-04 12:52:20 +02:00
|
|
|
|
"Center", typeof(Location), typeof(Map), new FrameworkPropertyMetadata(new Location(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
2012-06-12 20:30:05 +02:00
|
|
|
|
(o, e) => ((Map)o).CenterPropertyChanged((Location)e.NewValue),
|
|
|
|
|
|
(o, v) => ((Map)o).CoerceCenterProperty((Location)v)));
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty TargetCenterProperty = DependencyProperty.Register(
|
2012-05-04 12:52:20 +02:00
|
|
|
|
"TargetCenter", typeof(Location), typeof(Map), new FrameworkPropertyMetadata(new Location(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
2012-06-12 20:30:05 +02:00
|
|
|
|
(o, e) => ((Map)o).TargetCenterPropertyChanged((Location)e.NewValue),
|
|
|
|
|
|
(o, v) => ((Map)o).CoerceCenterProperty((Location)v)));
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty ZoomLevelProperty = DependencyProperty.Register(
|
|
|
|
|
|
"ZoomLevel", typeof(double), typeof(Map), new FrameworkPropertyMetadata(1d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
2012-06-12 20:30:05 +02:00
|
|
|
|
(o, e) => ((Map)o).ZoomLevelPropertyChanged((double)e.NewValue),
|
|
|
|
|
|
(o, v) => ((Map)o).CoerceZoomLevelProperty((double)v)));
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty TargetZoomLevelProperty = DependencyProperty.Register(
|
|
|
|
|
|
"TargetZoomLevel", typeof(double), typeof(Map), new FrameworkPropertyMetadata(1d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
2012-06-12 20:30:05 +02:00
|
|
|
|
(o, e) => ((Map)o).TargetZoomLevelPropertyChanged((double)e.NewValue),
|
|
|
|
|
|
(o, v) => ((Map)o).CoerceZoomLevelProperty((double)v)));
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty HeadingProperty = DependencyProperty.Register(
|
|
|
|
|
|
"Heading", typeof(double), typeof(Map), new FrameworkPropertyMetadata(0d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
2012-06-12 20:30:05 +02:00
|
|
|
|
(o, e) => ((Map)o).HeadingPropertyChanged((double)e.NewValue),
|
|
|
|
|
|
(o, v) => ((Map)o).CoerceHeadingProperty((double)v)));
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty TargetHeadingProperty = DependencyProperty.Register(
|
|
|
|
|
|
"TargetHeading", typeof(double), typeof(Map), new FrameworkPropertyMetadata(0d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
2012-06-12 20:30:05 +02:00
|
|
|
|
(o, e) => ((Map)o).TargetHeadingPropertyChanged((double)e.NewValue),
|
|
|
|
|
|
(o, v) => ((Map)o).CoerceHeadingProperty((double)v)));
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
private static readonly DependencyPropertyKey CenterScalePropertyKey = DependencyProperty.RegisterReadOnly(
|
|
|
|
|
|
"CenterScale", typeof(double), typeof(Map), null);
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty CenterScaleProperty = CenterScalePropertyKey.DependencyProperty;
|
|
|
|
|
|
|
|
|
|
|
|
private readonly TileContainer tileContainer = new TileContainer();
|
2012-05-04 12:52:20 +02:00
|
|
|
|
private readonly MapTransform mapTransform = new MercatorTransform();
|
2012-04-25 22:02:53 +02:00
|
|
|
|
private readonly ScaleTransform scaleTransform = new ScaleTransform();
|
|
|
|
|
|
private readonly RotateTransform rotateTransform = new RotateTransform();
|
|
|
|
|
|
private readonly MatrixTransform scaleRotateTransform = new MatrixTransform();
|
2012-05-04 12:52:20 +02:00
|
|
|
|
private Location transformOrigin;
|
|
|
|
|
|
private Point viewportOrigin;
|
|
|
|
|
|
private LocationAnimation centerAnimation;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
private DoubleAnimation zoomLevelAnimation;
|
|
|
|
|
|
private DoubleAnimation headingAnimation;
|
|
|
|
|
|
private bool updateTransform = true;
|
|
|
|
|
|
|
|
|
|
|
|
public Map()
|
|
|
|
|
|
{
|
|
|
|
|
|
MinZoomLevel = 1;
|
|
|
|
|
|
MaxZoomLevel = 20;
|
|
|
|
|
|
AddVisualChild(tileContainer);
|
2012-06-13 17:33:23 +02:00
|
|
|
|
TileLayers = new TileLayerCollection();
|
2012-08-10 09:41:59 +02:00
|
|
|
|
SetValue(ParentMapPropertyKey, this);
|
2012-06-14 10:43:27 +02:00
|
|
|
|
|
|
|
|
|
|
Loaded += (o, e) =>
|
|
|
|
|
|
{
|
2012-08-13 19:16:59 +02:00
|
|
|
|
if (BaseTileLayer == null)
|
2012-06-14 10:43:27 +02:00
|
|
|
|
{
|
2012-08-13 19:16:59 +02:00
|
|
|
|
BaseTileLayer = new TileLayer
|
2012-06-14 10:43:27 +02:00
|
|
|
|
{
|
2012-08-09 10:22:03 +02:00
|
|
|
|
Name = "OpenStreetMap",
|
2012-06-14 10:43:27 +02:00
|
|
|
|
Description = "© {y} OpenStreetMap Contributors, CC-BY-SA",
|
2012-07-20 21:57:29 +02:00
|
|
|
|
TileSource = new TileSource("http://{c}.tile.openstreetmap.org/{z}/{x}/{y}.png")
|
2012-06-14 10:43:27 +02:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-08-08 20:42:06 +02:00
|
|
|
|
/// Raised when the current viewport has changed.
|
2012-04-25 22:02:53 +02:00
|
|
|
|
/// </summary>
|
2012-08-08 20:42:06 +02:00
|
|
|
|
public event Action ViewportChanged;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
public double MinZoomLevel { get; set; }
|
|
|
|
|
|
public double MaxZoomLevel { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public double FontSize
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (double)GetValue(FontSizeProperty); }
|
|
|
|
|
|
set { SetValue(FontSizeProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public FontFamily FontFamily
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (FontFamily)GetValue(FontFamilyProperty); }
|
|
|
|
|
|
set { SetValue(FontFamilyProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public FontStyle FontStyle
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (FontStyle)GetValue(FontStyleProperty); }
|
|
|
|
|
|
set { SetValue(FontStyleProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public FontWeight FontWeight
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (FontWeight)GetValue(FontWeightProperty); }
|
|
|
|
|
|
set { SetValue(FontWeightProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public FontStretch FontStretch
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (FontStretch)GetValue(FontStretchProperty); }
|
|
|
|
|
|
set { SetValue(FontStretchProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Brush Foreground
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (Brush)GetValue(ForegroundProperty); }
|
|
|
|
|
|
set { SetValue(ForegroundProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Brush LightForeground
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (Brush)GetValue(LightForegroundProperty); }
|
|
|
|
|
|
set { SetValue(LightForegroundProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Brush DarkForeground
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (Brush)GetValue(DarkForegroundProperty); }
|
|
|
|
|
|
set { SetValue(DarkForegroundProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Brush LightBackground
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (Brush)GetValue(LightBackgroundProperty); }
|
|
|
|
|
|
set { SetValue(LightBackgroundProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Brush DarkBackground
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (Brush)GetValue(DarkBackgroundProperty); }
|
|
|
|
|
|
set { SetValue(DarkBackgroundProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the TileLayers used by this Map.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public TileLayerCollection TileLayers
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (TileLayerCollection)GetValue(TileLayersProperty); }
|
|
|
|
|
|
set { SetValue(TileLayersProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-08-13 19:16:59 +02:00
|
|
|
|
/// Gets or sets the base TileLayer used by this Map, i.e. TileLayers[0].
|
2012-04-25 22:02:53 +02:00
|
|
|
|
/// </summary>
|
2012-08-13 19:16:59 +02:00
|
|
|
|
public TileLayer BaseTileLayer
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-13 19:16:59 +02:00
|
|
|
|
get { return (TileLayer)GetValue(BaseTileLayerProperty); }
|
|
|
|
|
|
set { SetValue(BaseTileLayerProperty, value); }
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the opacity of the tile layers.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public double TileOpacity
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (double)GetValue(TileOpacityProperty); }
|
|
|
|
|
|
set { SetValue(TileOpacityProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// Gets or sets the location of the center point of the Map.
|
2012-04-25 22:02:53 +02:00
|
|
|
|
/// </summary>
|
2012-05-04 12:52:20 +02:00
|
|
|
|
public Location Center
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-05-04 12:52:20 +02:00
|
|
|
|
get { return (Location)GetValue(CenterProperty); }
|
2012-04-25 22:02:53 +02:00
|
|
|
|
set { SetValue(CenterProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the target value of a Center animation.
|
|
|
|
|
|
/// </summary>
|
2012-05-04 12:52:20 +02:00
|
|
|
|
public Location TargetCenter
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-05-04 12:52:20 +02:00
|
|
|
|
get { return (Location)GetValue(TargetCenterProperty); }
|
2012-04-25 22:02:53 +02:00
|
|
|
|
set { SetValue(TargetCenterProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the map zoom level.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public double ZoomLevel
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (double)GetValue(ZoomLevelProperty); }
|
|
|
|
|
|
set { SetValue(ZoomLevelProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the target value of a ZoomLevel animation.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public double TargetZoomLevel
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (double)GetValue(TargetZoomLevelProperty); }
|
|
|
|
|
|
set { SetValue(TargetZoomLevelProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-08-08 20:42:06 +02:00
|
|
|
|
/// Gets or sets the map heading, or clockwise rotation angle in degrees.
|
2012-04-25 22:02:53 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public double Heading
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (double)GetValue(HeadingProperty); }
|
|
|
|
|
|
set { SetValue(HeadingProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the target value of a Heading animation.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public double TargetHeading
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (double)GetValue(TargetHeadingProperty); }
|
|
|
|
|
|
set { SetValue(TargetHeadingProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// Gets the map scale at the Center location as viewport coordinate units (pixels) per meter.
|
2012-04-25 22:02:53 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public double CenterScale
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (double)GetValue(CenterScaleProperty); }
|
|
|
|
|
|
private set { SetValue(CenterScalePropertyKey, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// Gets the transformation from geographic coordinates to cartesian map coordinates.
|
2012-04-25 22:02:53 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public MapTransform MapTransform
|
|
|
|
|
|
{
|
2012-05-04 12:52:20 +02:00
|
|
|
|
get { return mapTransform; }
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// Gets the transformation from cartesian map coordinates to viewport coordinates.
|
2012-04-25 22:02:53 +02:00
|
|
|
|
/// </summary>
|
2012-05-04 12:52:20 +02:00
|
|
|
|
public Transform ViewportTransform
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-05-04 12:52:20 +02:00
|
|
|
|
get { return tileContainer.ViewportTransform; }
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// Gets the scaling transformation from meters to viewport coordinate units (pixels)
|
|
|
|
|
|
/// at the viewport center point.
|
2012-04-25 22:02:53 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Transform ScaleTransform
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return scaleTransform; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the transformation that rotates by the value of the Heading property.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Transform RotateTransform
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return rotateTransform; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the combination of ScaleTransform and RotateTransform
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Transform ScaleRotateTransform
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return scaleRotateTransform; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// Transforms a geographic location to a viewport coordinates point.
|
2012-04-25 22:02:53 +02:00
|
|
|
|
/// </summary>
|
2012-05-04 12:52:20 +02:00
|
|
|
|
public Point LocationToViewportPoint(Location location)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ViewportTransform.Transform(MapTransform.Transform(location));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Transforms a viewport coordinates point to a geographic location.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Location ViewportPointToLocation(Point point)
|
|
|
|
|
|
{
|
|
|
|
|
|
return MapTransform.TransformBack(ViewportTransform.Inverse.Transform(point));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sets a temporary origin location in geographic coordinates for scaling and rotation transformations.
|
|
|
|
|
|
/// This origin location is automatically removed when the Center property is set by application code.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void SetTransformOrigin(Location origin)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
|
|
|
|
|
transformOrigin = origin;
|
2012-05-04 12:52:20 +02:00
|
|
|
|
viewportOrigin = LocationToViewportPoint(origin);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// Sets a temporary origin point in viewport coordinates for scaling and rotation transformations.
|
2012-04-25 22:02:53 +02:00
|
|
|
|
/// This origin point is automatically removed when the Center property is set by application code.
|
|
|
|
|
|
/// </summary>
|
2012-05-04 12:52:20 +02:00
|
|
|
|
public void SetTransformOrigin(Point origin)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-05-04 12:52:20 +02:00
|
|
|
|
viewportOrigin.X = Math.Min(Math.Max(origin.X, 0d), RenderSize.Width);
|
|
|
|
|
|
viewportOrigin.Y = Math.Min(Math.Max(origin.Y, 0d), RenderSize.Height);
|
2012-06-12 20:30:05 +02:00
|
|
|
|
transformOrigin = CoerceCenterProperty(ViewportPointToLocation(viewportOrigin));
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// Removes the temporary transform origin point set by SetTransformOrigin.
|
2012-04-25 22:02:53 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void ResetTransformOrigin()
|
|
|
|
|
|
{
|
|
|
|
|
|
transformOrigin = null;
|
2012-05-04 12:52:20 +02:00
|
|
|
|
viewportOrigin = new Point(RenderSize.Width / 2d, RenderSize.Height / 2d);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// Changes the Center property according to the specified translation in viewport coordinates.
|
2012-04-25 22:02:53 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void TranslateMap(Vector translation)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (translation.X != 0d || translation.Y != 0d)
|
|
|
|
|
|
{
|
2012-08-06 17:41:39 +02:00
|
|
|
|
if (transformOrigin != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
viewportOrigin += translation;
|
2012-08-08 20:42:06 +02:00
|
|
|
|
UpdateTransform();
|
2012-08-06 17:41:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Center = ViewportPointToLocation(viewportOrigin - translation);
|
|
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Changes the Center, Heading and ZoomLevel properties according to the specified
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// viewport coordinate translation, rotation and scale delta values. Rotation and scaling
|
|
|
|
|
|
/// is performed relative to the specified origin point in viewport coordinates.
|
2012-04-25 22:02:53 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void TransformMap(Point origin, Vector translation, double rotation, double scale)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (rotation != 0d || scale != 1d)
|
|
|
|
|
|
{
|
2012-05-04 12:52:20 +02:00
|
|
|
|
SetTransformOrigin(origin);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
updateTransform = false;
|
2012-07-04 17:19:48 +02:00
|
|
|
|
Heading = (((Heading + rotation) % 360d) + 360d) % 360d;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
ZoomLevel += Math.Log(scale, 2d);
|
|
|
|
|
|
updateTransform = true;
|
2012-08-08 20:42:06 +02:00
|
|
|
|
UpdateTransform();
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TranslateMap(translation);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sets the value of the ZoomLevel property while retaining the specified origin point
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// in viewport coordinates.
|
2012-04-25 22:02:53 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void ZoomMap(Point origin, double zoomLevel)
|
|
|
|
|
|
{
|
2012-05-04 12:52:20 +02:00
|
|
|
|
SetTransformOrigin(origin);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
TargetZoomLevel = zoomLevel;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// Gets the map scale at the specified location as viewport coordinate units (pixels) per meter.
|
2012-04-25 22:02:53 +02:00
|
|
|
|
/// </summary>
|
2012-05-04 12:52:20 +02:00
|
|
|
|
public double GetMapScale(Location location)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-05-04 12:52:20 +02:00
|
|
|
|
return mapTransform.RelativeScale(location) * Math.Pow(2d, ZoomLevel) * 256d / (MeterPerDegree * 360d);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override int VisualChildrenCount
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return InternalChildren.Count + 1; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override Visual GetVisualChild(int index)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (index == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return tileContainer;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return InternalChildren[index - 1];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.OnRenderSizeChanged(sizeInfo);
|
|
|
|
|
|
|
|
|
|
|
|
ResetTransformOrigin();
|
2012-08-08 20:42:06 +02:00
|
|
|
|
UpdateTransform();
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnRender(DrawingContext drawingContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
drawingContext.DrawRectangle(Background, null, new Rect(RenderSize));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-08 20:42:06 +02:00
|
|
|
|
protected override void OnViewportChanged()
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-08 20:42:06 +02:00
|
|
|
|
base.OnViewportChanged();
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2012-08-08 20:42:06 +02:00
|
|
|
|
if (ViewportChanged != null)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-08 20:42:06 +02:00
|
|
|
|
ViewportChanged();
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-22 20:21:31 +02:00
|
|
|
|
private void TileLayerCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-22 20:21:31 +02:00
|
|
|
|
switch (e.Action)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-08 20:42:06 +02:00
|
|
|
|
case NotifyCollectionChangedAction.Add:
|
2012-08-22 20:21:31 +02:00
|
|
|
|
tileContainer.AddTileLayers(e.NewStartingIndex, e.NewItems.Cast<TileLayer>());
|
2012-08-08 20:42:06 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case NotifyCollectionChangedAction.Remove:
|
2012-08-22 20:21:31 +02:00
|
|
|
|
tileContainer.RemoveTileLayers(e.OldStartingIndex, e.OldItems.Cast<TileLayer>());
|
2012-08-08 20:42:06 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case NotifyCollectionChangedAction.Move:
|
|
|
|
|
|
case NotifyCollectionChangedAction.Replace:
|
2012-08-22 20:21:31 +02:00
|
|
|
|
tileContainer.RemoveTileLayers(e.OldStartingIndex, e.OldItems.Cast<TileLayer>());
|
|
|
|
|
|
tileContainer.AddTileLayers(e.NewStartingIndex, e.NewItems.Cast<TileLayer>());
|
2012-08-08 20:42:06 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case NotifyCollectionChangedAction.Reset:
|
|
|
|
|
|
tileContainer.ClearTileLayers();
|
2012-08-22 20:21:31 +02:00
|
|
|
|
if (e.NewItems != null)
|
2012-08-08 20:42:06 +02:00
|
|
|
|
{
|
2012-08-22 20:21:31 +02:00
|
|
|
|
tileContainer.AddTileLayers(0, e.NewItems.Cast<TileLayer>());
|
2012-08-08 20:42:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
break;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-13 19:16:59 +02:00
|
|
|
|
UpdateBaseTileLayer();
|
2012-08-08 20:42:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void TileLayersPropertyChanged(TileLayerCollection oldTileLayers, TileLayerCollection newTileLayers)
|
|
|
|
|
|
{
|
|
|
|
|
|
tileContainer.ClearTileLayers();
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2012-08-08 20:42:06 +02:00
|
|
|
|
if (oldTileLayers != null)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-08 20:42:06 +02:00
|
|
|
|
oldTileLayers.CollectionChanged -= TileLayerCollectionChanged;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-08 20:42:06 +02:00
|
|
|
|
if (newTileLayers != null)
|
2012-06-13 17:33:23 +02:00
|
|
|
|
{
|
2012-08-08 20:42:06 +02:00
|
|
|
|
newTileLayers.CollectionChanged += TileLayerCollectionChanged;
|
|
|
|
|
|
tileContainer.AddTileLayers(0, newTileLayers);
|
2012-06-13 17:33:23 +02:00
|
|
|
|
}
|
2012-08-08 20:42:06 +02:00
|
|
|
|
|
2012-08-13 19:16:59 +02:00
|
|
|
|
UpdateBaseTileLayer();
|
2012-06-13 17:33:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private TileLayerCollection CoerceTileLayersProperty(TileLayerCollection tileLayers)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (tileLayers == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
tileLayers = new TileLayerCollection();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return tileLayers;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-13 19:16:59 +02:00
|
|
|
|
private void BaseTileLayerPropertyChanged(TileLayer baseTileLayer)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-13 19:16:59 +02:00
|
|
|
|
if (baseTileLayer != null)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-08 20:42:06 +02:00
|
|
|
|
if (TileLayers.Count == 0)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-13 19:16:59 +02:00
|
|
|
|
TileLayers.Add(baseTileLayer);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
2012-08-13 19:16:59 +02:00
|
|
|
|
else if (TileLayers[0] != baseTileLayer)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-13 19:16:59 +02:00
|
|
|
|
TileLayers[0] = baseTileLayer;
|
2012-08-08 20:42:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-13 19:16:59 +02:00
|
|
|
|
if (baseTileLayer != null && baseTileLayer.HasDarkBackground)
|
2012-08-08 20:42:06 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (DarkForeground != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Foreground = DarkForeground;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (DarkBackground != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Background = DarkBackground;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (LightForeground != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Foreground = LightForeground;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (LightBackground != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Background = LightBackground;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-13 19:16:59 +02:00
|
|
|
|
private TileLayer CoerceBaseTileLayerProperty(TileLayer baseTileLayer)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-13 19:16:59 +02:00
|
|
|
|
if (baseTileLayer == null && TileLayers.Count > 0)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-13 19:16:59 +02:00
|
|
|
|
baseTileLayer = TileLayers[0];
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-13 19:16:59 +02:00
|
|
|
|
return baseTileLayer;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-13 19:16:59 +02:00
|
|
|
|
private void UpdateBaseTileLayer()
|
2012-08-08 20:42:06 +02:00
|
|
|
|
{
|
2012-08-13 19:16:59 +02:00
|
|
|
|
TileLayer baseTileLayer = TileLayers.FirstOrDefault();
|
2012-08-08 20:42:06 +02:00
|
|
|
|
|
2012-08-13 19:16:59 +02:00
|
|
|
|
if (BaseTileLayer != baseTileLayer)
|
2012-08-08 20:42:06 +02:00
|
|
|
|
{
|
2012-08-13 19:16:59 +02:00
|
|
|
|
BaseTileLayer = baseTileLayer;
|
2012-08-08 20:42:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-06-12 20:30:05 +02:00
|
|
|
|
private void CenterPropertyChanged(Location center)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (updateTransform)
|
|
|
|
|
|
{
|
|
|
|
|
|
ResetTransformOrigin();
|
2012-08-08 20:42:06 +02:00
|
|
|
|
UpdateTransform();
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (centerAnimation == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
TargetCenter = center;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-06-12 20:30:05 +02:00
|
|
|
|
private void TargetCenterPropertyChanged(Location targetCenter)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (targetCenter != Center)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (centerAnimation != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
centerAnimation.Completed -= CenterAnimationCompleted;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-05-04 12:52:20 +02:00
|
|
|
|
centerAnimation = new LocationAnimation
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
|
|
|
|
|
From = Center,
|
|
|
|
|
|
To = targetCenter,
|
|
|
|
|
|
Duration = TimeSpan.FromSeconds(0.5),
|
2012-08-06 17:41:39 +02:00
|
|
|
|
FillBehavior = FillBehavior.Stop,
|
2012-04-25 22:02:53 +02:00
|
|
|
|
EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseOut }
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
centerAnimation.Completed += CenterAnimationCompleted;
|
|
|
|
|
|
BeginAnimation(CenterProperty, centerAnimation);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-22 20:21:31 +02:00
|
|
|
|
private void CenterAnimationCompleted(object sender, EventArgs e)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-06-18 20:12:35 +02:00
|
|
|
|
Center = TargetCenter;
|
2012-08-06 17:41:39 +02:00
|
|
|
|
centerAnimation.Completed -= CenterAnimationCompleted;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
centerAnimation = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-06-12 20:30:05 +02:00
|
|
|
|
private Location CoerceCenterProperty(Location location)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-05-04 12:52:20 +02:00
|
|
|
|
location.Latitude = Math.Min(Math.Max(location.Latitude, -MapTransform.MaxLatitude), MapTransform.MaxLatitude);
|
2012-05-06 11:28:47 +02:00
|
|
|
|
location.Longitude = Location.NormalizeLongitude(location.Longitude);
|
2012-05-04 12:52:20 +02:00
|
|
|
|
return location;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-06-12 20:30:05 +02:00
|
|
|
|
private void ZoomLevelPropertyChanged(double zoomLevel)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (updateTransform)
|
|
|
|
|
|
{
|
2012-08-08 20:42:06 +02:00
|
|
|
|
UpdateTransform();
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (zoomLevelAnimation == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
TargetZoomLevel = zoomLevel;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-06-12 20:30:05 +02:00
|
|
|
|
private void TargetZoomLevelPropertyChanged(double targetZoomLevel)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (targetZoomLevel != ZoomLevel)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (zoomLevelAnimation != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
zoomLevelAnimation.Completed -= ZoomLevelAnimationCompleted;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
zoomLevelAnimation = new DoubleAnimation
|
|
|
|
|
|
{
|
|
|
|
|
|
From = ZoomLevel,
|
|
|
|
|
|
To = targetZoomLevel,
|
|
|
|
|
|
Duration = TimeSpan.FromSeconds(0.5),
|
2012-08-06 17:41:39 +02:00
|
|
|
|
FillBehavior = FillBehavior.Stop,
|
2012-04-25 22:02:53 +02:00
|
|
|
|
EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseOut }
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
zoomLevelAnimation.Completed += ZoomLevelAnimationCompleted;
|
|
|
|
|
|
BeginAnimation(ZoomLevelProperty, zoomLevelAnimation);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-22 20:21:31 +02:00
|
|
|
|
private void ZoomLevelAnimationCompleted(object sender, EventArgs e)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-06-18 20:12:35 +02:00
|
|
|
|
ZoomLevel = TargetZoomLevel;
|
2012-08-06 17:41:39 +02:00
|
|
|
|
zoomLevelAnimation.Completed -= ZoomLevelAnimationCompleted;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
zoomLevelAnimation = null;
|
|
|
|
|
|
ResetTransformOrigin();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-06-12 20:30:05 +02:00
|
|
|
|
private double CoerceZoomLevelProperty(double zoomLevel)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
|
|
|
|
|
return Math.Min(Math.Max(zoomLevel, MinZoomLevel), MaxZoomLevel);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-06-12 20:30:05 +02:00
|
|
|
|
private void HeadingPropertyChanged(double heading)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (updateTransform)
|
|
|
|
|
|
{
|
2012-08-08 20:42:06 +02:00
|
|
|
|
UpdateTransform();
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (headingAnimation == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
TargetHeading = heading;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-06-12 20:30:05 +02:00
|
|
|
|
private void TargetHeadingPropertyChanged(double targetHeading)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (targetHeading != Heading)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (headingAnimation != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
headingAnimation.Completed -= HeadingAnimationCompleted;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double delta = targetHeading - Heading;
|
|
|
|
|
|
|
|
|
|
|
|
if (delta > 180d)
|
|
|
|
|
|
{
|
|
|
|
|
|
delta -= 360d;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (delta < -180d)
|
|
|
|
|
|
{
|
|
|
|
|
|
delta += 360d;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
headingAnimation = new DoubleAnimation
|
|
|
|
|
|
{
|
|
|
|
|
|
From = Heading,
|
|
|
|
|
|
By = delta,
|
|
|
|
|
|
Duration = TimeSpan.FromSeconds(0.5),
|
2012-08-06 17:41:39 +02:00
|
|
|
|
FillBehavior = FillBehavior.Stop,
|
2012-04-25 22:02:53 +02:00
|
|
|
|
EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseOut }
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
headingAnimation.Completed += HeadingAnimationCompleted;
|
|
|
|
|
|
BeginAnimation(HeadingProperty, headingAnimation);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-22 20:21:31 +02:00
|
|
|
|
private void HeadingAnimationCompleted(object sender, EventArgs e)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-06-18 20:12:35 +02:00
|
|
|
|
Heading = TargetHeading;
|
2012-08-06 17:41:39 +02:00
|
|
|
|
headingAnimation.Completed -= HeadingAnimationCompleted;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
headingAnimation = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-06-12 20:30:05 +02:00
|
|
|
|
private double CoerceHeadingProperty(double heading)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
|
|
|
|
|
return ((heading % 360d) + 360d) % 360d;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-08 20:42:06 +02:00
|
|
|
|
private void UpdateTransform()
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
|
|
|
|
|
double scale;
|
|
|
|
|
|
|
2012-05-04 12:52:20 +02:00
|
|
|
|
if (transformOrigin != null)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-08 20:42:06 +02:00
|
|
|
|
scale = tileContainer.SetViewportTransform(ZoomLevel, Heading, MapTransform.Transform(transformOrigin), viewportOrigin, RenderSize);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
updateTransform = false;
|
2012-05-04 12:52:20 +02:00
|
|
|
|
Center = ViewportPointToLocation(new Point(RenderSize.Width / 2d, RenderSize.Height / 2d));
|
2012-04-25 22:02:53 +02:00
|
|
|
|
updateTransform = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2012-08-08 20:42:06 +02:00
|
|
|
|
scale = tileContainer.SetViewportTransform(ZoomLevel, Heading, MapTransform.Transform(Center), viewportOrigin, RenderSize);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
scale *= MapTransform.RelativeScale(Center) / MeterPerDegree; // Pixels per meter at center latitude
|
|
|
|
|
|
|
|
|
|
|
|
CenterScale = scale;
|
|
|
|
|
|
scaleTransform.ScaleX = scale;
|
|
|
|
|
|
scaleTransform.ScaleY = scale;
|
|
|
|
|
|
rotateTransform.Angle = Heading;
|
|
|
|
|
|
scaleRotateTransform.Matrix = scaleTransform.Value * rotateTransform.Value;
|
|
|
|
|
|
|
2012-08-08 20:42:06 +02:00
|
|
|
|
OnViewportChanged();
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|