2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
|
|
|
|
|
// © 2017 Clemens Fischer
|
2012-05-04 12:52:20 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
using System.Collections.Generic;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using System.Linq;
|
2015-08-09 20:04:44 +02:00
|
|
|
|
#if NETFX_CORE
|
2014-07-01 18:57:44 +02:00
|
|
|
|
using Windows.Foundation;
|
2014-11-19 21:11:14 +01:00
|
|
|
|
using Windows.UI.Xaml;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
using Windows.UI.Xaml.Controls;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using Windows.UI.Xaml.Markup;
|
|
|
|
|
|
using Windows.UI.Xaml.Media;
|
|
|
|
|
|
#else
|
2012-04-25 22:02:53 +02:00
|
|
|
|
using System.Windows;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
using System.Windows.Controls;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
using System.Windows.Markup;
|
|
|
|
|
|
using System.Windows.Media;
|
2015-08-09 20:04:44 +02:00
|
|
|
|
using System.Windows.Threading;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
#endif
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
public interface ITileImageLoader
|
|
|
|
|
|
{
|
2017-07-17 21:31:09 +02:00
|
|
|
|
void LoadTiles(MapTileLayer tileLayer);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// <summary>
|
2015-11-28 21:09:25 +01:00
|
|
|
|
/// Fills the map viewport with map tiles from a TileSource.
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// </summary>
|
2015-08-09 20:04:44 +02:00
|
|
|
|
#if NETFX_CORE
|
2012-11-22 21:42:29 +01:00
|
|
|
|
[ContentProperty(Name = "TileSource")]
|
|
|
|
|
|
#else
|
2012-04-25 22:02:53 +02:00
|
|
|
|
[ContentProperty("TileSource")]
|
2012-11-22 21:42:29 +01:00
|
|
|
|
#endif
|
2017-06-25 23:05:48 +02:00
|
|
|
|
public partial class MapTileLayer : Panel, IMapLayer
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// <summary>
|
2017-07-17 21:31:09 +02:00
|
|
|
|
/// A default MapTileLayer using OpenStreetMap data.
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static MapTileLayer OpenStreetMapTileLayer
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
return new MapTileLayer
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
|
|
|
|
|
SourceName = "OpenStreetMap",
|
2014-11-19 21:11:14 +01:00
|
|
|
|
Description = "© [OpenStreetMap Contributors](http://www.openstreetmap.org/copyright)",
|
2016-01-14 17:56:25 +01:00
|
|
|
|
TileSource = new TileSource { UriFormat = "http://{c}.tile.openstreetmap.org/{z}/{x}/{y}.png" },
|
|
|
|
|
|
MaxZoomLevel = 19
|
2012-11-22 21:42:29 +01:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
public static readonly DependencyProperty TileSourceProperty = DependencyProperty.Register(
|
2017-06-25 23:05:48 +02:00
|
|
|
|
nameof(TileSource), typeof(TileSource), typeof(MapTileLayer),
|
2017-07-18 18:20:57 +02:00
|
|
|
|
new PropertyMetadata(null, (o, e) => ((MapTileLayer)o).TileSourcePropertyChanged()));
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty SourceNameProperty = DependencyProperty.Register(
|
2017-06-25 23:05:48 +02:00
|
|
|
|
nameof(SourceName), typeof(string), typeof(MapTileLayer), new PropertyMetadata(null));
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register(
|
2017-06-25 23:05:48 +02:00
|
|
|
|
nameof(Description), typeof(string), typeof(MapTileLayer), new PropertyMetadata(null));
|
2015-03-15 10:11:19 +01:00
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
public static readonly DependencyProperty ZoomLevelOffsetProperty = DependencyProperty.Register(
|
2017-06-25 23:05:48 +02:00
|
|
|
|
nameof(ZoomLevelOffset), typeof(double), typeof(MapTileLayer),
|
|
|
|
|
|
new PropertyMetadata(0d, (o, e) => ((MapTileLayer)o).UpdateTileGrid()));
|
2015-08-09 20:04:44 +02:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
public static readonly DependencyProperty MinZoomLevelProperty = DependencyProperty.Register(
|
2017-06-25 23:05:48 +02:00
|
|
|
|
nameof(MinZoomLevel), typeof(int), typeof(MapTileLayer), new PropertyMetadata(0));
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty MaxZoomLevelProperty = DependencyProperty.Register(
|
2017-06-25 23:05:48 +02:00
|
|
|
|
nameof(MaxZoomLevel), typeof(int), typeof(MapTileLayer), new PropertyMetadata(18));
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty MaxParallelDownloadsProperty = DependencyProperty.Register(
|
2017-06-25 23:05:48 +02:00
|
|
|
|
nameof(MaxParallelDownloads), typeof(int), typeof(MapTileLayer), new PropertyMetadata(4));
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
public static readonly DependencyProperty UpdateIntervalProperty = DependencyProperty.Register(
|
2017-06-25 23:05:48 +02:00
|
|
|
|
nameof(UpdateInterval), typeof(TimeSpan), typeof(MapTileLayer),
|
|
|
|
|
|
new PropertyMetadata(TimeSpan.FromSeconds(0.2), (o, e) => ((MapTileLayer)o).updateTimer.Interval = (TimeSpan)e.NewValue));
|
2015-08-09 20:04:44 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty UpdateWhileViewportChangingProperty = DependencyProperty.Register(
|
2017-06-25 23:05:48 +02:00
|
|
|
|
nameof(UpdateWhileViewportChanging), typeof(bool), typeof(MapTileLayer), new PropertyMetadata(true));
|
2015-08-09 20:04:44 +02:00
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
public static readonly DependencyProperty MapBackgroundProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(MapBackground), typeof(Brush), typeof(MapTileLayer), new PropertyMetadata(null));
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
public static readonly DependencyProperty MapForegroundProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(MapForeground), typeof(Brush), typeof(MapTileLayer), new PropertyMetadata(null));
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
private readonly DispatcherTimer updateTimer;
|
2014-11-19 21:11:14 +01:00
|
|
|
|
private MapBase parentMap;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
public MapTileLayer()
|
2014-10-19 21:50:23 +02:00
|
|
|
|
: this(new TileImageLoader())
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2014-10-19 21:50:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
public MapTileLayer(ITileImageLoader tileImageLoader)
|
2014-10-19 21:50:23 +02:00
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
Initialize();
|
2015-08-09 20:04:44 +02:00
|
|
|
|
|
|
|
|
|
|
RenderTransform = new MatrixTransform();
|
|
|
|
|
|
TileImageLoader = tileImageLoader;
|
|
|
|
|
|
Tiles = new List<Tile>();
|
|
|
|
|
|
|
|
|
|
|
|
updateTimer = new DispatcherTimer { Interval = UpdateInterval };
|
2015-11-11 19:48:50 +01:00
|
|
|
|
updateTimer.Tick += (s, e) => UpdateTileGrid();
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
partial void Initialize(); // Windows Runtime and Silverlight only
|
|
|
|
|
|
|
|
|
|
|
|
public ITileImageLoader TileImageLoader { get; private set; }
|
|
|
|
|
|
public ICollection<Tile> Tiles { get; private set; }
|
2015-11-11 19:48:50 +01:00
|
|
|
|
public TileGrid TileGrid { get; private set; }
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2015-03-15 10:11:19 +01:00
|
|
|
|
/// Provides map tile URIs or images.
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public TileSource TileSource
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (TileSource)GetValue(TileSourceProperty); }
|
|
|
|
|
|
set { SetValue(TileSourceProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-07-18 18:20:57 +02:00
|
|
|
|
/// Name of the TileSource. Used as component of a tile cache key.
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string SourceName
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (string)GetValue(SourceNameProperty); }
|
|
|
|
|
|
set { SetValue(SourceNameProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-07-18 18:20:57 +02:00
|
|
|
|
/// Description of the MapTileLayer. Used to display copyright information on top of the map.
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string Description
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (string)GetValue(DescriptionProperty); }
|
|
|
|
|
|
set { SetValue(DescriptionProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
/// <summary>
|
2017-07-17 21:31:09 +02:00
|
|
|
|
/// Adds an offset to the Map's ZoomLevel for a relative scale between the Map and the MapTileLayer.
|
2015-08-09 20:04:44 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public double ZoomLevelOffset
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (double)GetValue(ZoomLevelOffsetProperty); }
|
|
|
|
|
|
set { SetValue(ZoomLevelOffsetProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// <summary>
|
2017-07-17 21:31:09 +02:00
|
|
|
|
/// Minimum zoom level supported by the MapTileLayer.
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public int MinZoomLevel
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (int)GetValue(MinZoomLevelProperty); }
|
|
|
|
|
|
set { SetValue(MinZoomLevelProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-07-17 21:31:09 +02:00
|
|
|
|
/// Maximum zoom level supported by the MapTileLayer.
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public int MaxZoomLevel
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (int)GetValue(MaxZoomLevelProperty); }
|
|
|
|
|
|
set { SetValue(MaxZoomLevelProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-07-17 21:31:09 +02:00
|
|
|
|
/// Maximum number of parallel downloads that may be performed by the MapTileLayer's ITileImageLoader.
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public int MaxParallelDownloads
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (int)GetValue(MaxParallelDownloadsProperty); }
|
|
|
|
|
|
set { SetValue(MaxParallelDownloadsProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Minimum time interval between tile updates.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public TimeSpan UpdateInterval
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (TimeSpan)GetValue(UpdateIntervalProperty); }
|
|
|
|
|
|
set { SetValue(UpdateIntervalProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-06-25 23:05:48 +02:00
|
|
|
|
/// Controls if tiles are updated while the viewport is still changing.
|
2015-08-09 20:04:44 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool UpdateWhileViewportChanging
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (bool)GetValue(UpdateWhileViewportChangingProperty); }
|
|
|
|
|
|
set { SetValue(UpdateWhileViewportChangingProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// <summary>
|
2017-07-18 18:20:57 +02:00
|
|
|
|
/// Optional background brush. Sets MapBase.Background if not null and the MapTileLayer is the base map layer.
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// </summary>
|
2017-06-25 23:05:48 +02:00
|
|
|
|
public Brush MapBackground
|
2014-11-19 21:11:14 +01:00
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
get { return (Brush)GetValue(MapBackgroundProperty); }
|
|
|
|
|
|
set { SetValue(MapBackgroundProperty, value); }
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-07-18 18:20:57 +02:00
|
|
|
|
/// Optional foreground brush. Sets MapBase.Foreground if not null and the MapTileLayer is the base map layer.
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// </summary>
|
2017-06-25 23:05:48 +02:00
|
|
|
|
public Brush MapForeground
|
2014-11-19 21:11:14 +01:00
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
get { return (Brush)GetValue(MapForegroundProperty); }
|
|
|
|
|
|
set { SetValue(MapForegroundProperty, value); }
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
public MapBase ParentMap
|
2014-06-11 23:03:13 +02:00
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
get { return parentMap; }
|
2014-06-11 23:03:13 +02:00
|
|
|
|
set
|
|
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
if (parentMap != null)
|
2014-06-11 23:03:13 +02:00
|
|
|
|
{
|
2016-04-19 19:36:03 +02:00
|
|
|
|
parentMap.ViewportChanged -= OnViewportChanged;
|
2014-06-11 23:03:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
parentMap = value;
|
2014-10-19 21:50:23 +02:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
if (parentMap != null)
|
|
|
|
|
|
{
|
2016-04-19 19:36:03 +02:00
|
|
|
|
parentMap.ViewportChanged += OnViewportChanged;
|
2015-08-09 20:04:44 +02:00
|
|
|
|
}
|
2015-11-11 19:48:50 +01:00
|
|
|
|
|
|
|
|
|
|
UpdateTileGrid();
|
2015-08-09 20:04:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
protected override Size MeasureOverride(Size availableSize)
|
2015-08-09 20:04:44 +02:00
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (UIElement element in Children)
|
2015-08-09 20:04:44 +02:00
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
element.Measure(availableSize);
|
2015-08-09 20:04:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
return new Size();
|
|
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
protected override Size ArrangeOverride(Size finalSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (TileGrid != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var tile in Tiles)
|
2015-11-11 19:48:50 +01:00
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
var tileSize = TileSource.TileSize << (TileGrid.ZoomLevel - tile.ZoomLevel);
|
|
|
|
|
|
var x = tileSize * tile.X - TileSource.TileSize * TileGrid.XMin;
|
|
|
|
|
|
var y = tileSize * tile.Y - TileSource.TileSize * TileGrid.YMin;
|
|
|
|
|
|
|
|
|
|
|
|
tile.Image.Width = tileSize;
|
|
|
|
|
|
tile.Image.Height = tileSize;
|
|
|
|
|
|
tile.Image.Arrange(new Rect(x, y, tileSize, tileSize));
|
2015-11-11 19:48:50 +01:00
|
|
|
|
}
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
return finalSize;
|
2014-10-19 21:50:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
protected virtual void UpdateTileGrid()
|
2014-10-19 21:50:23 +02:00
|
|
|
|
{
|
2015-08-09 20:04:44 +02:00
|
|
|
|
updateTimer.Stop();
|
|
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
if (parentMap != null && parentMap.MapProjection.IsWebMercator)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
var tileGrid = GetTileGrid();
|
2015-08-09 20:04:44 +02:00
|
|
|
|
|
2015-11-11 19:48:50 +01:00
|
|
|
|
if (!tileGrid.Equals(TileGrid))
|
|
|
|
|
|
{
|
|
|
|
|
|
TileGrid = tileGrid;
|
2015-08-09 20:04:44 +02:00
|
|
|
|
SetRenderTransform();
|
2017-07-17 21:31:09 +02:00
|
|
|
|
UpdateTiles();
|
2015-08-09 20:04:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-11-11 19:48:50 +01:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
TileGrid = null;
|
2017-07-18 18:20:57 +02:00
|
|
|
|
UpdateTiles();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void TileSourcePropertyChanged()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (TileGrid != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Tiles.Clear();
|
|
|
|
|
|
UpdateTiles();
|
2015-11-11 19:48:50 +01:00
|
|
|
|
}
|
2015-08-09 20:04:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (TileGrid == null || e.ProjectionChanged || Math.Abs(e.LongitudeOffset) > 180d)
|
|
|
|
|
|
{
|
2017-07-18 18:20:57 +02:00
|
|
|
|
UpdateTileGrid(); // update immediately when projection has changed or center has moved across 180° longitude
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
SetRenderTransform();
|
|
|
|
|
|
|
|
|
|
|
|
if (updateTimer.IsEnabled && !UpdateWhileViewportChanging)
|
|
|
|
|
|
{
|
|
|
|
|
|
updateTimer.Stop(); // restart
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!updateTimer.IsEnabled)
|
|
|
|
|
|
{
|
|
|
|
|
|
updateTimer.Start();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private TileGrid GetTileGrid()
|
|
|
|
|
|
{
|
|
|
|
|
|
var tileZoomLevel = Math.Max(0, (int)Math.Round(parentMap.ZoomLevel + ZoomLevelOffset));
|
|
|
|
|
|
var tileScale = (1 << tileZoomLevel);
|
|
|
|
|
|
var scale = tileScale / (Math.Pow(2d, parentMap.ZoomLevel) * TileSource.TileSize);
|
2017-07-25 20:21:02 +02:00
|
|
|
|
var tileCenterX = tileScale * (0.5 + parentMap.Center.Longitude / 360d);
|
|
|
|
|
|
var tileCenterY = tileScale * (0.5 - WebMercatorProjection.LatitudeToY(parentMap.Center.Latitude) / 360d);
|
|
|
|
|
|
var viewCenterX = parentMap.RenderSize.Width / 2d;
|
|
|
|
|
|
var viewCenterY = parentMap.RenderSize.Height / 2d;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
var transform = new MatrixTransform
|
|
|
|
|
|
{
|
2017-07-25 20:21:02 +02:00
|
|
|
|
Matrix = MatrixEx.TranslateScaleRotateTranslate(viewCenterX, viewCenterY, scale, scale, -parentMap.Heading, tileCenterX, tileCenterY)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var bounds = transform.TransformBounds(new Rect(0d, 0d, parentMap.RenderSize.Width, parentMap.RenderSize.Height));
|
|
|
|
|
|
|
|
|
|
|
|
return new TileGrid(tileZoomLevel,
|
|
|
|
|
|
(int)Math.Floor(bounds.X), (int)Math.Floor(bounds.Y),
|
|
|
|
|
|
(int)Math.Floor(bounds.X + bounds.Width), (int)Math.Floor(bounds.Y + bounds.Height));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void SetRenderTransform()
|
|
|
|
|
|
{
|
|
|
|
|
|
var tileScale = (1 << TileGrid.ZoomLevel);
|
|
|
|
|
|
var scale = Math.Pow(2d, parentMap.ZoomLevel) / tileScale;
|
2017-07-25 20:21:02 +02:00
|
|
|
|
var tileCenterX = tileScale * (0.5 + parentMap.Center.Longitude / 360d);
|
|
|
|
|
|
var tileCenterY = tileScale * (0.5 - WebMercatorProjection.LatitudeToY(parentMap.Center.Latitude) / 360d);
|
|
|
|
|
|
var tileOriginX = TileSource.TileSize * (tileCenterX - TileGrid.XMin);
|
|
|
|
|
|
var tileOriginY = TileSource.TileSize * (tileCenterY - TileGrid.YMin);
|
|
|
|
|
|
var viewCenterX = parentMap.RenderSize.Width / 2d;
|
|
|
|
|
|
var viewCenterY = parentMap.RenderSize.Height / 2d;
|
|
|
|
|
|
|
|
|
|
|
|
((MatrixTransform)RenderTransform).Matrix = MatrixEx.TranslateScaleRotateTranslate(
|
|
|
|
|
|
tileOriginX, tileOriginY, scale, scale, parentMap.Heading, viewCenterX, viewCenterY);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-17 21:31:09 +02:00
|
|
|
|
private void UpdateTiles()
|
2014-11-19 21:11:14 +01:00
|
|
|
|
{
|
2012-11-22 21:42:29 +01:00
|
|
|
|
var newTiles = new List<Tile>();
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
if (parentMap != null && TileGrid != null && TileSource != null)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2015-11-11 19:48:50 +01:00
|
|
|
|
var maxZoomLevel = Math.Min(TileGrid.ZoomLevel, MaxZoomLevel);
|
2017-07-17 21:31:09 +02:00
|
|
|
|
var minZoomLevel = parentMap.MapLayer == this ? MinZoomLevel : maxZoomLevel; // load background tiles only if this is the base layer
|
2012-11-06 19:49:29 +01:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
for (var z = minZoomLevel; z <= maxZoomLevel; z++)
|
|
|
|
|
|
{
|
2015-11-11 19:48:50 +01:00
|
|
|
|
var tileSize = 1 << (TileGrid.ZoomLevel - z);
|
|
|
|
|
|
var x1 = (int)Math.Floor((double)TileGrid.XMin / tileSize); // may be negative
|
|
|
|
|
|
var x2 = TileGrid.XMax / tileSize;
|
|
|
|
|
|
var y1 = Math.Max(TileGrid.YMin / tileSize, 0);
|
|
|
|
|
|
var y2 = Math.Min(TileGrid.YMax / tileSize, (1 << z) - 1);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
for (var y = y1; y <= y2; y++)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (var x = x1; x <= x2; x++)
|
|
|
|
|
|
{
|
2015-08-09 20:04:44 +02:00
|
|
|
|
var tile = Tiles.FirstOrDefault(t => t.ZoomLevel == z && t.X == x && t.Y == y);
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
if (tile == null)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
tile = new Tile(z, x, y);
|
|
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
var equivalentTile = Tiles.FirstOrDefault(
|
2015-01-20 17:52:02 +01:00
|
|
|
|
t => t.ZoomLevel == z && t.XIndex == tile.XIndex && t.Y == y && t.Image.Source != null);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
|
|
|
|
|
if (equivalentTile != null)
|
|
|
|
|
|
{
|
2017-07-18 18:20:57 +02:00
|
|
|
|
tile.SetImage(equivalentTile.Image.Source, false); // do not animate to avoid flicker when crossing 180° longitude
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
2012-11-06 19:49:29 +01:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
newTiles.Add(tile);
|
|
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
Tiles = newTiles;
|
2017-07-18 18:20:57 +02:00
|
|
|
|
|
|
|
|
|
|
Children.Clear();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var tile in Tiles)
|
|
|
|
|
|
{
|
|
|
|
|
|
Children.Add(tile.Image);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TileImageLoader.LoadTiles(this);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2012-06-24 23:42:11 +02:00
|
|
|
|
}
|