2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2024-02-03 21:01:53 +01:00
|
|
|
|
// Copyright © 2024 Clemens Fischer
|
2012-10-12 19:22:49 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#if WPF
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Media;
|
2021-11-17 23:17:11 +01:00
|
|
|
|
#elif UWP
|
2024-05-21 13:51:10 +02:00
|
|
|
|
using Windows.UI;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using Windows.UI.Xaml;
|
2024-05-21 13:51:10 +02:00
|
|
|
|
using Windows.UI.Xaml.Media;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#elif WINUI
|
|
|
|
|
|
using Microsoft.UI;
|
|
|
|
|
|
using Microsoft.UI.Xaml;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Media;
|
|
|
|
|
|
#elif AVALONIA
|
|
|
|
|
|
using Avalonia.Media;
|
|
|
|
|
|
using DependencyProperty = Avalonia.AvaloniaProperty;
|
|
|
|
|
|
using UIElement = Avalonia.Controls.Control;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
#endif
|
2012-10-12 19:22:49 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
public interface IMapLayer : IMapElement
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
Brush MapBackground { get; }
|
|
|
|
|
|
Brush MapForeground { get; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The map control. Displays map content provided by one or more tile or image layers,
|
|
|
|
|
|
/// 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
|
|
|
|
|
|
{
|
|
|
|
|
|
public static TimeSpan ImageFadeDuration { get; set; } = TimeSpan.FromSeconds(0.1);
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty ForegroundProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<MapBase, Brush>(nameof(Foreground), new SolidColorBrush(Colors.Black));
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
public static readonly DependencyProperty AnimationDurationProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<MapBase, TimeSpan>(nameof(AnimationDuration), TimeSpan.FromSeconds(0.3));
|
2015-11-11 19:48:50 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
public static readonly DependencyProperty MapLayerProperty =
|
2024-05-23 18:22:52 +02:00
|
|
|
|
DependencyPropertyHelper.Register<MapBase, UIElement>(nameof(MapLayer), null,
|
2024-05-21 13:51:10 +02:00
|
|
|
|
(map, oldValue, newValue) => map.MapLayerPropertyChanged(oldValue, newValue));
|
2015-11-11 19:48:50 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
public static readonly DependencyProperty MapProjectionProperty =
|
2024-05-23 18:22:52 +02:00
|
|
|
|
DependencyPropertyHelper.Register<MapBase, MapProjection>(nameof(MapProjection), new WebMercatorProjection(),
|
2024-05-21 13:51:10 +02:00
|
|
|
|
(map, oldValue, newValue) => map.MapProjectionPropertyChanged(newValue));
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty ProjectionCenterProperty =
|
2024-05-23 18:22:52 +02:00
|
|
|
|
DependencyPropertyHelper.Register<MapBase, Location>(nameof(ProjectionCenter), null,
|
2024-05-21 13:51:10 +02:00
|
|
|
|
(map, oldValue, newValue) => map.ProjectionCenterPropertyChanged());
|
|
|
|
|
|
|
|
|
|
|
|
private Location transformCenter;
|
|
|
|
|
|
private Point viewCenter;
|
|
|
|
|
|
private double centerLongitude;
|
|
|
|
|
|
private double maxLatitude = 90d;
|
|
|
|
|
|
private bool internalPropertyChange;
|
2015-11-11 19:48:50 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-05-21 13:51:10 +02:00
|
|
|
|
/// Raised when the current map viewport has changed.
|
2015-11-11 19:48:50 +01:00
|
|
|
|
/// </summary>
|
2024-05-21 13:51:10 +02:00
|
|
|
|
public event EventHandler<ViewportChangedEventArgs> ViewportChanged;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the map foreground Brush.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Brush Foreground
|
2015-11-11 19:48:50 +01:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
get => (Brush)GetValue(ForegroundProperty);
|
|
|
|
|
|
set => SetValue(ForegroundProperty, value);
|
2015-11-11 19:48:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-02 19:49:18 +01:00
|
|
|
|
/// <summary>
|
2024-05-21 13:51:10 +02:00
|
|
|
|
/// Gets or sets the Duration of the Center, ZoomLevel and Heading animations.
|
|
|
|
|
|
/// The default value is 0.3 seconds.
|
2022-11-02 19:49:18 +01:00
|
|
|
|
/// </summary>
|
2024-05-21 13:51:10 +02:00
|
|
|
|
public TimeSpan AnimationDuration
|
|
|
|
|
|
{
|
|
|
|
|
|
get => (TimeSpan)GetValue(AnimationDurationProperty);
|
|
|
|
|
|
set => SetValue(AnimationDurationProperty, value);
|
|
|
|
|
|
}
|
2022-11-02 19:49:18 +01:00
|
|
|
|
|
2023-01-19 17:16:02 +01:00
|
|
|
|
/// <summary>
|
2024-05-21 13:51:10 +02:00
|
|
|
|
/// 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.
|
2023-01-19 17:16:02 +01:00
|
|
|
|
/// </summary>
|
2024-05-21 13:51:10 +02:00
|
|
|
|
public UIElement MapLayer
|
2023-01-19 17:16:02 +01:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
get => (UIElement)GetValue(MapLayerProperty);
|
|
|
|
|
|
set => SetValue(MapLayerProperty, value);
|
|
|
|
|
|
}
|
2024-05-19 23:22:54 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the MapProjection used by the map control.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public MapProjection MapProjection
|
|
|
|
|
|
{
|
|
|
|
|
|
get => (MapProjection)GetValue(MapProjectionProperty);
|
|
|
|
|
|
set => SetValue(MapProjectionProperty, value);
|
|
|
|
|
|
}
|
2024-05-19 23:22:54 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
/// <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
|
|
|
|
|
|
{
|
|
|
|
|
|
get => (Location)GetValue(ProjectionCenterProperty);
|
|
|
|
|
|
set => SetValue(ProjectionCenterProperty, value);
|
2023-01-19 17:16:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the location of the center point of the map.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Location Center
|
2014-11-19 21:11:14 +01:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
get => (Location)GetValue(CenterProperty);
|
|
|
|
|
|
set => SetValue(CenterProperty, value);
|
|
|
|
|
|
}
|
2022-03-05 18:40:57 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the target value of a Center animation.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Location TargetCenter
|
|
|
|
|
|
{
|
|
|
|
|
|
get => (Location)GetValue(TargetCenterProperty);
|
|
|
|
|
|
set => SetValue(TargetCenterProperty, value);
|
|
|
|
|
|
}
|
2022-03-05 18:40:57 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the minimum value of the ZoomLevel and TargetZoomLevel properties.
|
|
|
|
|
|
/// Must not be less than zero or greater than MaxZoomLevel. The default value is 1.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public double MinZoomLevel
|
|
|
|
|
|
{
|
|
|
|
|
|
get => (double)GetValue(MinZoomLevelProperty);
|
|
|
|
|
|
set => SetValue(MinZoomLevelProperty, value);
|
|
|
|
|
|
}
|
2022-03-05 18:40:57 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the maximum value of the ZoomLevel and TargetZoomLevel properties.
|
|
|
|
|
|
/// Must not be less than MinZoomLevel. The default value is 20.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public double MaxZoomLevel
|
|
|
|
|
|
{
|
|
|
|
|
|
get => (double)GetValue(MaxZoomLevelProperty);
|
|
|
|
|
|
set => SetValue(MaxZoomLevelProperty, value);
|
|
|
|
|
|
}
|
2022-03-05 18:40:57 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the map zoom level.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public double ZoomLevel
|
|
|
|
|
|
{
|
|
|
|
|
|
get => (double)GetValue(ZoomLevelProperty);
|
|
|
|
|
|
set => SetValue(ZoomLevelProperty, value);
|
2015-06-09 20:09:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the target value of a ZoomLevel animation.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public double TargetZoomLevel
|
2024-05-20 08:33:30 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
get => (double)GetValue(TargetZoomLevelProperty);
|
|
|
|
|
|
set => SetValue(TargetZoomLevelProperty, value);
|
|
|
|
|
|
}
|
2024-05-20 08:33:30 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the map heading, a counter-clockwise rotation angle in degrees.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public double Heading
|
|
|
|
|
|
{
|
|
|
|
|
|
get => (double)GetValue(HeadingProperty);
|
|
|
|
|
|
set => SetValue(HeadingProperty, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the target value of a Heading animation.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public double TargetHeading
|
|
|
|
|
|
{
|
|
|
|
|
|
get => (double)GetValue(TargetHeadingProperty);
|
|
|
|
|
|
set => SetValue(TargetHeadingProperty, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the ViewTransform instance that is used to transform between projected
|
|
|
|
|
|
/// map coordinates and view coordinates.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public ViewTransform ViewTransform { get; } = new ViewTransform();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-05-22 09:42:21 +02:00
|
|
|
|
/// Gets the map scale as horizontal and vertical scaling factors from meters to
|
|
|
|
|
|
/// view coordinates at the specified location.
|
2024-05-21 13:51:10 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Point GetScale(Location location)
|
|
|
|
|
|
{
|
2024-05-22 09:42:21 +02:00
|
|
|
|
return ViewTransform.GetMapScale(MapProjection.GetRelativeScale(location));
|
|
|
|
|
|
}
|
2024-05-21 13:51:10 +02:00
|
|
|
|
|
2024-05-22 09:42:21 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a transform Matrix from meters to view coordinates for scaling and rotating
|
|
|
|
|
|
/// objects that are anchored at a Location.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Matrix GetMapTransform(Location location)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ViewTransform.GetMapTransform(MapProjection.GetRelativeScale(location));
|
2024-05-21 13:51:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Transforms a Location in geographic coordinates to a Point in view coordinates.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Point? LocationToView(Location location)
|
|
|
|
|
|
{
|
|
|
|
|
|
var point = MapProjection.LocationToMap(location);
|
2024-05-20 08:33:30 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
if (!point.HasValue)
|
2024-05-20 08:33:30 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
return null;
|
2024-05-20 08:33:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
return ViewTransform.MapToView(point.Value);
|
2024-05-20 08:33:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Transforms a Point in view coordinates to a Location in geographic coordinates.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Location ViewToLocation(Point point)
|
2012-10-12 19:22:49 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
|
|
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)));
|
|
|
|
|
|
var x2 = Math.Max(p1.X, Math.Max(p2.X, Math.Max(p3.X, p4.X)));
|
|
|
|
|
|
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);
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
2024-05-20 08:33:30 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Changes the Center property according to the specified translation in view coordinates.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void TranslateMap(Point translation)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (transformCenter != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
ResetTransformCenter();
|
2012-10-12 19:22:49 +02:00
|
|
|
|
UpdateTransform();
|
2024-05-21 13:51:10 +02:00
|
|
|
|
}
|
2012-10-12 19:22:49 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
if (translation.X != 0d || translation.Y != 0d)
|
|
|
|
|
|
{
|
|
|
|
|
|
var center = ViewToLocation(new Point(viewCenter.X - translation.X, viewCenter.Y - translation.Y));
|
|
|
|
|
|
|
|
|
|
|
|
if (center != null)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
Center = center;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
2012-10-12 19:22:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
/// <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)
|
2012-10-12 19:22:49 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
if (rotation != 0d || scale != 1d)
|
2012-10-12 19:22:49 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
SetTransformCenter(center);
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
viewCenter = new Point(viewCenter.X + translation.X, viewCenter.Y + translation.Y);
|
|
|
|
|
|
|
|
|
|
|
|
if (rotation != 0d)
|
2012-10-12 19:22:49 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
var heading = (((Heading - rotation) % 360d) + 360d) % 360d;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
SetValueInternal(HeadingProperty, heading);
|
|
|
|
|
|
SetValueInternal(TargetHeadingProperty, heading);
|
|
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
if (scale != 1d)
|
|
|
|
|
|
{
|
|
|
|
|
|
var zoomLevel = Math.Min(Math.Max(ZoomLevel + Math.Log(scale, 2d), MinZoomLevel), MaxZoomLevel);
|
2017-11-10 17:26:15 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
SetValueInternal(ZoomLevelProperty, zoomLevel);
|
|
|
|
|
|
SetValueInternal(TargetZoomLevelProperty, zoomLevel);
|
2012-10-12 19:22:49 +02:00
|
|
|
|
}
|
2024-05-21 13:51:10 +02:00
|
|
|
|
|
|
|
|
|
|
UpdateTransform(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// More accurate than SetTransformCenter.
|
|
|
|
|
|
//
|
|
|
|
|
|
TranslateMap(translation);
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2012-10-12 19:22:49 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
/// <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)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
zoomLevel = CoerceZoomLevelProperty(zoomLevel);
|
|
|
|
|
|
|
|
|
|
|
|
if (TargetZoomLevel != zoomLevel)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
SetTransformCenter(center);
|
2012-10-12 19:22:49 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
TargetZoomLevel = zoomLevel;
|
2012-10-12 19:22:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-21 13:51:10 +02: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)
|
2012-10-12 19:22:49 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
var rect = MapProjection.BoundingBoxToMap(boundingBox);
|
|
|
|
|
|
|
|
|
|
|
|
if (rect.HasValue)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
var rectCenter = new Point(rect.Value.X + rect.Value.Width / 2d, rect.Value.Y + rect.Value.Height / 2d);
|
|
|
|
|
|
var targetCenter = MapProjection.MapToLocation(rectCenter);
|
|
|
|
|
|
|
|
|
|
|
|
if (targetCenter != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var scale = Math.Min(RenderSize.Width / rect.Value.Width, RenderSize.Height / rect.Value.Height);
|
|
|
|
|
|
|
|
|
|
|
|
TargetZoomLevel = ViewTransform.ScaleToZoomLevel(scale);
|
|
|
|
|
|
TargetCenter = targetCenter;
|
|
|
|
|
|
TargetHeading = 0d;
|
|
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
2012-10-12 19:22:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-21 17:39:03 +02:00
|
|
|
|
internal double CoerceLongitude(double longitude)
|
2012-10-12 19:22:49 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
var offset = longitude - Center.Longitude;
|
2021-01-07 22:52:41 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
if (offset > 180d)
|
|
|
|
|
|
{
|
|
|
|
|
|
longitude = Center.Longitude + (offset % 360d) - 360d;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
2024-05-21 13:51:10 +02:00
|
|
|
|
else if (offset < -180d)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
longitude = Center.Longitude + (offset % 360d) + 360d;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
2024-05-21 13:51:10 +02:00
|
|
|
|
|
|
|
|
|
|
return longitude;
|
2012-10-12 19:22:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
private Location CoerceCenterProperty(Location center)
|
2012-10-12 19:22:49 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
if (center == null)
|
2012-10-12 19:22:49 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
center = new Location();
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
2024-05-21 13:51:10 +02:00
|
|
|
|
else if (
|
|
|
|
|
|
center.Latitude < -maxLatitude || center.Latitude > maxLatitude ||
|
|
|
|
|
|
center.Longitude < -180d || center.Longitude > 180d)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
center = new Location(
|
|
|
|
|
|
Math.Min(Math.Max(center.Latitude, -maxLatitude), maxLatitude),
|
|
|
|
|
|
Location.NormalizeLongitude(center.Longitude));
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
2024-05-21 13:51:10 +02:00
|
|
|
|
|
|
|
|
|
|
return center;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
private double CoerceMinZoomLevelProperty(double minZoomLevel)
|
2024-05-20 08:33:30 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
return Math.Min(Math.Max(minZoomLevel, 0d), MaxZoomLevel);
|
|
|
|
|
|
}
|
2024-05-20 08:33:30 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
private double CoerceMaxZoomLevelProperty(double maxZoomLevel)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Math.Max(maxZoomLevel, MinZoomLevel);
|
|
|
|
|
|
}
|
2024-05-20 08:33:30 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
private double CoerceZoomLevelProperty(double zoomLevel)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Math.Min(Math.Max(zoomLevel, MinZoomLevel), MaxZoomLevel);
|
2024-05-20 08:33:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
private double CoerceHeadingProperty(double heading)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
return ((heading % 360d) + 360d) % 360d;
|
|
|
|
|
|
}
|
2024-05-20 08:33:30 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
private void SetValueInternal(DependencyProperty property, object value)
|
|
|
|
|
|
{
|
|
|
|
|
|
internalPropertyChange = true;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
SetValue(property, value);
|
|
|
|
|
|
|
|
|
|
|
|
internalPropertyChange = false;
|
2012-10-12 19:22:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
private void MapLayerPropertyChanged(UIElement oldLayer, UIElement newLayer)
|
2012-10-12 19:22:49 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
if (oldLayer != null)
|
2012-10-12 19:22:49 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
Children.Remove(oldLayer);
|
2012-10-12 19:22:49 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
if (oldLayer is IMapLayer mapLayer)
|
2012-10-12 19:22:49 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
if (mapLayer.MapBackground != null)
|
2013-11-17 16:52:03 +01:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
ClearValue(BackgroundProperty);
|
2013-11-17 16:52:03 +01:00
|
|
|
|
}
|
2024-05-21 13:51:10 +02:00
|
|
|
|
if (mapLayer.MapForeground != null)
|
2013-11-17 16:52:03 +01:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
ClearValue(ForegroundProperty);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2013-11-17 16:52:03 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
if (newLayer != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Children.Insert(0, newLayer);
|
2017-11-10 17:26:15 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
if (newLayer is IMapLayer mapLayer)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (mapLayer.MapBackground != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Background = mapLayer.MapBackground;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (mapLayer.MapForeground != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Foreground = mapLayer.MapForeground;
|
|
|
|
|
|
}
|
2013-11-17 16:52:03 +01:00
|
|
|
|
}
|
2012-10-12 19:22:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
private void MapProjectionPropertyChanged(MapProjection projection)
|
2012-10-12 19:22:49 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
maxLatitude = 90d;
|
|
|
|
|
|
|
|
|
|
|
|
if (projection.Type <= MapProjectionType.NormalCylindrical)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
var maxLocation = projection.MapToLocation(new Point(0d, 180d * MapProjection.Wgs84MeterPerDegree));
|
2017-11-10 17:26:15 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
if (maxLocation != null && maxLocation.Latitude < 90d)
|
|
|
|
|
|
{
|
|
|
|
|
|
maxLatitude = maxLocation.Latitude;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
Center = CoerceCenterProperty(Center);
|
|
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
2024-05-21 13:51:10 +02:00
|
|
|
|
|
|
|
|
|
|
ResetTransformCenter();
|
|
|
|
|
|
UpdateTransform(false, true);
|
2012-10-12 19:22:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
private void ProjectionCenterPropertyChanged()
|
2024-05-20 08:33:30 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
ResetTransformCenter();
|
|
|
|
|
|
UpdateTransform();
|
2024-05-20 08:33:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
private void UpdateTransform(bool resetTransformCenter = false, bool projectionChanged = false)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
var transformCenterChanged = false;
|
|
|
|
|
|
var viewScale = ViewTransform.ZoomLevelToScale(ZoomLevel);
|
|
|
|
|
|
var projection = MapProjection;
|
2024-05-20 08:33:30 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
projection.Center = ProjectionCenter ?? Center;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
var mapCenter = projection.LocationToMap(transformCenter ?? Center);
|
2012-10-12 19:22:49 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
if (mapCenter.HasValue)
|
2012-10-12 19:22:49 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
ViewTransform.SetTransform(mapCenter.Value, viewCenter, viewScale, -Heading);
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
if (transformCenter != null)
|
2012-10-12 19:22:49 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
var center = ViewToLocation(new Point(RenderSize.Width / 2d, RenderSize.Height / 2d));
|
2012-10-12 19:22:49 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
if (center != null)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
var centerLatitude = center.Latitude;
|
|
|
|
|
|
var centerLongitude = Location.NormalizeLongitude(center.Longitude);
|
2012-10-12 19:22:49 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
if (centerLatitude < -maxLatitude || centerLatitude > maxLatitude)
|
|
|
|
|
|
{
|
|
|
|
|
|
centerLatitude = Math.Min(Math.Max(centerLatitude, -maxLatitude), maxLatitude);
|
|
|
|
|
|
resetTransformCenter = true;
|
|
|
|
|
|
}
|
2012-10-12 19:22:49 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
center = new Location(centerLatitude, centerLongitude);
|
|
|
|
|
|
|
|
|
|
|
|
SetValueInternal(CenterProperty, center);
|
|
|
|
|
|
|
|
|
|
|
|
if (centerAnimation == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
SetValueInternal(TargetCenterProperty, center);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (resetTransformCenter)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Check if transform center has moved across 180° longitude.
|
|
|
|
|
|
//
|
|
|
|
|
|
transformCenterChanged = Math.Abs(center.Longitude - transformCenter.Longitude) > 180d;
|
2012-10-12 19:22:49 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
ResetTransformCenter();
|
2017-11-10 17:26:15 +01:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
projection.Center = ProjectionCenter ?? Center;
|
|
|
|
|
|
|
|
|
|
|
|
mapCenter = projection.LocationToMap(center);
|
|
|
|
|
|
|
|
|
|
|
|
if (mapCenter.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
ViewTransform.SetTransform(mapCenter.Value, viewCenter, viewScale, -Heading);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
2024-05-21 13:51:10 +02:00
|
|
|
|
|
2024-05-22 09:42:21 +02:00
|
|
|
|
ViewScale = ViewTransform.Scale;
|
2024-05-21 13:51:10 +02:00
|
|
|
|
|
|
|
|
|
|
// Check if view center has moved across 180° longitude.
|
|
|
|
|
|
//
|
|
|
|
|
|
transformCenterChanged = transformCenterChanged || Math.Abs(Center.Longitude - centerLongitude) > 180d;
|
|
|
|
|
|
centerLongitude = Center.Longitude;
|
|
|
|
|
|
|
|
|
|
|
|
OnViewportChanged(new ViewportChangedEventArgs(projectionChanged, transformCenterChanged));
|
2012-10-12 19:22:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
protected override void OnViewportChanged(ViewportChangedEventArgs e)
|
2012-10-12 19:22:49 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
base.OnViewportChanged(e);
|
2012-10-12 19:22:49 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
ViewportChanged?.Invoke(this, e);
|
2012-10-12 19:22:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|