2012-11-22 21:42:29 +01:00
|
|
|
|
// 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;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using System.Linq;
|
2015-08-09 20:04:44 +02:00
|
|
|
|
#if NETFX_CORE
|
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.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;
|
2015-08-09 20:04:44 +02:00
|
|
|
|
using System.Windows.Threading;
|
|
|
|
|
|
using System.Diagnostics;
|
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>
|
2012-07-03 18:03:56 +02:00
|
|
|
|
/// Fills a rectangular area with map tiles from a TileSource.
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// </summary>
|
2015-08-09 20:04:44 +02:00
|
|
|
|
#if NETFX_CORE
|
2012-11-22 21:42:29 +01:00
|
|
|
|
[ContentProperty(Name = "TileSource")]
|
|
|
|
|
|
#else
|
2012-04-25 22:02:53 +02:00
|
|
|
|
[ContentProperty("TileSource")]
|
2012-11-22 21:42:29 +01:00
|
|
|
|
#endif
|
2014-11-19 21:11:14 +01:00
|
|
|
|
public partial class TileLayer : PanelBase, IMapElement
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2015-08-09 20:04:44 +02:00
|
|
|
|
public static TileLayer OpenStreetMapTileLayer
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
return new TileLayer
|
|
|
|
|
|
{
|
|
|
|
|
|
SourceName = "OpenStreetMap",
|
2014-11-19 21:11:14 +01:00
|
|
|
|
Description = "© [OpenStreetMap Contributors](http://www.openstreetmap.org/copyright)",
|
2014-10-19 21:50:23 +02:00
|
|
|
|
TileSource = new TileSource { UriFormat = "http://{c}.tile.openstreetmap.org/{z}/{x}/{y}.png" }
|
2012-11-22 21:42:29 +01:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
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)));
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
2015-03-15 10:11:19 +01:00
|
|
|
|
public static readonly DependencyProperty LogoImageProperty = DependencyProperty.Register(
|
|
|
|
|
|
"LogoImage", typeof(ImageSource), typeof(TileLayer), new PropertyMetadata(null));
|
|
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
public static readonly DependencyProperty ZoomLevelOffsetProperty = DependencyProperty.Register(
|
|
|
|
|
|
"ZoomLevelOffset", typeof(double), typeof(TileLayer),
|
|
|
|
|
|
new PropertyMetadata(0d, (o, e) => ((TileLayer)o).UpdateTileRect()));
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
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));
|
|
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
public static readonly DependencyProperty UpdateIntervalProperty = DependencyProperty.Register(
|
|
|
|
|
|
"UpdateInterval", typeof(TimeSpan), typeof(TileLayer),
|
|
|
|
|
|
new PropertyMetadata(TimeSpan.FromSeconds(0.5), (o, e) => ((TileLayer)o).updateTimer.Interval = (TimeSpan)e.NewValue));
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty UpdateWhileViewportChangingProperty = DependencyProperty.Register(
|
|
|
|
|
|
"UpdateWhileViewportChanging", typeof(bool), typeof(TileLayer), new PropertyMetadata(true));
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
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));
|
|
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
private readonly DispatcherTimer updateTimer;
|
2014-11-19 21:11:14 +01:00
|
|
|
|
private MapBase parentMap;
|
2015-08-09 20:04:44 +02:00
|
|
|
|
private double mapOriginX;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
public TileLayer()
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public TileLayer(ITileImageLoader tileImageLoader)
|
|
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
Initialize();
|
2015-08-09 20:04:44 +02:00
|
|
|
|
|
|
|
|
|
|
RenderTransform = new MatrixTransform();
|
|
|
|
|
|
TileImageLoader = tileImageLoader;
|
|
|
|
|
|
Tiles = new List<Tile>();
|
|
|
|
|
|
TileZoomLevel = -1;
|
|
|
|
|
|
|
|
|
|
|
|
updateTimer = new DispatcherTimer { Interval = UpdateInterval };
|
|
|
|
|
|
updateTimer.Tick += (s, e) => UpdateTileRect();
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
partial void Initialize(); // Windows Runtime and Silverlight only
|
|
|
|
|
|
|
|
|
|
|
|
public ITileImageLoader TileImageLoader { get; private set; }
|
|
|
|
|
|
public ICollection<Tile> Tiles { get; private set; }
|
|
|
|
|
|
public Int32Rect TileRect { get; private set; }
|
|
|
|
|
|
public int TileZoomLevel { get; private set; }
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2015-03-15 10:11:19 +01:00
|
|
|
|
/// Provides map tile URIs or images.
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public TileSource TileSource
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (TileSource)GetValue(TileSourceProperty); }
|
|
|
|
|
|
set { SetValue(TileSourceProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2015-03-15 10:11:19 +01:00
|
|
|
|
/// Name of the TileSource. Used as key in a TileLayerCollection and as component of a tile cache key.
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string SourceName
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (string)GetValue(SourceNameProperty); }
|
|
|
|
|
|
set { SetValue(SourceNameProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2015-03-15 10:11:19 +01:00
|
|
|
|
/// Description of the TileLayer. Used to display copyright information on top of the map.
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string Description
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (string)GetValue(DescriptionProperty); }
|
|
|
|
|
|
set { SetValue(DescriptionProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-03-15 10:11:19 +01:00
|
|
|
|
/// <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); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Adds an offset to the Map's ZoomLevel for a relative scale between the Map and the TileLayer.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public double ZoomLevelOffset
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (double)GetValue(ZoomLevelOffsetProperty); }
|
|
|
|
|
|
set { SetValue(ZoomLevelOffsetProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// <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); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Minimum time interval between tile updates.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public TimeSpan UpdateInterval
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (TimeSpan)GetValue(UpdateIntervalProperty); }
|
|
|
|
|
|
set { SetValue(UpdateIntervalProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Controls if tiles are updates while the viewport is still changing.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool UpdateWhileViewportChanging
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (bool)GetValue(UpdateWhileViewportChangingProperty); }
|
|
|
|
|
|
set { SetValue(UpdateWhileViewportChangingProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// <summary>
|
2015-03-15 10:11:19 +01:00
|
|
|
|
/// Optional foreground brush. Sets MapBase.Foreground, if not null.
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Brush Foreground
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (Brush)GetValue(ForegroundProperty); }
|
|
|
|
|
|
set { SetValue(ForegroundProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2015-03-15 10:11:19 +01:00
|
|
|
|
/// Optional background brush. Sets MapBase.Background, if not null.
|
2014-11-19 21:11:14 +01:00
|
|
|
|
/// 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
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
public MapBase ParentMap
|
2014-06-11 23:03:13 +02:00
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
get { return parentMap; }
|
2014-06-11 23:03:13 +02:00
|
|
|
|
set
|
|
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
if (parentMap != null)
|
2014-06-11 23:03:13 +02:00
|
|
|
|
{
|
2015-08-09 20:04:44 +02:00
|
|
|
|
parentMap.ViewportChanged -= ViewportChanged;
|
|
|
|
|
|
TileZoomLevel = -1;
|
|
|
|
|
|
UpdateTiles(true);
|
2014-06-11 23:03:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
parentMap = value;
|
2014-10-19 21:50:23 +02:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
if (parentMap != null)
|
|
|
|
|
|
{
|
2015-08-09 20:04:44 +02:00
|
|
|
|
parentMap.ViewportChanged += ViewportChanged;
|
|
|
|
|
|
ViewportChanged(this, EventArgs.Empty);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ViewportChanged(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (TileZoomLevel < 0 || Math.Abs(parentMap.MapOrigin.X - mapOriginX) > 180d)
|
|
|
|
|
|
{
|
|
|
|
|
|
// immediately handle map origin leap when map center moves across 180° longitude
|
|
|
|
|
|
UpdateTileRect();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
SetRenderTransform();
|
|
|
|
|
|
|
|
|
|
|
|
if (!UpdateWhileViewportChanging)
|
|
|
|
|
|
{
|
|
|
|
|
|
updateTimer.Stop();
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
updateTimer.Start();
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
2015-08-09 20:04:44 +02:00
|
|
|
|
|
|
|
|
|
|
mapOriginX = parentMap.MapOrigin.X;
|
2014-10-19 21:50:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
protected void UpdateTileRect()
|
2014-10-19 21:50:23 +02:00
|
|
|
|
{
|
2015-08-09 20:04:44 +02:00
|
|
|
|
updateTimer.Stop();
|
|
|
|
|
|
|
|
|
|
|
|
if (parentMap != null)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2015-08-09 20:04:44 +02:00
|
|
|
|
var zoomLevel = (int)Math.Round(parentMap.ZoomLevel + ZoomLevelOffset);
|
|
|
|
|
|
var transform = GetTileIndexMatrix(zoomLevel);
|
|
|
|
|
|
|
|
|
|
|
|
// tile indices of visible rectangle
|
|
|
|
|
|
var p1 = transform.Transform(new Point(0d, 0d));
|
|
|
|
|
|
var p2 = transform.Transform(new Point(parentMap.RenderSize.Width, 0d));
|
|
|
|
|
|
var p3 = transform.Transform(new Point(0d, parentMap.RenderSize.Height));
|
|
|
|
|
|
var p4 = transform.Transform(new Point(parentMap.RenderSize.Width, parentMap.RenderSize.Height));
|
|
|
|
|
|
|
|
|
|
|
|
// index ranges of visible tiles
|
|
|
|
|
|
var x1 = (int)Math.Floor(Math.Min(p1.X, Math.Min(p2.X, Math.Min(p3.X, p4.X))));
|
|
|
|
|
|
var y1 = (int)Math.Floor(Math.Min(p1.Y, Math.Min(p2.Y, Math.Min(p3.Y, p4.Y))));
|
|
|
|
|
|
var x2 = (int)Math.Floor(Math.Max(p1.X, Math.Max(p2.X, Math.Max(p3.X, p4.X))));
|
|
|
|
|
|
var y2 = (int)Math.Floor(Math.Max(p1.Y, Math.Max(p2.Y, Math.Max(p3.Y, p4.Y))));
|
|
|
|
|
|
var rect = new Int32Rect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
|
|
|
|
|
|
|
|
|
|
|
|
if (TileZoomLevel != zoomLevel || TileRect != rect)
|
|
|
|
|
|
{
|
|
|
|
|
|
TileZoomLevel = zoomLevel;
|
|
|
|
|
|
TileRect = rect;
|
|
|
|
|
|
|
|
|
|
|
|
SetRenderTransform();
|
|
|
|
|
|
UpdateTiles(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void UpdateTiles(bool clearTiles)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Tiles.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
TileImageLoader.CancelLoadTiles(this);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
2014-10-19 21:50:23 +02:00
|
|
|
|
|
2015-01-20 17:52:02 +01:00
|
|
|
|
if (clearTiles)
|
|
|
|
|
|
{
|
2015-08-09 20:04:44 +02:00
|
|
|
|
Tiles.Clear();
|
2015-01-20 17:52:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
SelectTiles();
|
2015-01-20 17:52:02 +01:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
Children.Clear();
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
if (Tiles.Count > 0)
|
2014-11-19 21:11:14 +01:00
|
|
|
|
{
|
2015-08-09 20:04:44 +02:00
|
|
|
|
foreach (var tile in Tiles)
|
2014-10-19 21:50:23 +02:00
|
|
|
|
{
|
|
|
|
|
|
Children.Add(tile.Image);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
TileImageLoader.BeginLoadTiles(this, Tiles.Where(t => t.Pending).OrderByDescending(t => t.ZoomLevel));
|
2014-10-19 21:50:23 +02:00
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
protected void SelectTiles()
|
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
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
if (TileZoomLevel >= 0 && parentMap != null && TileSource != null)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2015-08-09 20:04:44 +02:00
|
|
|
|
var maxZoomLevel = Math.Min(TileZoomLevel, MaxZoomLevel);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
var minZoomLevel = MinZoomLevel;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2015-01-26 19:45:05 +01:00
|
|
|
|
if (minZoomLevel < maxZoomLevel && this != parentMap.TileLayers.FirstOrDefault())
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
// do not load background tiles if this is not the base layer
|
2015-01-26 19:45:05 +01:00
|
|
|
|
minZoomLevel = maxZoomLevel;
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
2012-11-06 19:49:29 +01:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
for (var z = minZoomLevel; z <= maxZoomLevel; z++)
|
|
|
|
|
|
{
|
2015-08-09 20:04:44 +02:00
|
|
|
|
var tileSize = 1 << (TileZoomLevel - z);
|
|
|
|
|
|
var x1 = (int)Math.Floor((double)TileRect.X / tileSize); // may be negative
|
|
|
|
|
|
var x2 = (TileRect.X + TileRect.Width - 1) / tileSize;
|
|
|
|
|
|
var y1 = Math.Max(TileRect.Y / tileSize, 0);
|
|
|
|
|
|
var y2 = Math.Min((TileRect.Y + TileRect.Height - 1) / tileSize, (1 << z) - 1);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
for (var y = y1; y <= y2; y++)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (var x = x1; x <= x2; x++)
|
|
|
|
|
|
{
|
2015-08-09 20:04:44 +02:00
|
|
|
|
var tile = Tiles.FirstOrDefault(t => t.ZoomLevel == z && t.X == x && t.Y == y);
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
if (tile == null)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
tile = new Tile(z, x, y);
|
|
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
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);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2012-11-06 19:49:29 +01:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
newTiles.Add(tile);
|
|
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-08-09 20:04:44 +02:00
|
|
|
|
Tiles = newTiles;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
2014-07-01 18:57:44 +02:00
|
|
|
|
|
|
|
|
|
|
protected override Size ArrangeOverride(Size finalSize)
|
|
|
|
|
|
{
|
2015-08-09 20:04:44 +02:00
|
|
|
|
if (TileZoomLevel >= 0)
|
2014-07-01 18:57:44 +02:00
|
|
|
|
{
|
2015-08-09 20:04:44 +02:00
|
|
|
|
foreach (var tile in Tiles)
|
2014-11-19 21:11:14 +01:00
|
|
|
|
{
|
2015-08-09 20:04:44 +02:00
|
|
|
|
var tileSize = (double)(256 << (TileZoomLevel - tile.ZoomLevel));
|
|
|
|
|
|
var x = tileSize * tile.X - 256 * TileRect.X;
|
|
|
|
|
|
var y = tileSize * tile.Y - 256 * TileRect.Y;
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
|
|
|
|
|
tile.Image.Width = tileSize;
|
|
|
|
|
|
tile.Image.Height = tileSize;
|
|
|
|
|
|
tile.Image.Arrange(new Rect(x, y, tileSize, tileSize));
|
|
|
|
|
|
}
|
2014-07-01 18:57:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return finalSize;
|
|
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
2012-06-24 23:42:11 +02:00
|
|
|
|
}
|