2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2020-01-09 19:40:10 +01:00
|
|
|
|
// © 2020 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;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
#if WINDOWS_UWP
|
2014-07-01 18:57:44 +02:00
|
|
|
|
using Windows.Foundation;
|
2014-11-19 21:11:14 +01:00
|
|
|
|
using Windows.UI.Xaml;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using Windows.UI.Xaml.Media;
|
|
|
|
|
|
#else
|
2012-04-25 22:02:53 +02:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Media;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
#endif
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// <summary>
|
2020-03-28 21:53:38 +01:00
|
|
|
|
/// Fills the viewport with map tiles from a TileSource.
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// </summary>
|
2020-03-20 18:12:56 +01:00
|
|
|
|
public class MapTileLayer : MapTileLayerBase
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2020-03-22 18:33:34 +01:00
|
|
|
|
public const int TileSize = 256;
|
2020-03-24 16:13:25 +01:00
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
public static readonly Point TileMatrixTopLeft = new Point(
|
2020-03-24 16:13:25 +01:00
|
|
|
|
-180d * MapProjection.Wgs84MetersPerDegree, 180d * MapProjection.Wgs84MetersPerDegree);
|
2020-03-22 18:33:34 +01: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)",
|
2018-08-14 23:23:43 +02:00
|
|
|
|
TileSource = new TileSource { UriFormat = "https://{c}.tile.openstreetmap.org/{z}/{x}/{y}.png" },
|
2016-01-14 17:56:25 +01:00
|
|
|
|
MaxZoomLevel = 19
|
2012-11-22 21:42:29 +01: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
|
|
|
|
|
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)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
: base(tileImageLoader)
|
2014-10-19 21:50:23 +02:00
|
|
|
|
{
|
2017-09-05 20:57:17 +02:00
|
|
|
|
}
|
2015-08-09 20:04:44 +02:00
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
public TileMatrix TileMatrix { get; private set; }
|
2015-08-09 20:04:44 +02:00
|
|
|
|
|
2020-03-23 17:13:50 +01:00
|
|
|
|
public IReadOnlyCollection<Tile> Tiles { get; private set; } = new List<Tile>();
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// <summary>
|
2019-07-13 00:08:56 +02:00
|
|
|
|
/// Minimum zoom level supported by the MapTileLayer. Default value is 0.
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public int MinZoomLevel
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (int)GetValue(MinZoomLevelProperty); }
|
|
|
|
|
|
set { SetValue(MinZoomLevelProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-07-13 00:08:56 +02:00
|
|
|
|
/// Maximum zoom level supported by the MapTileLayer. Default value is 18.
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public int MaxZoomLevel
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (int)GetValue(MaxZoomLevelProperty); }
|
|
|
|
|
|
set { SetValue(MaxZoomLevelProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-20 18:12:56 +01:00
|
|
|
|
protected override void TileSourcePropertyChanged()
|
2017-07-18 18:20:57 +02:00
|
|
|
|
{
|
2020-03-26 19:08:20 +01:00
|
|
|
|
if (TileMatrix != null)
|
2017-07-18 18:20:57 +02:00
|
|
|
|
{
|
2018-08-21 23:56:54 +02:00
|
|
|
|
Tiles = new List<Tile>();
|
2017-07-18 18:20:57 +02:00
|
|
|
|
UpdateTiles();
|
2015-11-11 19:48:50 +01:00
|
|
|
|
}
|
2015-08-09 20:04:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-20 18:12:56 +01:00
|
|
|
|
protected override void UpdateTileLayer()
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2020-03-20 18:12:56 +01:00
|
|
|
|
UpdateTimer.Stop();
|
|
|
|
|
|
|
|
|
|
|
|
if (ParentMap == null || !ParentMap.MapProjection.IsWebMercator)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2020-03-26 19:08:20 +01:00
|
|
|
|
TileMatrix = null;
|
2020-03-20 18:12:56 +01:00
|
|
|
|
UpdateTiles();
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
2020-03-26 19:08:20 +01:00
|
|
|
|
else if (SetTileMatrix())
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
|
|
|
|
|
SetRenderTransform();
|
2020-03-20 18:12:56 +01:00
|
|
|
|
UpdateTiles();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2020-03-20 18:12:56 +01:00
|
|
|
|
protected override void SetRenderTransform()
|
|
|
|
|
|
{
|
2020-03-26 19:08:20 +01:00
|
|
|
|
// tile matrix origin in pixels
|
2020-03-20 18:12:56 +01:00
|
|
|
|
//
|
2020-03-26 19:08:20 +01:00
|
|
|
|
var tileMatrixOrigin = new Point(TileSize * TileMatrix.XMin, TileSize * TileMatrix.YMin);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2020-03-28 21:53:38 +01:00
|
|
|
|
var tileMatrixScale = ViewTransform.ZoomLevelToScale(TileMatrix.ZoomLevel);
|
|
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
((MatrixTransform)RenderTransform).Matrix = ParentMap.ViewTransform.GetTileLayerTransform(
|
2020-03-28 21:53:38 +01:00
|
|
|
|
tileMatrixScale, TileMatrixTopLeft, tileMatrixOrigin);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
private bool SetTileMatrix()
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2020-03-26 19:08:20 +01:00
|
|
|
|
var tileMatrixZoomLevel = (int)Math.Floor(ParentMap.ZoomLevel + 0.001); // avoid rounding issues
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
2020-03-28 21:53:38 +01:00
|
|
|
|
var tileMatrixScale = ViewTransform.ZoomLevelToScale(tileMatrixZoomLevel);
|
|
|
|
|
|
|
|
|
|
|
|
// bounds in tile pixels from view size
|
2020-03-20 18:12:56 +01:00
|
|
|
|
//
|
2020-03-26 19:08:20 +01:00
|
|
|
|
var tileBounds = ParentMap.ViewTransform.GetTileMatrixBounds(
|
2020-03-28 21:53:38 +01:00
|
|
|
|
tileMatrixScale, TileMatrixTopLeft, ParentMap.RenderSize);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2020-03-22 18:33:34 +01:00
|
|
|
|
// tile column and row index bounds
|
2020-03-20 18:12:56 +01:00
|
|
|
|
//
|
2020-03-22 18:33:34 +01:00
|
|
|
|
var xMin = (int)Math.Floor(tileBounds.X / TileSize);
|
|
|
|
|
|
var yMin = (int)Math.Floor(tileBounds.Y / TileSize);
|
|
|
|
|
|
var xMax = (int)Math.Floor((tileBounds.X + tileBounds.Width) / TileSize);
|
|
|
|
|
|
var yMax = (int)Math.Floor((tileBounds.Y + tileBounds.Height) / TileSize);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
if (TileMatrix != null &&
|
|
|
|
|
|
TileMatrix.ZoomLevel == tileMatrixZoomLevel &&
|
|
|
|
|
|
TileMatrix.XMin == xMin && TileMatrix.YMin == yMin &&
|
|
|
|
|
|
TileMatrix.XMax == xMax && TileMatrix.YMax == yMax)
|
2020-03-20 18:12:56 +01:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
TileMatrix = new TileMatrix(tileMatrixZoomLevel, xMin, yMin, xMax, yMax);
|
2020-03-20 18:12:56 +01:00
|
|
|
|
|
|
|
|
|
|
return true;
|
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
|
|
|
|
|
2020-03-26 19:08:20 +01:00
|
|
|
|
if (ParentMap != null && TileMatrix != null && TileSource != null)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2020-03-26 19:08:20 +01:00
|
|
|
|
var maxZoomLevel = Math.Min(TileMatrix.ZoomLevel, MaxZoomLevel);
|
2017-10-07 17:43:28 +02:00
|
|
|
|
|
2019-07-13 00:08:56 +02:00
|
|
|
|
if (maxZoomLevel >= MinZoomLevel)
|
2017-10-07 17:43:28 +02:00
|
|
|
|
{
|
2019-07-13 00:08:56 +02:00
|
|
|
|
var minZoomLevel = maxZoomLevel;
|
2012-11-06 19:49:29 +01:00
|
|
|
|
|
2020-03-20 18:12:56 +01:00
|
|
|
|
if (this == ParentMap.MapLayer) // load background tiles
|
2019-07-13 00:08:56 +02:00
|
|
|
|
{
|
2020-03-26 19:08:20 +01:00
|
|
|
|
minZoomLevel = Math.Max(TileMatrix.ZoomLevel - MaxBackgroundLevels, MinZoomLevel);
|
2019-07-13 00:08:56 +02:00
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2019-07-13 00:08:56 +02:00
|
|
|
|
for (var z = minZoomLevel; z <= maxZoomLevel; z++)
|
2014-11-19 21:11:14 +01:00
|
|
|
|
{
|
2020-03-26 19:08:20 +01:00
|
|
|
|
var tileSize = 1 << (TileMatrix.ZoomLevel - z);
|
|
|
|
|
|
var x1 = (int)Math.Floor((double)TileMatrix.XMin / tileSize); // may be negative
|
|
|
|
|
|
var x2 = TileMatrix.XMax / tileSize;
|
|
|
|
|
|
var y1 = Math.Max(TileMatrix.YMin / tileSize, 0);
|
|
|
|
|
|
var y2 = Math.Min(TileMatrix.YMax / tileSize, (1 << z) - 1);
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2019-07-13 00:08:56 +02:00
|
|
|
|
for (var y = y1; y <= y2; y++)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (var x = x1; x <= x2; x++)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2019-07-13 00:08:56 +02:00
|
|
|
|
var tile = Tiles.FirstOrDefault(t => t.ZoomLevel == z && t.X == x && t.Y == y);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
2019-07-13 00:08:56 +02:00
|
|
|
|
if (tile == null)
|
2014-11-19 21:11:14 +01:00
|
|
|
|
{
|
2019-07-13 00:08:56 +02:00
|
|
|
|
tile = new Tile(z, x, y);
|
|
|
|
|
|
|
|
|
|
|
|
var equivalentTile = Tiles.FirstOrDefault(
|
|
|
|
|
|
t => t.ZoomLevel == z && t.XIndex == tile.XIndex && t.Y == y && t.Image.Source != null);
|
|
|
|
|
|
|
|
|
|
|
|
if (equivalentTile != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
tile.SetImage(equivalentTile.Image.Source, false); // no fade-in animation
|
|
|
|
|
|
}
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
2012-11-06 19:49:29 +01:00
|
|
|
|
|
2019-07-13 00:08:56 +02:00
|
|
|
|
newTiles.Add(tile);
|
|
|
|
|
|
}
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-10 18:00:25 +02:00
|
|
|
|
TileImageLoader.LoadTilesAsync(Tiles, TileSource, SourceName);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
2020-03-23 17:13:50 +01:00
|
|
|
|
|
|
|
|
|
|
protected override Size MeasureOverride(Size availableSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var tile in Tiles)
|
|
|
|
|
|
{
|
|
|
|
|
|
tile.Image.Measure(availableSize);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new Size();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override Size ArrangeOverride(Size finalSize)
|
|
|
|
|
|
{
|
2020-03-26 19:08:20 +01:00
|
|
|
|
if (TileMatrix != null)
|
2020-03-23 17:13:50 +01:00
|
|
|
|
{
|
|
|
|
|
|
foreach (var tile in Tiles)
|
|
|
|
|
|
{
|
2020-03-26 19:08:20 +01:00
|
|
|
|
var tileSize = TileSize << (TileMatrix.ZoomLevel - tile.ZoomLevel);
|
|
|
|
|
|
var x = tileSize * tile.X - TileSize * TileMatrix.XMin;
|
|
|
|
|
|
var y = tileSize * tile.Y - TileSize * TileMatrix.YMin;
|
2020-03-23 17:13:50 +01:00
|
|
|
|
|
|
|
|
|
|
tile.Image.Width = tileSize;
|
|
|
|
|
|
tile.Image.Height = tileSize;
|
|
|
|
|
|
tile.Image.Arrange(new Rect(x, y, tileSize, tileSize));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return finalSize;
|
|
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
2012-06-24 23:42:11 +02:00
|
|
|
|
}
|