mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
Updated MapTileLayer
This commit is contained in:
parent
8eb3c072f5
commit
0a17a73949
|
|
@ -60,7 +60,7 @@ namespace MapControl
|
|||
|
||||
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>
|
||||
/// 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<Tile>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue