XAML-Map-Control/MapControl/TileLayer.cs

300 lines
10 KiB
C#
Raw Normal View History

// XAML Map Control - http://xamlmapcontrol.codeplex.com/
2015-01-20 17:52:02 +01:00
// © 2015 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;
using System.Linq;
#if WINDOWS_RUNTIME
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml.Media;
#else
2012-04-25 22:02:53 +02:00
using System.Windows;
using System.Windows.Markup;
using System.Windows.Media;
#endif
2012-04-25 22:02:53 +02:00
namespace MapControl
{
2012-05-04 12:52:20 +02:00
/// <summary>
/// Fills a rectangular area with map tiles from a TileSource.
2012-05-04 12:52:20 +02:00
/// </summary>
#if WINDOWS_RUNTIME
[ContentProperty(Name = "TileSource")]
#else
2012-04-25 22:02:53 +02:00
[ContentProperty("TileSource")]
#endif
public partial class TileLayer : PanelBase, IMapElement
2012-04-25 22:02:53 +02:00
{
public static TileLayer Default
{
get
{
return new TileLayer
{
SourceName = "OpenStreetMap",
Description = "© [OpenStreetMap Contributors](http://www.openstreetmap.org/copyright)",
TileSource = new TileSource { UriFormat = "http://{c}.tile.openstreetmap.org/{z}/{x}/{y}.png" }
};
}
}
public static readonly DependencyProperty TileSourceProperty = DependencyProperty.Register(
"TileSource", typeof(TileSource), typeof(TileLayer),
2015-01-20 17:52:02 +01:00
new PropertyMetadata(null, (o, e) => ((TileLayer)o).UpdateTiles(true)));
public static readonly DependencyProperty SourceNameProperty = DependencyProperty.Register(
"SourceName", typeof(string), typeof(TileLayer), new PropertyMetadata(null));
public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register(
"Description", typeof(string), typeof(TileLayer), new PropertyMetadata(null));
public static readonly DependencyProperty LogoImageProperty = DependencyProperty.Register(
"LogoImage", typeof(ImageSource), typeof(TileLayer), new PropertyMetadata(null));
public static readonly DependencyProperty MinZoomLevelProperty = DependencyProperty.Register(
"MinZoomLevel", typeof(int), typeof(TileLayer), new PropertyMetadata(0));
public static readonly DependencyProperty MaxZoomLevelProperty = DependencyProperty.Register(
"MaxZoomLevel", typeof(int), typeof(TileLayer), new PropertyMetadata(18));
public static readonly DependencyProperty MaxParallelDownloadsProperty = DependencyProperty.Register(
"MaxParallelDownloads", typeof(int), typeof(TileLayer), new PropertyMetadata(4));
public static readonly DependencyProperty ForegroundProperty = DependencyProperty.Register(
"Foreground", typeof(Brush), typeof(TileLayer), new PropertyMetadata(null));
public static readonly new DependencyProperty BackgroundProperty = DependencyProperty.Register(
"Background", typeof(Brush), typeof(TileLayer), new PropertyMetadata(null));
private readonly ITileImageLoader tileImageLoader;
private List<Tile> tiles = new List<Tile>();
private MapBase parentMap;
2012-04-25 22:02:53 +02:00
public TileLayer()
: this(new TileImageLoader())
2012-04-25 22:02:53 +02:00
{
}
public TileLayer(ITileImageLoader tileImageLoader)
{
this.tileImageLoader = tileImageLoader;
Initialize();
}
partial void Initialize();
/// <summary>
/// Provides map tile URIs or images.
/// </summary>
public TileSource TileSource
{
get { return (TileSource)GetValue(TileSourceProperty); }
set { SetValue(TileSourceProperty, value); }
}
/// <summary>
/// Name of the TileSource. Used as key in a TileLayerCollection and as component of a tile cache key.
/// </summary>
public string SourceName
{
get { return (string)GetValue(SourceNameProperty); }
set { SetValue(SourceNameProperty, value); }
}
/// <summary>
/// Description of the TileLayer. Used to display copyright information on top of the map.
/// </summary>
public string Description
{
get { return (string)GetValue(DescriptionProperty); }
set { SetValue(DescriptionProperty, value); }
}
/// <summary>
/// Logo image. Used to display a provider brand logo on top of the map.
/// </summary>
public ImageSource LogoImage
{
get { return (ImageSource)GetValue(LogoImageProperty); }
set { SetValue(LogoImageProperty, value); }
}
/// <summary>
/// Minimum zoom level supported by the TileLayer.
/// </summary>
public int MinZoomLevel
{
get { return (int)GetValue(MinZoomLevelProperty); }
set { SetValue(MinZoomLevelProperty, value); }
}
/// <summary>
/// Maximum zoom level supported by the TileLayer.
/// </summary>
public int MaxZoomLevel
{
get { return (int)GetValue(MaxZoomLevelProperty); }
set { SetValue(MaxZoomLevelProperty, value); }
}
/// <summary>
/// Maximum number of parallel downloads that may be performed by the TileLayer's ITileImageLoader.
/// </summary>
public int MaxParallelDownloads
{
get { return (int)GetValue(MaxParallelDownloadsProperty); }
set { SetValue(MaxParallelDownloadsProperty, value); }
}
/// <summary>
/// Optional foreground brush. Sets MapBase.Foreground, if not null.
/// </summary>
public Brush Foreground
{
get { return (Brush)GetValue(ForegroundProperty); }
set { SetValue(ForegroundProperty, value); }
}
/// <summary>
/// Optional background brush. Sets MapBase.Background, if not null.
/// New property prevents filling of RenderTransformed TileLayer with Panel.Background.
/// </summary>
public new Brush Background
{
get { return (Brush)GetValue(BackgroundProperty); }
set { SetValue(BackgroundProperty, value); }
}
2012-04-25 22:02:53 +02:00
public MapBase ParentMap
{
get { return parentMap; }
set
{
if (parentMap != null)
{
parentMap.TileGridChanged -= UpdateTiles;
ClearValue(RenderTransformProperty);
}
parentMap = value;
if (parentMap != null)
{
parentMap.TileGridChanged += UpdateTiles;
RenderTransform = parentMap.TileLayerTransform;
}
2012-04-25 22:02:53 +02:00
UpdateTiles();
}
}
2015-01-20 17:52:02 +01:00
protected virtual void UpdateTiles(bool clearTiles = false)
{
if (tiles.Count > 0)
2012-04-25 22:02:53 +02:00
{
tileImageLoader.CancelLoadTiles(this);
}
2015-01-20 17:52:02 +01:00
if (clearTiles)
{
tiles.Clear();
}
SelectTiles();
2015-01-20 17:52:02 +01:00
Children.Clear();
2012-04-25 22:02:53 +02:00
if (tiles.Count > 0)
{
foreach (var tile in tiles)
{
Children.Add(tile.Image);
}
tileImageLoader.BeginLoadTiles(this, tiles.Where(t => t.Pending));
}
2012-04-25 22:02:53 +02:00
}
private void UpdateTiles(object sender, EventArgs e)
2012-04-25 22:02:53 +02:00
{
UpdateTiles();
}
2012-04-25 22:02:53 +02:00
private void SelectTiles()
{
var newTiles = new List<Tile>();
2012-04-25 22:02:53 +02:00
if (parentMap != null && TileSource != null)
2012-04-25 22:02:53 +02:00
{
var grid = parentMap.TileGrid;
var zoomLevel = parentMap.TileZoomLevel;
var maxZoomLevel = Math.Min(zoomLevel, MaxZoomLevel);
var minZoomLevel = MinZoomLevel;
2012-04-25 22:02:53 +02:00
if (minZoomLevel < maxZoomLevel && this != parentMap.TileLayers.FirstOrDefault())
2012-04-25 22:02:53 +02:00
{
// do not load background tiles if this is not the base layer
minZoomLevel = maxZoomLevel;
}
for (var z = minZoomLevel; z <= maxZoomLevel; z++)
{
var tileSize = 1 << (zoomLevel - z);
var x1 = (int)Math.Floor((double)grid.X / tileSize); // may be negative
var x2 = (grid.X + grid.Width - 1) / tileSize;
var y1 = Math.Max(grid.Y / tileSize, 0);
var y2 = Math.Min((grid.Y + grid.Height - 1) / tileSize, (1 << z) - 1);
2012-04-25 22:02:53 +02:00
for (var y = y1; y <= y2; y++)
{
for (var x = x1; x <= x2; x++)
{
var tile = tiles.FirstOrDefault(t => t.ZoomLevel == z && t.X == x && t.Y == y);
if (tile == null)
2012-04-25 22:02:53 +02:00
{
tile = new Tile(z, x, y);
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);
if (equivalentTile != null)
{
// do not animate to avoid flicker when crossing 180°
tile.SetImage(equivalentTile.Image.Source, false);
}
2012-04-25 22:02:53 +02:00
}
newTiles.Add(tile);
}
2012-04-25 22:02:53 +02:00
}
}
}
tiles = newTiles;
2012-04-25 22:02:53 +02:00
}
protected override Size ArrangeOverride(Size finalSize)
{
if (parentMap != null)
{
foreach (var tile in tiles)
{
var tileSize = (double)(256 << (parentMap.TileZoomLevel - tile.ZoomLevel));
var x = tileSize * tile.X - 256 * parentMap.TileGrid.X;
var y = tileSize * tile.Y - 256 * parentMap.TileGrid.Y;
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
}