2017-08-04 21:38:58 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
|
|
|
|
|
// © 2017 Clemens Fischer
|
|
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Windows.Storage;
|
|
|
|
|
|
using Windows.Storage.Streams;
|
|
|
|
|
|
using Windows.UI.Core;
|
|
|
|
|
|
using Windows.UI.Xaml.Media.Imaging;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
public partial class TileImageLoader : ITileImageLoader
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Default StorageFolder where an IImageCache instance may save cached data.
|
|
|
|
|
|
/// </summary>
|
2017-09-05 23:59:22 +02:00
|
|
|
|
public static StorageFolder DefaultCacheFolder { get; } = ApplicationData.Current.TemporaryFolder;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The IImageCache implementation used to cache tile images. The default is null.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static Caching.IImageCache Cache { get; set; }
|
|
|
|
|
|
|
2017-09-05 23:59:22 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the maximum number of concurrent connections.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static int DefaultConnectionLimit { get; set; } = 2;
|
|
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
private async Task LoadTileImageAsync(Tile tile, Uri uri, string cacheKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
var cacheItem = await Cache.GetAsync(cacheKey);
|
|
|
|
|
|
var buffer = cacheItem?.Buffer;
|
|
|
|
|
|
var loaded = false;
|
|
|
|
|
|
|
|
|
|
|
|
if (buffer == null || cacheItem.Expiration < DateTime.UtcNow)
|
|
|
|
|
|
{
|
|
|
|
|
|
loaded = await DownloadTileImageAsync(tile, uri, cacheKey);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!loaded && buffer != null) // keep expired image if download failed
|
|
|
|
|
|
{
|
|
|
|
|
|
await SetTileImageAsync(tile, buffer);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<bool> DownloadTileImageAsync(Tile tile, Uri uri, string cacheKey)
|
|
|
|
|
|
{
|
2017-09-05 20:57:17 +02:00
|
|
|
|
var success = false;
|
|
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2017-09-05 20:57:17 +02:00
|
|
|
|
using (var response = await TileSource.HttpClient.GetAsync(uri))
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2017-09-05 20:57:17 +02:00
|
|
|
|
success = response.IsSuccessStatusCode;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2017-09-05 20:57:17 +02:00
|
|
|
|
if (!success)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("TileImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (TileSource.TileAvailable(response.Headers))
|
|
|
|
|
|
{
|
|
|
|
|
|
var buffer = await response.Content.ReadAsBufferAsync();
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2017-09-05 20:57:17 +02:00
|
|
|
|
await SetTileImageAsync(tile, buffer); // create BitmapImage before caching
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2017-09-05 20:57:17 +02:00
|
|
|
|
await Cache.SetAsync(cacheKey, buffer, GetExpiration(response));
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("TileImageLoader: {0}: {1}", uri, ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-09-05 20:57:17 +02:00
|
|
|
|
return success;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task SetTileImageAsync(Tile tile, IBuffer buffer)
|
|
|
|
|
|
{
|
|
|
|
|
|
var tcs = new TaskCompletionSource<object>();
|
|
|
|
|
|
|
|
|
|
|
|
using (var stream = new InMemoryRandomAccessStream())
|
|
|
|
|
|
{
|
|
|
|
|
|
await stream.WriteAsync(buffer);
|
|
|
|
|
|
await stream.FlushAsync(); // necessary?
|
|
|
|
|
|
stream.Seek(0);
|
|
|
|
|
|
|
|
|
|
|
|
await tile.Image.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var bitmapImage = new BitmapImage();
|
|
|
|
|
|
await bitmapImage.SetSourceAsync(stream);
|
|
|
|
|
|
|
|
|
|
|
|
tile.SetImage(bitmapImage);
|
|
|
|
|
|
tcs.SetResult(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
tcs.SetException(ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await tcs.Task;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|