mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-05 14:37:01 +00:00
Version 4.12.2 Improved TileImageLoader.
This commit is contained in:
parent
4531e620ca
commit
26bf0b5005
11 changed files with 162 additions and 140 deletions
|
|
@ -7,9 +7,11 @@ using System.Diagnostics;
|
|||
using System.Threading.Tasks;
|
||||
#if WINDOWS_UWP
|
||||
using Windows.Web.Http;
|
||||
using Windows.UI.Xaml.Media;
|
||||
using Windows.UI.Xaml.Media.Imaging;
|
||||
#else
|
||||
using System.Net.Http;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
#endif
|
||||
|
||||
|
|
@ -22,9 +24,9 @@ namespace MapControl
|
|||
/// </summary>
|
||||
public static HttpClient HttpClient { get; set; } = new HttpClient();
|
||||
|
||||
public static async Task<BitmapSource> LoadImageAsync(Uri uri)
|
||||
public static async Task<ImageSource> LoadImageAsync(Uri uri)
|
||||
{
|
||||
BitmapSource image = null;
|
||||
ImageSource image = null;
|
||||
|
||||
try
|
||||
{
|
||||
|
|
@ -49,9 +51,9 @@ namespace MapControl
|
|||
return image;
|
||||
}
|
||||
|
||||
private static async Task<BitmapSource> LoadHttpImageAsync(Uri uri)
|
||||
private static async Task<ImageSource> LoadHttpImageAsync(Uri uri)
|
||||
{
|
||||
BitmapSource image = null;
|
||||
ImageSource image = null;
|
||||
|
||||
using (var response = await HttpClient.GetAsync(uri))
|
||||
{
|
||||
|
|
@ -59,7 +61,7 @@ namespace MapControl
|
|||
{
|
||||
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
||||
}
|
||||
else if (IsTileAvailable(response.Headers))
|
||||
else if (ImageAvailable(response.Headers))
|
||||
{
|
||||
image = await LoadImageAsync(response.Content);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,10 @@ namespace MapControl
|
|||
{
|
||||
public interface ITileImageLoader
|
||||
{
|
||||
void LoadTilesAsync(IEnumerable<Tile> tiles, TileSource tileSource, string sourceName);
|
||||
TileSource TileSource { get; set; }
|
||||
string SourceName { get; set; }
|
||||
|
||||
void LoadTilesAsync(IEnumerable<Tile> tiles);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -51,7 +54,8 @@ namespace MapControl
|
|||
new PropertyMetadata(null, (o, e) => ((MapTileLayer)o).TileSourcePropertyChanged()));
|
||||
|
||||
public static readonly DependencyProperty SourceNameProperty = DependencyProperty.Register(
|
||||
nameof(SourceName), typeof(string), typeof(MapTileLayer), new PropertyMetadata(null));
|
||||
nameof(SourceName), typeof(string), typeof(MapTileLayer),
|
||||
new PropertyMetadata(null, (o, e) => ((MapTileLayer)o).TileImageLoader.SourceName = (string)e.NewValue));
|
||||
|
||||
public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register(
|
||||
nameof(Description), typeof(string), typeof(MapTileLayer), new PropertyMetadata(null));
|
||||
|
|
@ -71,7 +75,7 @@ namespace MapControl
|
|||
new PropertyMetadata(TimeSpan.FromSeconds(0.2), (o, e) => ((MapTileLayer)o).updateTimer.Interval = (TimeSpan)e.NewValue));
|
||||
|
||||
public static readonly DependencyProperty UpdateWhileViewportChangingProperty = DependencyProperty.Register(
|
||||
nameof(UpdateWhileViewportChanging), typeof(bool), typeof(MapTileLayer), new PropertyMetadata(true));
|
||||
nameof(UpdateWhileViewportChanging), typeof(bool), typeof(MapTileLayer), new PropertyMetadata(false));
|
||||
|
||||
public static readonly DependencyProperty MapBackgroundProperty = DependencyProperty.Register(
|
||||
nameof(MapBackground), typeof(Brush), typeof(MapTileLayer), new PropertyMetadata(null));
|
||||
|
|
@ -99,7 +103,7 @@ namespace MapControl
|
|||
MapPanel.InitMapElement(this);
|
||||
}
|
||||
|
||||
public ITileImageLoader TileImageLoader { get; private set; }
|
||||
public ITileImageLoader TileImageLoader { get; }
|
||||
|
||||
public TileGrid TileGrid { get; private set; }
|
||||
|
||||
|
|
@ -271,6 +275,8 @@ namespace MapControl
|
|||
|
||||
private void TileSourcePropertyChanged()
|
||||
{
|
||||
TileImageLoader.TileSource = TileSource;
|
||||
|
||||
if (TileGrid != null)
|
||||
{
|
||||
Tiles = new List<Tile>();
|
||||
|
|
@ -391,7 +397,7 @@ namespace MapControl
|
|||
Children.Add(tile.Image);
|
||||
}
|
||||
|
||||
TileImageLoader.LoadTilesAsync(Tiles, TileSource, SourceName);
|
||||
TileImageLoader.LoadTilesAsync(Tiles);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,11 +44,6 @@ namespace MapControl
|
|||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("{0}/{1}/{2}", ZoomLevel, XIndex, Y);
|
||||
}
|
||||
|
||||
private void FadeIn()
|
||||
{
|
||||
Image.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation { From = 0d, To = 1d, Duration = FadeDuration, FillBehavior = FillBehavior.Stop });
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
|
@ -38,57 +39,42 @@ namespace MapControl
|
|||
public static TimeSpan DefaultCacheExpiration { get; set; } = TimeSpan.FromDays(1);
|
||||
|
||||
/// <summary>
|
||||
/// Format string for creating cache keys from the SourceName property of a TileSource,
|
||||
/// the ZoomLevel, XIndex, and Y properties of a Tile, and the image file extension.
|
||||
/// The default value is "{0};{1};{2};{3}{4}".
|
||||
/// Format string for creating cache keys from the SourceName property and the
|
||||
/// ZoomLevel, XIndex, and Y properties of a Tile and the image file extension.
|
||||
/// The default value is "{0}/{1}/{2}/{3}{4}".
|
||||
/// </summary>
|
||||
public static string CacheKeyFormat { get; set; } = "{0};{1};{2};{3}{4}";
|
||||
public static string CacheKeyFormat { get; set; } = "{0}/{1}/{2}/{3}{4}";
|
||||
|
||||
|
||||
private readonly TileQueue tileQueue = new TileQueue();
|
||||
private int taskCount;
|
||||
|
||||
public TileSource TileSource { get; set; }
|
||||
public string SourceName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Loads all pending tiles from the tiles collection in up to MaxLoadTasks parallel Tasks.
|
||||
/// If the UriFormat of the TileSource starts with "http" and the sourceName string is non-empty,
|
||||
/// tile images are cached in the TileImageLoader's Cache.
|
||||
/// If the UriFormat of TileSource starts with "http" and SourceName is a non-empty string,
|
||||
/// tile images will be cached in the TileImageLoader's Cache.
|
||||
/// </summary>
|
||||
public void LoadTilesAsync(IEnumerable<Tile> tiles, TileSource tileSource, string sourceName)
|
||||
public async void LoadTilesAsync(IEnumerable<Tile> tiles)
|
||||
{
|
||||
tileQueue.Clear();
|
||||
tileQueue.Enqueue(tiles);
|
||||
|
||||
if (tileSource != null)
|
||||
var newTasks = Math.Min(tileQueue.Count, MaxLoadTasks) - taskCount;
|
||||
|
||||
if (newTasks > 0)
|
||||
{
|
||||
tileQueue.Enqueue(tiles);
|
||||
Interlocked.Add(ref taskCount, newTasks);
|
||||
|
||||
var newTasks = Math.Min(tileQueue.Count, MaxLoadTasks) - taskCount;
|
||||
|
||||
if (newTasks > 0)
|
||||
{
|
||||
Func<Tile, Task> loadTileFunc;
|
||||
|
||||
if (Cache != null &&
|
||||
tileSource.UriFormat != null &&
|
||||
tileSource.UriFormat.StartsWith("http") &&
|
||||
!string.IsNullOrEmpty(sourceName))
|
||||
{
|
||||
loadTileFunc = tile => LoadCachedTileImageAsync(tile, tileSource, sourceName);
|
||||
}
|
||||
else
|
||||
{
|
||||
loadTileFunc = tile => LoadTileImageAsync(tile, tileSource);
|
||||
}
|
||||
|
||||
Interlocked.Add(ref taskCount, newTasks);
|
||||
|
||||
while (--newTasks >= 0)
|
||||
{
|
||||
Task.Run(() => LoadTilesFromQueueAsync(loadTileFunc));
|
||||
}
|
||||
}
|
||||
await Task
|
||||
.WhenAll(Enumerable.Range(0, newTasks).Select(n => LoadTilesFromQueueAsync()))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task LoadTilesFromQueueAsync(Func<Tile, Task> loadTileFunc)
|
||||
private async Task LoadTilesFromQueueAsync()
|
||||
{
|
||||
Tile tile;
|
||||
|
||||
|
|
@ -96,7 +82,7 @@ namespace MapControl
|
|||
{
|
||||
try
|
||||
{
|
||||
await loadTileFunc(tile);
|
||||
await LoadTileImageAsync(tile, TileSource, SourceName).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -107,22 +93,35 @@ namespace MapControl
|
|||
Interlocked.Decrement(ref taskCount);
|
||||
}
|
||||
|
||||
private async Task LoadCachedTileImageAsync(Tile tile, TileSource tileSource, string sourceName)
|
||||
private async Task LoadTileImageAsync(Tile tile, TileSource tileSource, string sourceName)
|
||||
{
|
||||
var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);
|
||||
|
||||
if (uri != null)
|
||||
if (tileSource != null)
|
||||
{
|
||||
var extension = Path.GetExtension(uri.LocalPath);
|
||||
|
||||
if (string.IsNullOrEmpty(extension) || extension == ".jpeg")
|
||||
if (Cache != null &&
|
||||
tileSource.UriFormat != null &&
|
||||
tileSource.UriFormat.StartsWith("http") &&
|
||||
!string.IsNullOrEmpty(sourceName))
|
||||
{
|
||||
extension = ".jpg";
|
||||
var uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);
|
||||
|
||||
if (uri != null)
|
||||
{
|
||||
var extension = Path.GetExtension(uri.LocalPath);
|
||||
|
||||
if (string.IsNullOrEmpty(extension) || extension == ".jpeg")
|
||||
{
|
||||
extension = ".jpg";
|
||||
}
|
||||
|
||||
var cacheKey = string.Format(CacheKeyFormat, sourceName, tile.ZoomLevel, tile.XIndex, tile.Y, extension);
|
||||
|
||||
await LoadCachedTileImageAsync(tile, uri, cacheKey).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await LoadTileImageAsync(tile, tileSource).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var cacheKey = string.Format(CacheKeyFormat, sourceName, tile.ZoomLevel, tile.XIndex, tile.Y, extension);
|
||||
|
||||
await LoadCachedTileImageAsync(tile, uri, cacheKey);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue