From 0a17a73949e3229a09ebbbbd9ad6faf324a3824e Mon Sep 17 00:00:00 2001 From: ClemensF Date: Thu, 29 Oct 2020 21:46:41 +0100 Subject: [PATCH] Updated MapTileLayer --- MapControl/Shared/MapTileLayer.cs | 168 +++++++++++++++--------------- MapControl/Shared/Tile.cs | 14 +-- MapControl/Shared/TileMatrix.cs | 12 +-- 3 files changed, 99 insertions(+), 95 deletions(-) diff --git a/MapControl/Shared/MapTileLayer.cs b/MapControl/Shared/MapTileLayer.cs index 2a55526d..528d6986 100644 --- a/MapControl/Shared/MapTileLayer.cs +++ b/MapControl/Shared/MapTileLayer.cs @@ -60,7 +60,7 @@ namespace MapControl public TileMatrix TileMatrix { get; private set; } - public List Tiles { get; private set; } = new List(); + public List Tiles { get; } = new List(); /// /// Minimum zoom level supported by the MapTileLayer. Default value is 0. @@ -80,34 +80,73 @@ namespace MapControl set { SetValue(MaxZoomLevelProperty, value); } } + 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) + { + if (TileMatrix != null) + { + foreach (var tile in Tiles) + { + var tileSize = TileSize << (TileMatrix.ZoomLevel - tile.ZoomLevel); + var x = tileSize * tile.X - TileSize * TileMatrix.XMin; + var y = tileSize * tile.Y - TileSize * TileMatrix.YMin; + + tile.Image.Width = tileSize; + tile.Image.Height = tileSize; + tile.Image.Arrange(new Rect(x, y, tileSize, tileSize)); + } + } + + return finalSize; + } + protected override void UpdateTileLayer(bool tileSourceChanged) { - var updateTiles = false; + var update = false; if (ParentMap == null || !ParentMap.MapProjection.IsWebMercator) { - Tiles.Clear(); TileMatrix = null; - updateTiles = true; + update = true; } else { if (tileSourceChanged) { Tiles.Clear(); - updateTiles = true; + update = true; } if (SetTileMatrix()) { SetRenderTransform(); - updateTiles = true; + update = true; } } - if (updateTiles) + if (update) { - UpdateTiles(); + SetTiles(); + + Children.Clear(); + + foreach (var tile in Tiles) + { + Children.Add(tile.Image); + } + + TileImageLoader.LoadTiles(Tiles, TileSource, SourceName); } } @@ -153,97 +192,62 @@ namespace MapControl return true; } - private void UpdateTiles() + private void SetTiles() { - if (ParentMap != null && TileMatrix != null && TileSource != null) + int maxZoomLevel; + + if (TileSource == null || + TileMatrix == null || + (maxZoomLevel = Math.Min(TileMatrix.ZoomLevel, MaxZoomLevel)) < MinZoomLevel) + { + Tiles.Clear(); + } + else { var newTiles = new List(); - var maxZoomLevel = Math.Min(TileMatrix.ZoomLevel, MaxZoomLevel); + var minZoomLevel = maxZoomLevel; - if (maxZoomLevel >= MinZoomLevel) + if (this == ParentMap.MapLayer) // load background tiles { - var minZoomLevel = maxZoomLevel; + minZoomLevel = Math.Max(TileMatrix.ZoomLevel - MaxBackgroundLevels, MinZoomLevel); + } - if (this == ParentMap.MapLayer) // load background tiles + var oldTiles = Tiles.Where(t => t.ZoomLevel >= minZoomLevel && t.ZoomLevel <= maxZoomLevel).ToList(); + + Tiles.Clear(); + + for (var z = minZoomLevel; z <= maxZoomLevel; z++) + { + 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); + + for (var y = y1; y <= y2; y++) { - minZoomLevel = Math.Max(TileMatrix.ZoomLevel - MaxBackgroundLevels, MinZoomLevel); - } - - for (var z = minZoomLevel; z <= maxZoomLevel; z++) - { - 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); - - for (var y = y1; y <= y2; y++) + for (var x = x1; x <= x2; x++) { - for (var x = x1; x <= x2; x++) + var tile = oldTiles.FirstOrDefault(t => t.ZoomLevel == z && t.X == x && t.Y == y); + + if (tile == null) { - var tile = Tiles.FirstOrDefault(t => t.ZoomLevel == z && t.X == x && t.Y == y); + tile = new Tile(z, x, y); - if (tile == null) + var equivalentTile = oldTiles.FirstOrDefault( + t => t.ZoomLevel == z && t.XIndex == tile.XIndex && t.Y == y && !t.Pending); + + if (equivalentTile != null) { - 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 - } + tile.SetImage(equivalentTile.Image.Source, false); // no fade-in animation } - - newTiles.Add(tile); } + + Tiles.Add(tile); } } } - - Tiles = newTiles; } - - Children.Clear(); - - foreach (var tile in Tiles) - { - Children.Add(tile.Image); - } - - TileImageLoader.LoadTiles(Tiles, TileSource, SourceName); - } - - 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) - { - if (TileMatrix != null) - { - foreach (var tile in Tiles) - { - var tileSize = TileSize << (TileMatrix.ZoomLevel - tile.ZoomLevel); - var x = tileSize * tile.X - TileSize * TileMatrix.XMin; - var y = tileSize * tile.Y - TileSize * TileMatrix.YMin; - - tile.Image.Width = tileSize; - tile.Image.Height = tileSize; - tile.Image.Arrange(new Rect(x, y, tileSize, tileSize)); - } - } - - return finalSize; } } } diff --git a/MapControl/Shared/Tile.cs b/MapControl/Shared/Tile.cs index 592abba6..27db2d73 100644 --- a/MapControl/Shared/Tile.cs +++ b/MapControl/Shared/Tile.cs @@ -2,7 +2,6 @@ // © 2020 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) -using System; #if WINDOWS_UWP using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; @@ -19,11 +18,6 @@ namespace MapControl { public partial class Tile { - public readonly int ZoomLevel; - public readonly int X; - public readonly int Y; - public readonly Image Image = new Image { Opacity = 0d, Stretch = Stretch.Fill }; - public Tile(int zoomLevel, int x, int y) { ZoomLevel = zoomLevel; @@ -31,7 +25,9 @@ namespace MapControl Y = y; } - public bool Pending { get; set; } = true; + public int ZoomLevel { get; } + public int X { get; } + public int Y { get; } public int XIndex { @@ -42,6 +38,10 @@ namespace MapControl } } + public Image Image { get; } = new Image { Opacity = 0d, Stretch = Stretch.Fill }; + + public bool Pending { get; set; } = true; + private void FadeIn() { Image.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation diff --git a/MapControl/Shared/TileMatrix.cs b/MapControl/Shared/TileMatrix.cs index ef786815..97aa6f6a 100644 --- a/MapControl/Shared/TileMatrix.cs +++ b/MapControl/Shared/TileMatrix.cs @@ -6,12 +6,6 @@ namespace MapControl { public class TileMatrix { - public readonly int ZoomLevel; - public readonly int XMin; - public readonly int YMin; - public readonly int XMax; - public readonly int YMax; - public TileMatrix(int zoomLevel, int xMin, int yMin, int xMax, int yMax) { ZoomLevel = zoomLevel; @@ -20,5 +14,11 @@ namespace MapControl XMax = xMax; YMax = yMax; } + + public int ZoomLevel { get; } + public int XMin { get; } + public int YMin { get; } + public int XMax { get; } + public int YMax { get; } } }