2020-03-20 18:12:56 +01:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2024-02-03 21:01:53 +01:00
|
|
|
|
// Copyright © 2024 Clemens Fischer
|
2020-03-20 18:12:56 +01:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2021-07-05 00:03:44 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#if WPF
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
using System.Windows.Threading;
|
|
|
|
|
|
#elif UWP
|
2025-01-01 07:20:05 +01:00
|
|
|
|
using Windows.UI.Composition;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
|
using Windows.UI.Xaml.Controls;
|
2025-01-01 07:20:05 +01:00
|
|
|
|
using Windows.UI.Xaml.Hosting;
|
2024-05-22 11:25:32 +02:00
|
|
|
|
using Windows.UI.Xaml.Media;
|
2024-05-20 23:24:34 +02:00
|
|
|
|
#elif WINUI
|
2025-01-01 07:20:05 +01:00
|
|
|
|
using Microsoft.UI.Composition;
|
2021-06-14 21:41:37 +02:00
|
|
|
|
using Microsoft.UI.Xaml;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Controls;
|
2025-01-01 07:20:05 +01:00
|
|
|
|
using Microsoft.UI.Xaml.Hosting;
|
2021-06-14 21:41:37 +02:00
|
|
|
|
using Microsoft.UI.Xaml.Media;
|
2022-08-02 19:50:11 +02:00
|
|
|
|
using DispatcherTimer = Microsoft.UI.Dispatching.DispatcherQueueTimer;
|
2020-03-20 18:12:56 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2024-12-31 16:11:37 +01:00
|
|
|
|
public abstract class MapTileLayerBase : Panel, IMapLayer
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2024-05-20 23:24:34 +02:00
|
|
|
|
public static readonly DependencyProperty TileSourceProperty =
|
2024-05-23 18:22:52 +02:00
|
|
|
|
DependencyPropertyHelper.Register<MapTileLayerBase, TileSource>(nameof(TileSource), null,
|
2024-05-22 11:47:57 +02:00
|
|
|
|
async (layer, oldValue, newValue) => await layer.Update(true));
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
public static readonly DependencyProperty SourceNameProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<MapTileLayerBase, string>(nameof(SourceName));
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
public static readonly DependencyProperty DescriptionProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<MapTileLayerBase, string>(nameof(Description));
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
public static readonly DependencyProperty MaxBackgroundLevelsProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<MapTileLayerBase, int>(nameof(MaxBackgroundLevels), 5);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
public static readonly DependencyProperty UpdateIntervalProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<MapTileLayerBase, TimeSpan>(nameof(UpdateInterval), TimeSpan.FromSeconds(0.2));
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
public static readonly DependencyProperty UpdateWhileViewportChangingProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<MapTileLayerBase, bool>(nameof(UpdateWhileViewportChanging));
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
public static readonly DependencyProperty MapBackgroundProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<MapTileLayerBase, Brush>(nameof(MapBackground));
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
public static readonly DependencyProperty MapForegroundProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<MapTileLayerBase, Brush>(nameof(MapForeground));
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
public static readonly DependencyProperty LoadingProgressProperty =
|
|
|
|
|
|
DependencyPropertyHelper.Register<MapTileLayerBase, double>(nameof(LoadingProgress), 1d);
|
2022-08-05 18:54:19 +02:00
|
|
|
|
|
2023-08-13 07:52:11 +02:00
|
|
|
|
private readonly Progress<double> loadingProgress;
|
2022-01-12 23:56:05 +01:00
|
|
|
|
private readonly DispatcherTimer updateTimer;
|
2023-08-22 18:16:24 +02:00
|
|
|
|
private ITileImageLoader tileImageLoader;
|
2020-03-20 18:12:56 +01:00
|
|
|
|
private MapBase parentMap;
|
|
|
|
|
|
|
2023-08-22 18:16:24 +02:00
|
|
|
|
protected MapTileLayerBase()
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2024-05-25 18:58:51 +02:00
|
|
|
|
IsHitTestVisible = false;
|
2022-08-05 18:54:19 +02:00
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
loadingProgress = new Progress<double>(p => SetValue(LoadingProgressProperty, p));
|
2022-01-12 23:56:05 +01:00
|
|
|
|
|
|
|
|
|
|
updateTimer = this.CreateTimer(UpdateInterval);
|
2023-08-12 17:36:37 +02:00
|
|
|
|
updateTimer.Tick += async (s, e) => await Update(false);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2024-05-25 19:01:00 +02:00
|
|
|
|
MapPanel.SetRenderTransform(this, new MatrixTransform());
|
2025-01-01 07:20:05 +01:00
|
|
|
|
#if WPF
|
|
|
|
|
|
RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
|
|
|
|
|
|
#elif UWP || WINUI
|
2020-03-20 18:12:56 +01:00
|
|
|
|
MapPanel.InitMapElement(this);
|
2025-01-01 07:20:05 +01:00
|
|
|
|
ElementCompositionPreview.GetElementVisual(this).BorderMode = CompositionBorderMode.Hard;
|
2021-01-17 00:31:30 +01:00
|
|
|
|
#endif
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-22 18:16:24 +02:00
|
|
|
|
public ITileImageLoader TileImageLoader
|
|
|
|
|
|
{
|
|
|
|
|
|
get => tileImageLoader ?? (tileImageLoader = new TileImageLoader());
|
|
|
|
|
|
set => tileImageLoader = value;
|
|
|
|
|
|
}
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Provides map tile URIs or images.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public TileSource TileSource
|
|
|
|
|
|
{
|
2022-08-06 10:40:59 +02:00
|
|
|
|
get => (TileSource)GetValue(TileSourceProperty);
|
|
|
|
|
|
set => SetValue(TileSourceProperty, value);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-07-13 08:19:17 +02:00
|
|
|
|
/// Name of the tile source that is used as component of a tile cache key.
|
|
|
|
|
|
/// Tile images are not cached when SourceName is null or empty.
|
2020-03-20 18:12:56 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string SourceName
|
|
|
|
|
|
{
|
2022-08-06 10:40:59 +02:00
|
|
|
|
get => (string)GetValue(SourceNameProperty);
|
|
|
|
|
|
set => SetValue(SourceNameProperty, value);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-11-28 23:50:17 +01:00
|
|
|
|
/// Description of the layer. Used to display copyright information on top of the map.
|
2020-03-20 18:12:56 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string Description
|
|
|
|
|
|
{
|
2022-08-06 10:40:59 +02:00
|
|
|
|
get => (string)GetValue(DescriptionProperty);
|
|
|
|
|
|
set => SetValue(DescriptionProperty, value);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-08-22 21:13:45 +02:00
|
|
|
|
/// Maximum number of background tile levels. Default value is 5.
|
2020-03-20 21:45:54 +01:00
|
|
|
|
/// Only effective in a MapTileLayer or WmtsTileLayer that is the MapLayer of its ParentMap.
|
2020-03-20 18:12:56 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public int MaxBackgroundLevels
|
|
|
|
|
|
{
|
2022-08-06 10:40:59 +02:00
|
|
|
|
get => (int)GetValue(MaxBackgroundLevelsProperty);
|
|
|
|
|
|
set => SetValue(MaxBackgroundLevelsProperty, value);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Minimum time interval between tile updates.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public TimeSpan UpdateInterval
|
|
|
|
|
|
{
|
2022-08-06 10:40:59 +02:00
|
|
|
|
get => (TimeSpan)GetValue(UpdateIntervalProperty);
|
|
|
|
|
|
set => SetValue(UpdateIntervalProperty, value);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Controls if tiles are updated while the viewport is still changing.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool UpdateWhileViewportChanging
|
|
|
|
|
|
{
|
2022-08-06 10:40:59 +02:00
|
|
|
|
get => (bool)GetValue(UpdateWhileViewportChangingProperty);
|
|
|
|
|
|
set => SetValue(UpdateWhileViewportChangingProperty, value);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Optional background brush. Sets MapBase.Background if not null and this layer is the base map layer.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Brush MapBackground
|
|
|
|
|
|
{
|
2022-08-06 10:40:59 +02:00
|
|
|
|
get => (Brush)GetValue(MapBackgroundProperty);
|
|
|
|
|
|
set => SetValue(MapBackgroundProperty, value);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Optional foreground brush. Sets MapBase.Foreground if not null and this layer is the base map layer.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Brush MapForeground
|
|
|
|
|
|
{
|
2022-08-06 10:40:59 +02:00
|
|
|
|
get => (Brush)GetValue(MapForegroundProperty);
|
|
|
|
|
|
set => SetValue(MapForegroundProperty, value);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-05 18:54:19 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the progress of the TileImageLoader as a double value between 0 and 1.
|
|
|
|
|
|
/// </summary>
|
2024-05-20 23:24:34 +02:00
|
|
|
|
public double LoadingProgress => (double)GetValue(LoadingProgressProperty);
|
2022-08-05 18:54:19 +02:00
|
|
|
|
|
2023-01-19 17:16:02 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Implements IMapElement.ParentMap.
|
|
|
|
|
|
/// </summary>
|
2020-03-20 18:12:56 +01:00
|
|
|
|
public MapBase ParentMap
|
|
|
|
|
|
{
|
2022-08-06 10:40:59 +02:00
|
|
|
|
get => parentMap;
|
2020-03-20 18:12:56 +01:00
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
if (parentMap != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
parentMap.ViewportChanged -= OnViewportChanged;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
parentMap = value;
|
|
|
|
|
|
|
|
|
|
|
|
if (parentMap != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
parentMap.ViewportChanged += OnViewportChanged;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-12 23:56:05 +01:00
|
|
|
|
updateTimer.Run();
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-06 10:40:59 +02:00
|
|
|
|
protected bool IsBaseMapLayer => parentMap != null && parentMap.Children.Count > 0 && parentMap.Children[0] == this;
|
2021-11-10 22:31:39 +01:00
|
|
|
|
|
2021-11-14 19:29:35 +01:00
|
|
|
|
protected abstract void SetRenderTransform();
|
|
|
|
|
|
|
2023-08-12 17:36:37 +02:00
|
|
|
|
protected abstract Task UpdateTileLayer(bool tileSourceChanged);
|
|
|
|
|
|
|
|
|
|
|
|
protected Task LoadTiles(IEnumerable<Tile> tiles, string cacheName)
|
|
|
|
|
|
{
|
2023-08-19 17:48:52 +02:00
|
|
|
|
return TileImageLoader.LoadTilesAsync(tiles, TileSource, cacheName, loadingProgress);
|
2023-08-12 17:36:37 +02:00
|
|
|
|
}
|
2021-11-14 19:29:35 +01:00
|
|
|
|
|
2023-08-12 17:36:37 +02:00
|
|
|
|
private Task Update(bool tileSourceChanged)
|
2021-11-22 23:53:01 +01:00
|
|
|
|
{
|
|
|
|
|
|
updateTimer.Stop();
|
|
|
|
|
|
|
2023-08-12 17:36:37 +02:00
|
|
|
|
return UpdateTileLayer(tileSourceChanged);
|
2021-11-22 23:53:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-05 00:03:44 +02:00
|
|
|
|
private async void OnViewportChanged(object sender, ViewportChangedEventArgs e)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2022-11-25 19:05:48 +01:00
|
|
|
|
if (e.TransformCenterChanged || e.ProjectionChanged || Children.Count == 0)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2023-08-12 17:36:37 +02:00
|
|
|
|
await Update(false); // update immediately
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
SetRenderTransform();
|
|
|
|
|
|
|
2022-01-12 23:56:05 +01:00
|
|
|
|
updateTimer.Run(!UpdateWhileViewportChanging);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|