mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-04 14:08:32 +00:00
Added WorldMercatorProjection, fixed bounding box problems when changing projection.
This commit is contained in:
parent
2f123886ff
commit
156ebfe177
21 changed files with 586 additions and 469 deletions
110
MapControl/UWP/ImageLoader.UWP.cs
Normal file
110
MapControl/UWP/ImageLoader.UWP.cs
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
// 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.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Storage;
|
||||
using Windows.Storage.Streams;
|
||||
using Windows.UI.Xaml.Media;
|
||||
using Windows.UI.Xaml.Media.Imaging;
|
||||
using Windows.Web.Http;
|
||||
using Windows.Web.Http.Headers;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public static class ImageLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// The HttpClient instance used when image data is downloaded from a web resource.
|
||||
/// </summary>
|
||||
public static HttpClient HttpClient { get; set; } = new HttpClient();
|
||||
|
||||
public static async Task<ImageSource> LoadImageAsync(Uri uri, bool isTileImage)
|
||||
{
|
||||
if (!uri.IsAbsoluteUri || uri.Scheme == "file")
|
||||
{
|
||||
return await LoadLocalImageAsync(uri);
|
||||
}
|
||||
|
||||
if (uri.Scheme == "http")
|
||||
{
|
||||
return await LoadHttpImageAsync(uri, isTileImage);
|
||||
}
|
||||
|
||||
return new BitmapImage(uri);
|
||||
}
|
||||
|
||||
public static async Task<ImageSource> LoadLocalImageAsync(Uri uri)
|
||||
{
|
||||
var path = uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString;
|
||||
|
||||
if (!await Task.Run(() => File.Exists(path)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var file = await StorageFile.GetFileFromPathAsync(path);
|
||||
|
||||
using (var stream = await file.OpenReadAsync())
|
||||
{
|
||||
var bitmapImage = new BitmapImage();
|
||||
await bitmapImage.SetSourceAsync(stream);
|
||||
|
||||
return bitmapImage;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<ImageSource> LoadHttpImageAsync(Uri uri, bool isTileImage)
|
||||
{
|
||||
using (var response = await HttpClient.GetAsync(uri))
|
||||
{
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
||||
}
|
||||
else if (!isTileImage || IsTileAvailable(response.Headers))
|
||||
{
|
||||
using (var stream = new InMemoryRandomAccessStream())
|
||||
{
|
||||
await response.Content.WriteToStreamAsync(stream);
|
||||
stream.Seek(0);
|
||||
|
||||
var bitmapImage = new BitmapImage();
|
||||
await bitmapImage.SetSourceAsync(stream);
|
||||
|
||||
return bitmapImage;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<bool> LoadHttpTileImageAsync(Uri uri, Func<IBuffer, TimeSpan?, Task> tileCallback)
|
||||
{
|
||||
using (var response = await HttpClient.GetAsync(uri))
|
||||
{
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
||||
}
|
||||
else if (IsTileAvailable(response.Headers))
|
||||
{
|
||||
var buffer = await response.Content.ReadAsBufferAsync();
|
||||
|
||||
await tileCallback(buffer, response.Headers.CacheControl?.MaxAge);
|
||||
}
|
||||
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsTileAvailable(HttpResponseHeaderCollection responseHeaders)
|
||||
{
|
||||
return !responseHeaders.TryGetValue("X-VE-Tile-Info", out string tileInfo) || tileInfo != "no-tile";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -136,13 +136,15 @@
|
|||
<Compile Include="..\Shared\WmsImageLayer.cs">
|
||||
<Link>WmsImageLayer.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Shared\WorldMercatorProjection.cs">
|
||||
<Link>WorldMercatorProjection.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="Extensions.UWP.cs" />
|
||||
<Compile Include="ImageCache.UWP.cs" />
|
||||
<Compile Include="ImageFileCache.UWP.cs" />
|
||||
<Compile Include="Map.UWP.cs" />
|
||||
<Compile Include="MapBase.UWP.cs" />
|
||||
<Compile Include="MapGraticule.UWP.cs" />
|
||||
<Compile Include="MapImageLayer.UWP.cs" />
|
||||
<Compile Include="MapOverlay.UWP.cs" />
|
||||
<Compile Include="MapPanel.UWP.cs" />
|
||||
<Compile Include="MapPath.UWP.cs" />
|
||||
|
|
@ -151,7 +153,7 @@
|
|||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Tile.UWP.cs" />
|
||||
<Compile Include="TileImageLoader.UWP.cs" />
|
||||
<Compile Include="TileSource.UWP.cs" />
|
||||
<Compile Include="ImageLoader.UWP.cs" />
|
||||
<EmbeddedResource Include="Properties\MapControl.UWP.rd.xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -1,52 +0,0 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// © 2017 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using Windows.UI.Xaml.Media;
|
||||
using Windows.UI.Xaml.Media.Imaging;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public partial class MapImageLayer
|
||||
{
|
||||
protected void UpdateImage(ImageSource imageSource)
|
||||
{
|
||||
SetTopImage(imageSource);
|
||||
|
||||
var bitmapImage = imageSource as BitmapImage;
|
||||
|
||||
if (bitmapImage != null)
|
||||
{
|
||||
bitmapImage.ImageOpened += BitmapImageOpened;
|
||||
bitmapImage.ImageFailed += BitmapImageFailed;
|
||||
}
|
||||
else
|
||||
{
|
||||
SwapImages();
|
||||
}
|
||||
}
|
||||
|
||||
private void BitmapImageOpened(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var bitmapImage = (BitmapImage)sender;
|
||||
|
||||
bitmapImage.ImageOpened -= BitmapImageOpened;
|
||||
bitmapImage.ImageFailed -= BitmapImageFailed;
|
||||
|
||||
SwapImages();
|
||||
}
|
||||
|
||||
private void BitmapImageFailed(object sender, ExceptionRoutedEventArgs e)
|
||||
{
|
||||
var bitmapImage = (BitmapImage)sender;
|
||||
|
||||
bitmapImage.ImageOpened -= BitmapImageOpened;
|
||||
bitmapImage.ImageFailed -= BitmapImageFailed;
|
||||
|
||||
((Image)Children[topImageIndex]).Source = null;
|
||||
SwapImages();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -36,50 +36,30 @@ namespace MapControl
|
|||
private async Task LoadTileImageAsync(Tile tile, Uri uri, string cacheKey)
|
||||
{
|
||||
var cacheItem = await Cache.GetAsync(cacheKey);
|
||||
var buffer = cacheItem?.Buffer;
|
||||
var cacheBuffer = cacheItem?.Buffer;
|
||||
var loaded = false;
|
||||
|
||||
if (buffer == null || cacheItem.Expiration < DateTime.UtcNow)
|
||||
if (cacheBuffer == 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)
|
||||
{
|
||||
var success = false;
|
||||
|
||||
try
|
||||
{
|
||||
using (var response = await TileSource.HttpClient.GetAsync(uri))
|
||||
try
|
||||
{
|
||||
success = response.IsSuccessStatusCode;
|
||||
|
||||
if (!success)
|
||||
loaded = await ImageLoader.LoadHttpTileImageAsync(uri, async (buffer, maxAge) =>
|
||||
{
|
||||
Debug.WriteLine("TileImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
||||
}
|
||||
else if (TileSource.TileAvailable(response.Headers))
|
||||
{
|
||||
var buffer = await response.Content.ReadAsBufferAsync();
|
||||
|
||||
await SetTileImageAsync(tile, buffer); // create BitmapImage before caching
|
||||
|
||||
await Cache.SetAsync(cacheKey, buffer, GetExpiration(response));
|
||||
}
|
||||
await Cache.SetAsync(cacheKey, buffer, GetExpiration(maxAge));
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("TileImageLoader: {0}: {1}", uri, ex.Message);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("TileImageLoader: {0}: {1}", uri, ex.Message);
|
||||
}
|
||||
|
||||
return success;
|
||||
if (!loaded && cacheBuffer != null) // keep expired image if download failed
|
||||
{
|
||||
await SetTileImageAsync(tile, cacheBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SetTileImageAsync(Tile tile, IBuffer buffer)
|
||||
|
|
|
|||
|
|
@ -25,14 +25,14 @@ namespace MapControl
|
|||
/// <summary>
|
||||
/// Check HTTP response headers for tile availability, e.g. X-VE-Tile-Info=no-tile
|
||||
/// </summary>
|
||||
public static bool TileAvailable(HttpResponseHeaderCollection responseHeaders)
|
||||
public static bool IsTileAvailable(HttpResponseHeaderCollection responseHeaders)
|
||||
{
|
||||
string tileInfo;
|
||||
|
||||
return !responseHeaders.TryGetValue("X-VE-Tile-Info", out tileInfo) || tileInfo != "no-tile";
|
||||
}
|
||||
|
||||
protected async Task<ImageSource> LoadLocalImageAsync(Uri uri)
|
||||
protected static async Task<ImageSource> LoadLocalImageAsync(Uri uri)
|
||||
{
|
||||
var path = uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString;
|
||||
|
||||
|
|
@ -52,7 +52,7 @@ namespace MapControl
|
|||
}
|
||||
}
|
||||
|
||||
protected async Task<ImageSource> LoadHttpImageAsync(Uri uri)
|
||||
protected static async Task<ImageSource> LoadHttpImageAsync(Uri uri)
|
||||
{
|
||||
using (var response = await HttpClient.GetAsync(uri))
|
||||
{
|
||||
|
|
@ -60,7 +60,7 @@ namespace MapControl
|
|||
{
|
||||
Debug.WriteLine("TileSource: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
||||
}
|
||||
else if (TileAvailable(response.Headers))
|
||||
else if (IsTileAvailable(response.Headers))
|
||||
{
|
||||
using (var stream = new InMemoryRandomAccessStream())
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue