Updated MapTileLayer

This commit is contained in:
ClemensF 2020-10-29 21:46:41 +01:00
parent 8eb3c072f5
commit 0a17a73949
3 changed files with 99 additions and 95 deletions

View file

@ -60,7 +60,7 @@ namespace MapControl
public TileMatrix TileMatrix { get; private set; } public TileMatrix TileMatrix { get; private set; }
public List<Tile> Tiles { get; private set; } = new List<Tile>(); public List<Tile> Tiles { get; } = new List<Tile>();
/// <summary> /// <summary>
/// Minimum zoom level supported by the MapTileLayer. Default value is 0. /// Minimum zoom level supported by the MapTileLayer. Default value is 0.
@ -80,34 +80,73 @@ namespace MapControl
set { SetValue(MaxZoomLevelProperty, value); } 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) protected override void UpdateTileLayer(bool tileSourceChanged)
{ {
var updateTiles = false; var update = false;
if (ParentMap == null || !ParentMap.MapProjection.IsWebMercator) if (ParentMap == null || !ParentMap.MapProjection.IsWebMercator)
{ {
Tiles.Clear();
TileMatrix = null; TileMatrix = null;
updateTiles = true; update = true;
} }
else else
{ {
if (tileSourceChanged) if (tileSourceChanged)
{ {
Tiles.Clear(); Tiles.Clear();
updateTiles = true; update = true;
} }
if (SetTileMatrix()) if (SetTileMatrix())
{ {
SetRenderTransform(); 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,15 +192,19 @@ namespace MapControl
return true; 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<Tile>(); var newTiles = new List<Tile>();
var maxZoomLevel = Math.Min(TileMatrix.ZoomLevel, MaxZoomLevel);
if (maxZoomLevel >= MinZoomLevel)
{
var minZoomLevel = maxZoomLevel; var minZoomLevel = maxZoomLevel;
if (this == ParentMap.MapLayer) // load background tiles if (this == ParentMap.MapLayer) // load background tiles
@ -169,6 +212,10 @@ namespace MapControl
minZoomLevel = Math.Max(TileMatrix.ZoomLevel - MaxBackgroundLevels, MinZoomLevel); minZoomLevel = Math.Max(TileMatrix.ZoomLevel - MaxBackgroundLevels, MinZoomLevel);
} }
var oldTiles = Tiles.Where(t => t.ZoomLevel >= minZoomLevel && t.ZoomLevel <= maxZoomLevel).ToList();
Tiles.Clear();
for (var z = minZoomLevel; z <= maxZoomLevel; z++) for (var z = minZoomLevel; z <= maxZoomLevel; z++)
{ {
var tileSize = 1 << (TileMatrix.ZoomLevel - z); var tileSize = 1 << (TileMatrix.ZoomLevel - z);
@ -181,14 +228,14 @@ namespace MapControl
{ {
for (var x = x1; x <= x2; x++) for (var x = x1; x <= x2; x++)
{ {
var tile = Tiles.FirstOrDefault(t => t.ZoomLevel == z && t.X == x && t.Y == y); var tile = oldTiles.FirstOrDefault(t => t.ZoomLevel == z && t.X == x && t.Y == y);
if (tile == null) if (tile == null)
{ {
tile = new Tile(z, x, y); tile = new Tile(z, x, y);
var equivalentTile = Tiles.FirstOrDefault( var equivalentTile = oldTiles.FirstOrDefault(
t => t.ZoomLevel == z && t.XIndex == tile.XIndex && t.Y == y && t.Image.Source != null); t => t.ZoomLevel == z && t.XIndex == tile.XIndex && t.Y == y && !t.Pending);
if (equivalentTile != null) if (equivalentTile != null)
{ {
@ -196,54 +243,11 @@ namespace MapControl
} }
} }
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;
} }
} }
} }

View file

@ -2,7 +2,6 @@
// © 2020 Clemens Fischer // © 2020 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL) // Licensed under the Microsoft Public License (Ms-PL)
using System;
#if WINDOWS_UWP #if WINDOWS_UWP
using Windows.UI.Xaml; using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
@ -19,11 +18,6 @@ namespace MapControl
{ {
public partial class Tile 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) public Tile(int zoomLevel, int x, int y)
{ {
ZoomLevel = zoomLevel; ZoomLevel = zoomLevel;
@ -31,7 +25,9 @@ namespace MapControl
Y = y; Y = y;
} }
public bool Pending { get; set; } = true; public int ZoomLevel { get; }
public int X { get; }
public int Y { get; }
public int XIndex 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() private void FadeIn()
{ {
Image.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation Image.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation

View file

@ -6,12 +6,6 @@ namespace MapControl
{ {
public class TileMatrix 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) public TileMatrix(int zoomLevel, int xMin, int yMin, int xMax, int yMax)
{ {
ZoomLevel = zoomLevel; ZoomLevel = zoomLevel;
@ -20,5 +14,11 @@ namespace MapControl
XMax = xMax; XMax = xMax;
YMax = yMax; YMax = yMax;
} }
public int ZoomLevel { get; }
public int XMin { get; }
public int YMin { get; }
public int XMax { get; }
public int YMax { get; }
} }
} }