2020-03-20 18:12:56 +01:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2023-01-03 15:12:53 +01:00
|
|
|
|
// Copyright © 2023 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;
|
2021-06-14 21:41:37 +02:00
|
|
|
|
#if WINUI
|
|
|
|
|
|
using Microsoft.UI.Xaml;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Controls;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Media;
|
2022-08-02 19:50:11 +02:00
|
|
|
|
using DispatcherTimer = Microsoft.UI.Dispatching.DispatcherQueueTimer;
|
2021-11-17 23:17:11 +01:00
|
|
|
|
#elif UWP
|
2020-03-20 18:12:56 +01:00
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
|
using Windows.UI.Xaml.Controls;
|
|
|
|
|
|
using Windows.UI.Xaml.Media;
|
|
|
|
|
|
#else
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
using System.Windows.Threading;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
public interface ITileImageLoader
|
|
|
|
|
|
{
|
2023-08-12 17:36:37 +02:00
|
|
|
|
Task LoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName, IProgress<double> progress);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public abstract class MapTileLayerBase : Panel, IMapLayer
|
|
|
|
|
|
{
|
|
|
|
|
|
public static readonly DependencyProperty TileSourceProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(TileSource), typeof(TileSource), typeof(MapTileLayerBase),
|
2023-08-12 17:36:37 +02:00
|
|
|
|
new PropertyMetadata(null, async (o, e) => await ((MapTileLayerBase)o).Update(true)));
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty SourceNameProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(SourceName), typeof(string), typeof(MapTileLayerBase), new PropertyMetadata(null));
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(Description), typeof(string), typeof(MapTileLayerBase), new PropertyMetadata(null));
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty MaxBackgroundLevelsProperty = DependencyProperty.Register(
|
2022-08-22 21:13:45 +02:00
|
|
|
|
nameof(MaxBackgroundLevels), typeof(int), typeof(MapTileLayerBase), new PropertyMetadata(5));
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty UpdateIntervalProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(UpdateInterval), typeof(TimeSpan), typeof(MapTileLayerBase),
|
2020-10-25 16:09:09 +01:00
|
|
|
|
new PropertyMetadata(TimeSpan.FromSeconds(0.2), (o, e) => ((MapTileLayerBase)o).updateTimer.Interval = (TimeSpan)e.NewValue));
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty UpdateWhileViewportChangingProperty = DependencyProperty.Register(
|
2022-11-14 18:37:51 +01:00
|
|
|
|
nameof(UpdateWhileViewportChanging), typeof(bool), typeof(MapTileLayerBase), new PropertyMetadata(false));
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty MapBackgroundProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(MapBackground), typeof(Brush), typeof(MapTileLayerBase), new PropertyMetadata(null));
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty MapForegroundProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(MapForeground), typeof(Brush), typeof(MapTileLayerBase), new PropertyMetadata(null));
|
|
|
|
|
|
|
2022-08-05 18:54:19 +02:00
|
|
|
|
public static readonly DependencyProperty LoadingProgressProperty = DependencyProperty.Register(
|
2022-08-05 21:30:57 +02:00
|
|
|
|
nameof(LoadingProgress), typeof(double), typeof(MapTileLayerBase), new PropertyMetadata(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;
|
2020-03-20 18:12:56 +01:00
|
|
|
|
private MapBase parentMap;
|
|
|
|
|
|
|
2020-04-14 22:41:34 +02:00
|
|
|
|
protected MapTileLayerBase(ITileImageLoader tileImageLoader)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
|
|
|
|
|
RenderTransform = new MatrixTransform();
|
2022-08-05 18:54:19 +02:00
|
|
|
|
|
2020-03-20 18:12:56 +01:00
|
|
|
|
TileImageLoader = tileImageLoader;
|
2023-08-12 17:36:37 +02:00
|
|
|
|
|
|
|
|
|
|
loadingProgress = new Progress<double>(p => LoadingProgress = 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
|
|
|
|
|
2021-11-17 23:17:11 +01:00
|
|
|
|
#if WINUI || UWP
|
2020-03-20 18:12:56 +01:00
|
|
|
|
MapPanel.InitMapElement(this);
|
2021-01-17 00:31:30 +01:00
|
|
|
|
#endif
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ITileImageLoader TileImageLoader { get; }
|
|
|
|
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
|
|
/// Name of the TileSource. Used as component of a tile cache key.
|
|
|
|
|
|
/// </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>
|
|
|
|
|
|
public double LoadingProgress
|
|
|
|
|
|
{
|
2022-08-06 10:40:59 +02:00
|
|
|
|
get => (double)GetValue(LoadingProgressProperty);
|
|
|
|
|
|
private set => SetValue(LoadingProgressProperty, value);
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
return TileImageLoader.LoadTiles(tiles, TileSource, cacheName, loadingProgress);
|
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|