2020-03-20 18:12:56 +01:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2021-01-13 21:19:27 +01:00
|
|
|
|
// © 2021 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
|
2021-07-05 00:03:44 +02:00
|
|
|
|
using Microsoft.UI.Dispatching;
|
2021-06-14 21:41:37 +02:00
|
|
|
|
using Microsoft.UI.Xaml;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Controls;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Media;
|
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
|
|
|
|
|
|
{
|
2021-07-05 00:03:44 +02:00
|
|
|
|
TileSource TileSource { get; }
|
|
|
|
|
|
|
|
|
|
|
|
Task LoadTiles(IEnumerable<Tile> tiles, TileSource tileSource, string cacheName);
|
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),
|
2021-07-05 00:03:44 +02:00
|
|
|
|
new PropertyMetadata(null, async (o, e) => await ((MapTileLayerBase)o).Update()));
|
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(
|
|
|
|
|
|
nameof(MaxBackgroundLevels), typeof(int), typeof(MapTileLayerBase), new PropertyMetadata(8));
|
|
|
|
|
|
|
|
|
|
|
|
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(
|
|
|
|
|
|
nameof(UpdateWhileViewportChanging), typeof(bool), typeof(MapTileLayerBase), new PropertyMetadata(false));
|
|
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
2021-07-05 00:03:44 +02:00
|
|
|
|
#if WINUI
|
|
|
|
|
|
private readonly DispatcherQueueTimer updateTimer;
|
|
|
|
|
|
#else
|
|
|
|
|
|
private readonly DispatcherTimer updateTimer = new DispatcherTimer();
|
|
|
|
|
|
#endif
|
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();
|
|
|
|
|
|
TileImageLoader = tileImageLoader;
|
2021-07-05 00:03:44 +02:00
|
|
|
|
#if WINUI
|
|
|
|
|
|
updateTimer = DispatcherQueue.CreateTimer();
|
|
|
|
|
|
#endif
|
|
|
|
|
|
updateTimer.Interval = UpdateInterval;
|
|
|
|
|
|
updateTimer.Tick += async (s, e) => await Update();
|
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
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (TileSource)GetValue(TileSourceProperty); }
|
|
|
|
|
|
set { SetValue(TileSourceProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Name of the TileSource. Used as component of a tile cache key.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string SourceName
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (string)GetValue(SourceNameProperty); }
|
|
|
|
|
|
set { SetValue(SourceNameProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Description of the tile layer. Used to display copyright information on top of the map.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string Description
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (string)GetValue(DescriptionProperty); }
|
|
|
|
|
|
set { SetValue(DescriptionProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Maximum number of background tile levels. Default value is 8.
|
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
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (int)GetValue(MaxBackgroundLevelsProperty); }
|
|
|
|
|
|
set { SetValue(MaxBackgroundLevelsProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Minimum time interval between tile updates.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public TimeSpan UpdateInterval
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (TimeSpan)GetValue(UpdateIntervalProperty); }
|
|
|
|
|
|
set { SetValue(UpdateIntervalProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Controls if tiles are updated while the viewport is still changing.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool UpdateWhileViewportChanging
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (bool)GetValue(UpdateWhileViewportChangingProperty); }
|
|
|
|
|
|
set { SetValue(UpdateWhileViewportChangingProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Optional background brush. Sets MapBase.Background if not null and this layer is the base map layer.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Brush MapBackground
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (Brush)GetValue(MapBackgroundProperty); }
|
|
|
|
|
|
set { SetValue(MapBackgroundProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Optional foreground brush. Sets MapBase.Foreground if not null and this layer is the base map layer.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Brush MapForeground
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (Brush)GetValue(MapForegroundProperty); }
|
|
|
|
|
|
set { SetValue(MapForegroundProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public MapBase ParentMap
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return parentMap; }
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
if (parentMap != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
parentMap.ViewportChanged -= OnViewportChanged;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
parentMap = value;
|
|
|
|
|
|
|
|
|
|
|
|
if (parentMap != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
parentMap.ViewportChanged += OnViewportChanged;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-05 00:03:44 +02:00
|
|
|
|
updateTimer.Start();
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-11 22:27:57 +01:00
|
|
|
|
protected bool IsBaseMapLayer
|
2021-11-10 22:31:39 +01:00
|
|
|
|
{
|
2021-11-11 22:27:57 +01:00
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
return 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();
|
|
|
|
|
|
|
|
|
|
|
|
protected abstract Task UpdateTileLayer();
|
|
|
|
|
|
|
2021-07-05 00:03:44 +02:00
|
|
|
|
private async void OnViewportChanged(object sender, ViewportChangedEventArgs e)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (Children.Count == 0 || e.ProjectionChanged || Math.Abs(e.LongitudeOffset) > 180d)
|
|
|
|
|
|
{
|
2021-07-05 00:03:44 +02:00
|
|
|
|
await Update(); // update immediately when projection has changed or center has moved across 180° longitude
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
SetRenderTransform();
|
|
|
|
|
|
|
2020-10-27 23:34:14 +01:00
|
|
|
|
if (!UpdateWhileViewportChanging)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
2020-10-25 16:09:09 +01:00
|
|
|
|
updateTimer.Stop(); // restart
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-27 23:34:14 +01:00
|
|
|
|
updateTimer.Start();
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-05 00:03:44 +02:00
|
|
|
|
private Task Update()
|
2020-10-25 17:49:18 +01:00
|
|
|
|
{
|
|
|
|
|
|
updateTimer.Stop();
|
2020-10-25 18:14:38 +01:00
|
|
|
|
|
2021-07-05 00:03:44 +02:00
|
|
|
|
return UpdateTileLayer();
|
2020-10-25 17:49:18 +01:00
|
|
|
|
}
|
2020-03-20 18:12:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|