mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-05 14:37:01 +00:00
Version 4.12.2 Dropped Windows.Web.Http.HtpClient
This commit is contained in:
parent
014f57ee1a
commit
9c55597a16
10 changed files with 199 additions and 280 deletions
|
|
@ -3,14 +3,17 @@
|
|||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
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
|
||||
|
|
@ -36,7 +39,26 @@ namespace MapControl
|
|||
}
|
||||
else if (uri.Scheme == "http" || uri.Scheme == "https")
|
||||
{
|
||||
image = await LoadHttpImageAsync(uri);
|
||||
using (var response = await HttpClient.GetAsync(uri))
|
||||
{
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
if (ImageAvailable(response.Headers))
|
||||
{
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
await response.Content.CopyToAsync(stream);
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
image = await LoadImageAsync(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -51,23 +73,50 @@ namespace MapControl
|
|||
return image;
|
||||
}
|
||||
|
||||
private static async Task<ImageSource> LoadHttpImageAsync(Uri uri)
|
||||
internal class ImageStream : MemoryStream
|
||||
{
|
||||
ImageSource image = null;
|
||||
public TimeSpan? MaxAge { get; set; }
|
||||
}
|
||||
|
||||
using (var response = await HttpClient.GetAsync(uri))
|
||||
internal static async Task<ImageStream> LoadImageStreamAsync(Uri uri)
|
||||
{
|
||||
ImageStream stream = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (!response.IsSuccessStatusCode)
|
||||
using (var response = await HttpClient.GetAsync(uri).ConfigureAwait(false))
|
||||
{
|
||||
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
||||
}
|
||||
else if (ImageAvailable(response.Headers))
|
||||
{
|
||||
image = await LoadImageAsync(response.Content);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
stream = new ImageStream();
|
||||
|
||||
if (ImageAvailable(response.Headers))
|
||||
{
|
||||
await response.Content.CopyToAsync(stream).ConfigureAwait(false);
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
stream.MaxAge = response.Headers.CacheControl?.MaxAge;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("ImageLoader: {0}: {1}", uri, ex.Message);
|
||||
}
|
||||
|
||||
return image;
|
||||
return stream;
|
||||
}
|
||||
|
||||
private static bool ImageAvailable(HttpResponseHeaders responseHeaders)
|
||||
{
|
||||
IEnumerable<string> tileInfo;
|
||||
|
||||
return !responseHeaders.TryGetValues("X-VE-Tile-Info", out tileInfo) || !tileInfo.Contains("no-tile");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -66,6 +66,9 @@ namespace MapControl
|
|||
public static readonly DependencyProperty MaxZoomLevelProperty = DependencyProperty.Register(
|
||||
nameof(MaxZoomLevel), typeof(int), typeof(MapTileLayer), new PropertyMetadata(18));
|
||||
|
||||
public static readonly DependencyProperty MaxBackgroundZoomLevelsProperty = DependencyProperty.Register(
|
||||
nameof(MaxBackgroundZoomLevels), typeof(int), typeof(MapTileLayer), new PropertyMetadata(8));
|
||||
|
||||
public static readonly DependencyProperty UpdateIntervalProperty = DependencyProperty.Register(
|
||||
nameof(UpdateInterval), typeof(TimeSpan), typeof(MapTileLayer),
|
||||
new PropertyMetadata(TimeSpan.FromSeconds(0.2), (o, e) => ((MapTileLayer)o).updateTimer.Interval = (TimeSpan)e.NewValue));
|
||||
|
|
@ -142,7 +145,7 @@ namespace MapControl
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Minimum zoom level supported by the MapTileLayer.
|
||||
/// Minimum zoom level supported by the MapTileLayer. Default value is 0.
|
||||
/// </summary>
|
||||
public int MinZoomLevel
|
||||
{
|
||||
|
|
@ -151,7 +154,7 @@ namespace MapControl
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maximum zoom level supported by the MapTileLayer.
|
||||
/// Maximum zoom level supported by the MapTileLayer. Default value is 18.
|
||||
/// </summary>
|
||||
public int MaxZoomLevel
|
||||
{
|
||||
|
|
@ -159,6 +162,16 @@ namespace MapControl
|
|||
set { SetValue(MaxZoomLevelProperty, value); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maximum number of background tile levels. Default value is 8.
|
||||
/// Applies only to a MapTileLayer that is the MapLayer of its ParentMap.
|
||||
/// </summary>
|
||||
public int MaxBackgroundZoomLevels
|
||||
{
|
||||
get { return (int)GetValue(MaxBackgroundZoomLevelsProperty); }
|
||||
set { SetValue(MaxBackgroundZoomLevelsProperty, value); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Minimum time interval between tile updates.
|
||||
/// </summary>
|
||||
|
|
@ -342,41 +355,45 @@ namespace MapControl
|
|||
if (parentMap != null && TileGrid != null && TileSource != null)
|
||||
{
|
||||
var maxZoomLevel = Math.Min(TileGrid.ZoomLevel, MaxZoomLevel);
|
||||
var minZoomLevel = MinZoomLevel;
|
||||
|
||||
if (minZoomLevel < maxZoomLevel && parentMap.MapLayer != this)
|
||||
if (maxZoomLevel >= MinZoomLevel)
|
||||
{
|
||||
minZoomLevel = maxZoomLevel; // do not load lower level tiles if this is note a "base" layer
|
||||
}
|
||||
var minZoomLevel = maxZoomLevel;
|
||||
|
||||
for (var z = minZoomLevel; z <= maxZoomLevel; z++)
|
||||
{
|
||||
var tileSize = 1 << (TileGrid.ZoomLevel - z);
|
||||
var x1 = (int)Math.Floor((double)TileGrid.XMin / tileSize); // may be negative
|
||||
var x2 = TileGrid.XMax / tileSize;
|
||||
var y1 = Math.Max(TileGrid.YMin / tileSize, 0);
|
||||
var y2 = Math.Min(TileGrid.YMax / tileSize, (1 << z) - 1);
|
||||
|
||||
for (var y = y1; y <= y2; y++)
|
||||
if (this == parentMap.MapLayer) // load background tiles
|
||||
{
|
||||
for (var x = x1; x <= x2; x++)
|
||||
minZoomLevel = Math.Max(TileGrid.ZoomLevel - MaxBackgroundZoomLevels, MinZoomLevel);
|
||||
}
|
||||
|
||||
for (var z = minZoomLevel; z <= maxZoomLevel; z++)
|
||||
{
|
||||
var tileSize = 1 << (TileGrid.ZoomLevel - z);
|
||||
var x1 = (int)Math.Floor((double)TileGrid.XMin / tileSize); // may be negative
|
||||
var x2 = TileGrid.XMax / tileSize;
|
||||
var y1 = Math.Max(TileGrid.YMin / tileSize, 0);
|
||||
var y2 = Math.Min(TileGrid.YMax / tileSize, (1 << z) - 1);
|
||||
|
||||
for (var y = y1; y <= y2; y++)
|
||||
{
|
||||
var tile = Tiles.FirstOrDefault(t => t.ZoomLevel == z && t.X == x && t.Y == y);
|
||||
|
||||
if (tile == null)
|
||||
for (var x = x1; x <= x2; x++)
|
||||
{
|
||||
tile = new Tile(z, x, y);
|
||||
var tile = Tiles.FirstOrDefault(t => t.ZoomLevel == z && t.X == x && t.Y == y);
|
||||
|
||||
var equivalentTile = Tiles.FirstOrDefault(
|
||||
t => t.ZoomLevel == z && t.XIndex == tile.XIndex && t.Y == y && t.Image.Source != null);
|
||||
|
||||
if (equivalentTile != null)
|
||||
if (tile == null)
|
||||
{
|
||||
tile.SetImage(equivalentTile.Image.Source, false); // no fade-in animation
|
||||
}
|
||||
}
|
||||
tile = new Tile(z, x, y);
|
||||
|
||||
newTiles.Add(tile);
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
newTiles.Add(tile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,9 +3,11 @@
|
|||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
|
@ -45,13 +47,27 @@ namespace MapControl
|
|||
public static string CacheKeyFormat { get; set; } = "{0}/{1}/{2}/{3}{4}";
|
||||
|
||||
|
||||
public class TileQueue : ConcurrentStack<Tile>
|
||||
{
|
||||
public void Enqueue(IEnumerable<Tile> tiles)
|
||||
{
|
||||
PushRange(tiles.Reverse().ToArray());
|
||||
}
|
||||
|
||||
public bool TryDequeue(out Tile tile)
|
||||
{
|
||||
return TryPop(out tile);
|
||||
}
|
||||
}
|
||||
|
||||
private readonly TileQueue tileQueue = new TileQueue();
|
||||
private Func<Tile, Task> loadTileImage;
|
||||
private int taskCount;
|
||||
|
||||
/// <summary>
|
||||
/// Loads all pending tiles from the tiles collection in up to MaxLoadTasks parallel Tasks.
|
||||
/// Loads all pending tiles from the tiles collection.
|
||||
/// If tileSource.UriFormat starts with "http" and sourceName is a non-empty string,
|
||||
/// tile images will be cached in the TileImageLoader's Cache.
|
||||
/// tile images will be cached in the TileImageLoader's Cache (if it's not null).
|
||||
/// </summary>
|
||||
public void LoadTilesAsync(IEnumerable<Tile> tiles, TileSource tileSource, string sourceName)
|
||||
{
|
||||
|
|
@ -59,31 +75,41 @@ namespace MapControl
|
|||
|
||||
if (tileSource != null)
|
||||
{
|
||||
tileQueue.Enqueue(tiles);
|
||||
SetLoadTileImageFunction(tileSource, sourceName);
|
||||
|
||||
var newTasks = Math.Min(tileQueue.Count, MaxLoadTasks) - taskCount;
|
||||
tiles = tiles.Where(tile => tile.Pending);
|
||||
|
||||
if (newTasks > 0)
|
||||
if (tiles.Any())
|
||||
{
|
||||
Interlocked.Add(ref taskCount, newTasks);
|
||||
tileQueue.Enqueue(tiles);
|
||||
|
||||
while (--newTasks >= 0)
|
||||
var newTasks = Math.Min(tileQueue.Count, MaxLoadTasks) - taskCount;
|
||||
|
||||
if (newTasks > 0)
|
||||
{
|
||||
Task.Run(() => LoadTilesFromQueueAsync(tileSource, sourceName));
|
||||
var loadTasks = Enumerable.Range(0, newTasks).Select(n => LoadTilesFromQueueAsync());
|
||||
|
||||
Interlocked.Add(ref taskCount, newTasks);
|
||||
|
||||
Task.WhenAll(loadTasks); // not awaited
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task LoadTilesFromQueueAsync(TileSource tileSource, string sourceName)
|
||||
private async Task LoadTilesFromQueueAsync()
|
||||
{
|
||||
Tile tile;
|
||||
|
||||
while (tileQueue.TryDequeue(out tile))
|
||||
{
|
||||
tile.Pending = false;
|
||||
|
||||
try
|
||||
{
|
||||
await LoadTileImageAsync(tile, tileSource, sourceName).ConfigureAwait(false);
|
||||
Debug.WriteLine("TileImageLoader: loading {0}/{1}/{2} in thread {3}", tile.ZoomLevel, tile.XIndex, tile.Y, Environment.CurrentManagedThreadId);
|
||||
|
||||
await loadTileImage(tile).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -94,32 +120,37 @@ namespace MapControl
|
|||
Interlocked.Decrement(ref taskCount);
|
||||
}
|
||||
|
||||
private static async Task LoadTileImageAsync(Tile tile, TileSource tileSource, string sourceName)
|
||||
private void SetLoadTileImageFunction(TileSource tileSource, string sourceName)
|
||||
{
|
||||
if (Cache != null &&
|
||||
tileSource.UriFormat != null &&
|
||||
tileSource.UriFormat.StartsWith("http") &&
|
||||
!string.IsNullOrEmpty(sourceName))
|
||||
{
|
||||
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);
|
||||
}
|
||||
loadTileImage = tile => LoadTileImageAsync(tile, tileSource, sourceName);
|
||||
}
|
||||
else
|
||||
{
|
||||
await LoadTileImageAsync(tile, tileSource).ConfigureAwait(false);
|
||||
loadTileImage = tile => LoadTileImageAsync(tile, tileSource);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task LoadTileImageAsync(Tile tile, TileSource tileSource, string sourceName)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,35 +0,0 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// © 2019 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public class TileQueue : ConcurrentStack<Tile>
|
||||
{
|
||||
public void Enqueue(IEnumerable<Tile> tiles)
|
||||
{
|
||||
tiles = tiles.Where(tile => tile.Pending);
|
||||
|
||||
if (tiles.Any())
|
||||
{
|
||||
PushRange(tiles.Reverse().ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryDequeue(out Tile tile)
|
||||
{
|
||||
var success = TryPop(out tile);
|
||||
|
||||
if (success)
|
||||
{
|
||||
tile.Pending = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue