XAML-Map-Control/MapControl/Shared/MapBase.cs

836 lines
30 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2024-02-03 21:01:53 +01:00
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
2021-06-14 21:41:37 +02:00
#if WINUI
using Windows.Foundation;
2021-06-14 21:41:37 +02:00
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Animation;
2021-11-17 23:17:11 +01:00
#elif UWP
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
#else
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
#endif
namespace MapControl
{
public interface IMapLayer : IMapElement
{
Brush MapBackground { get; }
Brush MapForeground { get; }
}
/// <summary>
/// The map control. Displays map content provided by one or more tile or image layers,
2022-11-07 21:10:10 +01:00
/// such as MapTileLayerBase or MapImageLayer instances.
/// 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.
/// MapBase can contain map overlay child elements like other MapPanels or MapItemsControls.
/// </summary>
public partial class MapBase : MapPanel
{
2020-04-22 16:16:17 +02:00
public static TimeSpan ImageFadeDuration { get; set; } = TimeSpan.FromSeconds(0.1);
public static readonly DependencyProperty MapLayerProperty = DependencyProperty.Register(
nameof(MapLayer), typeof(UIElement), typeof(MapBase),
new PropertyMetadata(null, (o, e) => ((MapBase)o).MapLayerPropertyChanged((UIElement)e.OldValue, (UIElement)e.NewValue)));
public static readonly DependencyProperty MapProjectionProperty = DependencyProperty.Register(
nameof(MapProjection), typeof(MapProjection), typeof(MapBase),
2022-03-05 18:40:57 +01:00
new PropertyMetadata(new WebMercatorProjection(), (o, e) => ((MapBase)o).MapProjectionPropertyChanged((MapProjection)e.NewValue)));
public static readonly DependencyProperty ProjectionCenterProperty = DependencyProperty.Register(
nameof(ProjectionCenter), typeof(Location), typeof(MapBase),
new PropertyMetadata(null, (o, e) => ((MapBase)o).ProjectionCenterPropertyChanged()));
public static readonly DependencyProperty MinZoomLevelProperty = DependencyProperty.Register(
nameof(MinZoomLevel), typeof(double), typeof(MapBase),
new PropertyMetadata(1d, (o, e) => ((MapBase)o).MinZoomLevelPropertyChanged((double)e.NewValue)));
public static readonly DependencyProperty MaxZoomLevelProperty = DependencyProperty.Register(
nameof(MaxZoomLevel), typeof(double), typeof(MapBase),
2020-03-25 16:14:20 +01:00
new PropertyMetadata(20d, (o, e) => ((MapBase)o).MaxZoomLevelPropertyChanged((double)e.NewValue)));
public static readonly DependencyProperty AnimationDurationProperty = DependencyProperty.Register(
nameof(AnimationDuration), typeof(TimeSpan), typeof(MapBase),
new PropertyMetadata(TimeSpan.FromSeconds(0.3)));
public static readonly DependencyProperty AnimationEasingFunctionProperty = DependencyProperty.Register(
nameof(AnimationEasingFunction), typeof(EasingFunctionBase), typeof(MapBase),
new PropertyMetadata(new QuadraticEase { EasingMode = EasingMode.EaseOut }));
private PointAnimation centerAnimation;
private DoubleAnimation zoomLevelAnimation;
private DoubleAnimation headingAnimation;
private Location transformCenter;
private Point viewCenter;
private double centerLongitude;
2022-03-05 18:40:57 +01:00
private double maxLatitude = 90d;
private bool internalPropertyChange;
/// <summary>
/// Raised when the current map viewport has changed.
/// </summary>
public event EventHandler<ViewportChangedEventArgs> ViewportChanged;
/// <summary>
/// Gets or sets the map foreground Brush.
/// </summary>
public Brush Foreground
{
2022-08-06 10:40:59 +02:00
get => (Brush)GetValue(ForegroundProperty);
set => SetValue(ForegroundProperty, value);
}
/// <summary>
/// Gets or sets the base map layer, which is added as first element to the Children collection.
/// If the layer implements IMapLayer (like MapTileLayer or MapImageLayer), its (non-null) MapBackground
/// and MapForeground property values are used for the MapBase Background and Foreground properties.
/// </summary>
public UIElement MapLayer
{
2022-08-06 10:40:59 +02:00
get => (UIElement)GetValue(MapLayerProperty);
set => SetValue(MapLayerProperty, value);
}
/// <summary>
/// Gets or sets the MapProjection used by the map control.
/// </summary>
public MapProjection MapProjection
{
2022-08-06 10:40:59 +02:00
get => (MapProjection)GetValue(MapProjectionProperty);
set => SetValue(MapProjectionProperty, value);
}
/// <summary>
/// Gets or sets an optional center (reference point) for azimuthal projections.
/// If ProjectionCenter is null, the Center property value will be used instead.
/// </summary>
public Location ProjectionCenter
{
2022-08-06 10:40:59 +02:00
get => (Location)GetValue(ProjectionCenterProperty);
set => SetValue(ProjectionCenterProperty, value);
}
/// <summary>
/// Gets or sets the location of the center point of the map.
/// </summary>
public Location Center
{
2022-08-06 10:40:59 +02:00
get => (Location)GetValue(CenterProperty);
set => SetValue(CenterProperty, value);
}
/// <summary>
/// Gets or sets the target value of a Center animation.
/// </summary>
public Location TargetCenter
{
2022-08-06 10:40:59 +02:00
get => (Location)GetValue(TargetCenterProperty);
set => SetValue(TargetCenterProperty, value);
}
/// <summary>
/// Gets or sets the minimum value of the ZoomLevel and TargetZommLevel properties.
/// Must be greater than or equal to zero and less than or equal to MaxZoomLevel.
/// The default value is 1.
/// </summary>
public double MinZoomLevel
{
2022-08-06 10:40:59 +02:00
get => (double)GetValue(MinZoomLevelProperty);
set => SetValue(MinZoomLevelProperty, value);
}
/// <summary>
/// Gets or sets the maximum value of the ZoomLevel and TargetZommLevel properties.
2020-03-25 16:14:20 +01:00
/// Must be greater than or equal to MinZoomLevel and less than or equal to 22.
/// The default value is 20.
/// </summary>
public double MaxZoomLevel
{
2022-08-06 10:40:59 +02:00
get => (double)GetValue(MaxZoomLevelProperty);
set => SetValue(MaxZoomLevelProperty, value);
}
/// <summary>
/// Gets or sets the map zoom level.
/// </summary>
public double ZoomLevel
{
2022-08-06 10:40:59 +02:00
get => (double)GetValue(ZoomLevelProperty);
set => SetValue(ZoomLevelProperty, value);
}
/// <summary>
/// Gets or sets the target value of a ZoomLevel animation.
/// </summary>
public double TargetZoomLevel
{
2022-08-06 10:40:59 +02:00
get => (double)GetValue(TargetZoomLevelProperty);
set => SetValue(TargetZoomLevelProperty, value);
}
/// <summary>
2022-11-07 21:10:10 +01:00
/// Gets or sets the map heading, a counter-clockwise rotation angle in degrees.
/// </summary>
public double Heading
{
2022-08-06 10:40:59 +02:00
get => (double)GetValue(HeadingProperty);
set => SetValue(HeadingProperty, value);
}
/// <summary>
/// Gets or sets the target value of a Heading animation.
/// </summary>
public double TargetHeading
{
2022-08-06 10:40:59 +02:00
get => (double)GetValue(TargetHeadingProperty);
set => SetValue(TargetHeadingProperty, value);
}
/// <summary>
/// Gets or sets the Duration of the Center, ZoomLevel and Heading animations.
/// The default value is 0.3 seconds.
/// </summary>
public TimeSpan AnimationDuration
{
2022-08-06 10:40:59 +02:00
get => (TimeSpan)GetValue(AnimationDurationProperty);
set => SetValue(AnimationDurationProperty, value);
}
/// <summary>
/// Gets or sets the EasingFunction of the Center, ZoomLevel and Heading animations.
/// The default value is a QuadraticEase with EasingMode.EaseOut.
/// </summary>
public EasingFunctionBase AnimationEasingFunction
{
2022-08-06 10:40:59 +02:00
get => (EasingFunctionBase)GetValue(AnimationEasingFunctionProperty);
set => SetValue(AnimationEasingFunctionProperty, value);
}
2022-11-02 19:49:18 +01:00
/// <summary>
2022-11-05 17:32:29 +01:00
/// Gets the scaling factor from projected map coordinates to view coordinates,
2022-11-07 21:10:10 +01:00
/// as pixels per meter.
2022-11-02 19:49:18 +01:00
/// </summary>
public double ViewScale => (double)GetValue(ViewScaleProperty);
/// <summary>
2022-11-05 17:32:29 +01:00
/// Gets the ViewTransform instance that is used to transform between projected
/// map coordinates and view coordinates.
/// </summary>
public ViewTransform ViewTransform { get; } = new ViewTransform();
/// <summary>
2022-11-07 21:10:10 +01:00
/// Gets the map scale as the horizontal and vertical scaling factors from geographic
/// coordinates to view coordinates at the specified location, as pixels per meter.
/// </summary>
public Point GetScale(Location location)
{
var relativeScale = MapProjection.GetRelativeScale(location);
return new Point(ViewTransform.Scale * relativeScale.X, ViewTransform.Scale * relativeScale.Y);
}
/// <summary>
/// Gets a transform Matrix for scaling and rotating objects that are anchored
/// at a Location from map coordinates (i.e. meters) to view coordinates.
/// </summary>
public Matrix GetMapTransform(Location location)
{
var scale = GetScale(location);
var matrix = new Matrix(scale.X, 0d, 0d, scale.Y, 0d, 0d);
matrix.Rotate(ViewTransform.Rotation);
return matrix;
}
/// <summary>
/// Transforms a Location in geographic coordinates to a Point in view coordinates.
/// </summary>
public Point? LocationToView(Location location)
{
var point = MapProjection.LocationToMap(location);
if (!point.HasValue)
{
return null;
}
return ViewTransform.MapToView(point.Value);
}
/// <summary>
/// Transforms a Point in view coordinates to a Location in geographic coordinates.
/// </summary>
public Location ViewToLocation(Point point)
{
return MapProjection.MapToLocation(ViewTransform.ViewToMap(point));
}
/// <summary>
/// Transforms a Rect in view coordinates to a BoundingBox in geographic coordinates.
/// </summary>
public BoundingBox ViewRectToBoundingBox(Rect rect)
{
var p1 = ViewTransform.ViewToMap(new Point(rect.X, rect.Y));
var p2 = ViewTransform.ViewToMap(new Point(rect.X, rect.Y + rect.Height));
var p3 = ViewTransform.ViewToMap(new Point(rect.X + rect.Width, rect.Y));
var p4 = ViewTransform.ViewToMap(new Point(rect.X + rect.Width, rect.Y + rect.Height));
2022-11-30 17:59:38 +01:00
var x1 = Math.Min(p1.X, Math.Min(p2.X, Math.Min(p3.X, p4.X)));
var y1 = Math.Min(p1.Y, Math.Min(p2.Y, Math.Min(p3.Y, p4.Y)));
2022-12-07 17:00:25 +01:00
var x2 = Math.Max(p1.X, Math.Max(p2.X, Math.Max(p3.X, p4.X)));
2022-11-30 17:59:38 +01:00
var y2 = Math.Max(p1.Y, Math.Max(p2.Y, Math.Max(p3.Y, p4.Y)));
return MapProjection.MapToBoundingBox(new Rect(x1, y1, x2 - x1, y2 - y1));
}
/// <summary>
/// Sets a temporary center point in view coordinates for scaling and rotation transformations.
/// This center point is automatically reset when the Center property is set by application code
/// or by the methods TranslateMap, TransformMap, ZoomMap and ZoomToBounds.
/// </summary>
public void SetTransformCenter(Point center)
{
transformCenter = ViewToLocation(center);
2022-03-04 22:28:18 +01:00
viewCenter = transformCenter != null ? center : new Point(RenderSize.Width / 2d, RenderSize.Height / 2d);
}
/// <summary>
/// Resets the temporary transform center point set by SetTransformCenter.
/// </summary>
public void ResetTransformCenter()
{
transformCenter = null;
viewCenter = new Point(RenderSize.Width / 2d, RenderSize.Height / 2d);
}
/// <summary>
/// Changes the Center property according to the specified translation in view coordinates.
/// </summary>
public void TranslateMap(Point translation)
{
if (transformCenter != null)
{
ResetTransformCenter();
UpdateTransform();
}
if (translation.X != 0d || translation.Y != 0d)
{
var center = ViewToLocation(new Point(viewCenter.X - translation.X, viewCenter.Y - translation.Y));
2022-03-04 22:28:18 +01:00
if (center != null)
{
Center = center;
}
}
}
/// <summary>
/// Changes the Center, Heading and ZoomLevel properties according to the specified
/// view coordinate translation, rotation and scale delta values. Rotation and scaling
/// is performed relative to the specified center point in view coordinates.
/// </summary>
public void TransformMap(Point center, Point translation, double rotation, double scale)
{
if (rotation != 0d || scale != 1d)
{
2022-03-04 22:28:18 +01:00
SetTransformCenter(center);
viewCenter = new Point(viewCenter.X + translation.X, viewCenter.Y + translation.Y);
if (rotation != 0d)
{
2022-11-07 21:10:10 +01:00
var heading = (((Heading - rotation) % 360d) + 360d) % 360d;
2021-01-07 22:52:41 +01:00
2017-11-10 17:26:15 +01:00
SetValueInternal(HeadingProperty, heading);
SetValueInternal(TargetHeadingProperty, heading);
}
if (scale != 1d)
{
var zoomLevel = Math.Min(Math.Max(ZoomLevel + Math.Log(scale, 2d), MinZoomLevel), MaxZoomLevel);
2021-01-07 22:52:41 +01:00
2017-11-10 17:26:15 +01:00
SetValueInternal(ZoomLevelProperty, zoomLevel);
SetValueInternal(TargetZoomLevelProperty, zoomLevel);
}
UpdateTransform(true);
}
else
{
2022-11-30 22:18:45 +01:00
// More accurate than SetTransformCenter.
//
TranslateMap(translation);
}
}
/// <summary>
/// Sets the value of the TargetZoomLevel property while retaining the specified center point
/// in view coordinates.
/// </summary>
public void ZoomMap(Point center, double zoomLevel)
{
2016-02-21 21:39:37 +01:00
zoomLevel = Math.Min(Math.Max(zoomLevel, MinZoomLevel), MaxZoomLevel);
if (TargetZoomLevel != zoomLevel)
{
SetTransformCenter(center);
TargetZoomLevel = zoomLevel;
2016-02-21 21:39:37 +01:00
}
}
/// <summary>
/// Sets the TargetZoomLevel and TargetCenter properties so that the specified bounding box
/// fits into the current view. The TargetHeading property is set to zero.
/// </summary>
public void ZoomToBounds(BoundingBox boundingBox)
{
var rect = MapProjection.BoundingBoxToMap(boundingBox);
2022-03-04 22:28:18 +01:00
if (rect.HasValue)
2022-03-04 22:28:18 +01:00
{
var rectCenter = new Point(rect.Value.X + rect.Value.Width / 2d, rect.Value.Y + rect.Value.Height / 2d);
var targetCenter = MapProjection.MapToLocation(rectCenter);
2018-12-20 21:55:12 +01:00
2022-12-14 18:02:19 +01:00
if (targetCenter != null)
{
var scale = Math.Min(RenderSize.Width / rect.Value.Width, RenderSize.Height / rect.Value.Height);
2022-12-14 18:02:19 +01:00
TargetZoomLevel = ViewTransform.ScaleToZoomLevel(scale);
TargetCenter = targetCenter;
TargetHeading = 0d;
}
2022-03-04 22:28:18 +01:00
}
}
2020-05-13 18:17:28 +02:00
internal double ConstrainedLongitude(double longitude)
{
var offset = longitude - Center.Longitude;
if (offset > 180d)
{
2022-12-01 22:48:08 +01:00
longitude = Center.Longitude + (offset % 360d) - 360d;
2020-05-13 18:17:28 +02:00
}
else if (offset < -180d)
{
2022-12-01 22:48:08 +01:00
longitude = Center.Longitude + (offset % 360d) + 360d;
2020-05-13 18:17:28 +02:00
}
return longitude;
}
2024-05-18 20:24:04 +02:00
private void SetValueInternal(DependencyProperty property, object value)
{
internalPropertyChange = true;
SetValue(property, value);
internalPropertyChange = false;
}
private void AdjustCenterProperty(DependencyProperty property, ref Location center)
{
var c = center;
if (center == null)
{
center = new Location();
}
else if (
center.Latitude < -maxLatitude || center.Latitude > maxLatitude ||
center.Longitude < -180d || center.Longitude > 180d)
{
center = new Location(
Math.Min(Math.Max(center.Latitude, -maxLatitude), maxLatitude),
Location.NormalizeLongitude(center.Longitude));
}
if (center != c)
{
SetValueInternal(property, center);
}
}
private void AdjustZoomLevelProperty(DependencyProperty property, ref double zoomLevel)
{
if (zoomLevel < MinZoomLevel || zoomLevel > MaxZoomLevel)
{
zoomLevel = Math.Min(Math.Max(zoomLevel, MinZoomLevel), MaxZoomLevel);
SetValueInternal(property, zoomLevel);
}
}
private void AdjustHeadingProperty(DependencyProperty property, ref double heading)
{
if (heading < 0d || heading > 360d)
{
heading = ((heading % 360d) + 360d) % 360d;
SetValueInternal(property, heading);
}
}
private void MapLayerPropertyChanged(UIElement oldLayer, UIElement newLayer)
{
if (oldLayer != null)
{
Children.Remove(oldLayer);
2020-04-16 23:15:03 +02:00
if (oldLayer is IMapLayer mapLayer)
{
if (mapLayer.MapBackground != null)
{
ClearValue(BackgroundProperty);
}
if (mapLayer.MapForeground != null)
{
ClearValue(ForegroundProperty);
}
}
}
if (newLayer != null)
{
Children.Insert(0, newLayer);
2020-04-16 23:15:03 +02:00
if (newLayer is IMapLayer mapLayer)
{
if (mapLayer.MapBackground != null)
{
Background = mapLayer.MapBackground;
}
if (mapLayer.MapForeground != null)
{
Foreground = mapLayer.MapForeground;
}
}
}
}
2022-03-05 18:40:57 +01:00
private void MapProjectionPropertyChanged(MapProjection projection)
{
2022-03-05 18:40:57 +01:00
maxLatitude = 90d;
if (projection.Type <= MapProjectionType.NormalCylindrical)
{
var maxLocation = projection.MapToLocation(new Point(0d, 180d * MapProjection.Wgs84MeterPerDegree));
if (maxLocation != null && maxLocation.Latitude < 90d)
{
maxLatitude = maxLocation.Latitude;
var center = Center;
AdjustCenterProperty(CenterProperty, ref center);
}
}
ResetTransformCenter();
UpdateTransform(false, true);
}
private void ProjectionCenterPropertyChanged()
{
ResetTransformCenter();
UpdateTransform();
}
private void CenterPropertyChanged(Location center)
{
if (!internalPropertyChange)
{
AdjustCenterProperty(CenterProperty, ref center);
UpdateTransform();
if (centerAnimation == null)
{
2017-11-10 17:26:15 +01:00
SetValueInternal(TargetCenterProperty, center);
}
}
}
private void TargetCenterPropertyChanged(Location targetCenter)
{
if (!internalPropertyChange)
{
AdjustCenterProperty(TargetCenterProperty, ref targetCenter);
if (!targetCenter.Equals(Center))
{
if (centerAnimation != null)
{
centerAnimation.Completed -= CenterAnimationCompleted;
}
centerAnimation = new PointAnimation
{
2017-11-10 17:26:15 +01:00
From = new Point(Center.Longitude, Center.Latitude),
2020-05-13 18:17:28 +02:00
To = new Point(ConstrainedLongitude(targetCenter.Longitude), targetCenter.Latitude),
Duration = AnimationDuration,
2017-11-10 17:26:15 +01:00
EasingFunction = AnimationEasingFunction
};
centerAnimation.Completed += CenterAnimationCompleted;
2017-11-10 17:26:15 +01:00
this.BeginAnimation(CenterPointProperty, centerAnimation);
}
}
}
private void CenterAnimationCompleted(object sender, object e)
{
if (centerAnimation != null)
{
centerAnimation.Completed -= CenterAnimationCompleted;
centerAnimation = null;
2017-11-10 17:26:15 +01:00
this.BeginAnimation(CenterPointProperty, null);
}
}
private void CenterPointPropertyChanged(Location center)
{
2017-11-10 17:26:15 +01:00
if (centerAnimation != null)
{
SetValueInternal(CenterProperty, center);
UpdateTransform();
}
}
private void MinZoomLevelPropertyChanged(double minZoomLevel)
{
if (minZoomLevel < 0d || minZoomLevel > MaxZoomLevel)
{
minZoomLevel = Math.Min(Math.Max(minZoomLevel, 0d), MaxZoomLevel);
2021-01-07 22:52:41 +01:00
2017-11-10 17:26:15 +01:00
SetValueInternal(MinZoomLevelProperty, minZoomLevel);
}
if (ZoomLevel < minZoomLevel)
{
ZoomLevel = minZoomLevel;
}
}
private void MaxZoomLevelPropertyChanged(double maxZoomLevel)
{
2021-01-07 22:52:41 +01:00
if (maxZoomLevel < MinZoomLevel)
{
2021-01-07 22:52:41 +01:00
maxZoomLevel = MinZoomLevel;
2017-11-10 17:26:15 +01:00
SetValueInternal(MaxZoomLevelProperty, maxZoomLevel);
}
if (ZoomLevel > maxZoomLevel)
{
ZoomLevel = maxZoomLevel;
}
}
private void ZoomLevelPropertyChanged(double zoomLevel)
{
if (!internalPropertyChange)
{
AdjustZoomLevelProperty(ZoomLevelProperty, ref zoomLevel);
UpdateTransform();
if (zoomLevelAnimation == null)
{
2017-11-10 17:26:15 +01:00
SetValueInternal(TargetZoomLevelProperty, zoomLevel);
}
}
}
private void TargetZoomLevelPropertyChanged(double targetZoomLevel)
{
if (!internalPropertyChange)
{
AdjustZoomLevelProperty(TargetZoomLevelProperty, ref targetZoomLevel);
if (targetZoomLevel != ZoomLevel)
{
if (zoomLevelAnimation != null)
{
zoomLevelAnimation.Completed -= ZoomLevelAnimationCompleted;
}
zoomLevelAnimation = new DoubleAnimation
{
To = targetZoomLevel,
Duration = AnimationDuration,
2017-11-10 17:26:15 +01:00
EasingFunction = AnimationEasingFunction
};
zoomLevelAnimation.Completed += ZoomLevelAnimationCompleted;
2017-11-10 17:26:15 +01:00
this.BeginAnimation(ZoomLevelProperty, zoomLevelAnimation);
}
}
}
private void ZoomLevelAnimationCompleted(object sender, object e)
{
if (zoomLevelAnimation != null)
{
2017-11-10 17:26:15 +01:00
SetValueInternal(ZoomLevelProperty, TargetZoomLevel);
UpdateTransform(true);
zoomLevelAnimation.Completed -= ZoomLevelAnimationCompleted;
zoomLevelAnimation = null;
2017-11-10 17:26:15 +01:00
this.BeginAnimation(ZoomLevelProperty, null);
}
}
private void HeadingPropertyChanged(double heading)
{
if (!internalPropertyChange)
{
AdjustHeadingProperty(HeadingProperty, ref heading);
UpdateTransform();
if (headingAnimation == null)
{
2017-11-10 17:26:15 +01:00
SetValueInternal(TargetHeadingProperty, heading);
}
}
}
private void TargetHeadingPropertyChanged(double targetHeading)
{
if (!internalPropertyChange)
{
AdjustHeadingProperty(TargetHeadingProperty, ref targetHeading);
if (targetHeading != Heading)
{
var delta = targetHeading - Heading;
if (delta > 180d)
{
delta -= 360d;
}
else if (delta < -180d)
{
delta += 360d;
}
if (headingAnimation != null)
{
headingAnimation.Completed -= HeadingAnimationCompleted;
}
headingAnimation = new DoubleAnimation
{
By = delta,
Duration = AnimationDuration,
2017-11-10 17:26:15 +01:00
EasingFunction = AnimationEasingFunction
};
headingAnimation.Completed += HeadingAnimationCompleted;
2017-11-10 17:26:15 +01:00
this.BeginAnimation(HeadingProperty, headingAnimation);
}
}
}
private void HeadingAnimationCompleted(object sender, object e)
{
if (headingAnimation != null)
{
2017-11-10 17:26:15 +01:00
SetValueInternal(HeadingProperty, TargetHeading);
UpdateTransform();
headingAnimation.Completed -= HeadingAnimationCompleted;
headingAnimation = null;
2017-11-10 17:26:15 +01:00
this.BeginAnimation(HeadingProperty, null);
}
}
private void UpdateTransform(bool resetTransformCenter = false, bool projectionChanged = false)
{
var transformCenterChanged = false;
var viewScale = ViewTransform.ZoomLevelToScale(ZoomLevel);
var projection = MapProjection;
2022-03-07 17:28:08 +01:00
projection.Center = ProjectionCenter ?? Center;
2022-03-04 22:28:18 +01:00
var mapCenter = projection.LocationToMap(transformCenter ?? Center);
2014-10-29 18:22:50 +01:00
if (mapCenter.HasValue)
{
ViewTransform.SetTransform(mapCenter.Value, viewCenter, viewScale, -Heading);
2022-03-04 22:28:18 +01:00
if (transformCenter != null)
{
2022-03-04 22:28:18 +01:00
var center = ViewToLocation(new Point(RenderSize.Width / 2d, RenderSize.Height / 2d));
2022-03-04 22:28:18 +01:00
if (center != null)
{
2024-04-11 13:21:38 +02:00
var centerLatitude = center.Latitude;
var centerLongitude = Location.NormalizeLongitude(center.Longitude);
2024-04-11 13:21:38 +02:00
if (centerLatitude < -maxLatitude || centerLatitude > maxLatitude)
2022-03-04 22:28:18 +01:00
{
2024-04-11 13:21:38 +02:00
centerLatitude = Math.Min(Math.Max(centerLatitude, -maxLatitude), maxLatitude);
2022-03-04 22:28:18 +01:00
resetTransformCenter = true;
}
2024-04-11 13:21:38 +02:00
center = new Location(centerLatitude, centerLongitude);
2022-03-04 22:28:18 +01:00
SetValueInternal(CenterProperty, center);
2022-03-04 22:28:18 +01:00
if (centerAnimation == null)
{
SetValueInternal(TargetCenterProperty, center);
}
2022-03-04 22:28:18 +01:00
if (resetTransformCenter)
{
2024-04-17 20:10:45 +02:00
// Check if transform center has moved across 180° longitude.
2022-11-30 22:18:45 +01:00
//
transformCenterChanged = Math.Abs(center.Longitude - transformCenter.Longitude) > 180d;
2022-03-04 22:28:18 +01:00
ResetTransformCenter();
2022-03-07 17:28:08 +01:00
projection.Center = ProjectionCenter ?? Center;
2022-03-06 17:28:27 +01:00
2022-03-04 22:28:18 +01:00
mapCenter = projection.LocationToMap(center);
if (mapCenter.HasValue)
2022-03-04 22:28:18 +01:00
{
ViewTransform.SetTransform(mapCenter.Value, viewCenter, viewScale, -Heading);
2022-03-04 22:28:18 +01:00
}
}
}
}
2022-11-02 19:49:18 +01:00
SetViewScale(ViewTransform.Scale);
2024-04-17 20:10:45 +02:00
// Check if view center has moved across 180° longitude.
2022-11-30 22:18:45 +01:00
//
transformCenterChanged = transformCenterChanged || Math.Abs(Center.Longitude - centerLongitude) > 180d;
2022-03-04 22:28:18 +01:00
centerLongitude = Center.Longitude;
OnViewportChanged(new ViewportChangedEventArgs(projectionChanged, transformCenterChanged));
2022-03-04 22:28:18 +01:00
}
}
protected override void OnViewportChanged(ViewportChangedEventArgs e)
{
base.OnViewportChanged(e);
ViewportChanged?.Invoke(this, e);
}
}
}