XAML-Map-Control/MBTiles/Shared/MBTileSource.cs

68 lines
1.6 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2021-01-13 21:19:27 +01:00
// © 2021 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
2021-11-10 22:31:06 +01:00
using System.Diagnostics;
using System.Threading.Tasks;
2021-10-17 12:46:05 +02:00
#if WINUI
using Microsoft.UI.Xaml.Media;
#elif WINDOWS_UWP
using Windows.UI.Xaml.Media;
#else
using System.Windows.Media;
#endif
namespace MapControl.MBTiles
{
2019-11-17 00:10:08 +01:00
public class MBTileSource : TileSource, IDisposable
{
public MBTileData TileData { get; }
2019-11-14 23:21:08 +01:00
public MBTileSource(MBTileData tiledata)
{
2021-11-10 22:31:06 +01:00
var format = tiledata.Metadata["format"];
if (format == "png" || format == "jpg")
{
TileData = tiledata;
}
else
{
Debug.WriteLine($"MBTileSource: unsupported format '{format}'");
}
}
public void Dispose()
{
2019-11-17 00:10:08 +01:00
Dispose(true);
2021-11-10 22:31:06 +01:00
GC.SuppressFinalize(this);
2019-11-17 00:10:08 +01:00
}
protected virtual void Dispose(bool disposing)
{
2021-11-10 22:31:06 +01:00
if (disposing && TileData != null)
2019-11-17 00:10:08 +01:00
{
TileData.Dispose();
}
}
public override async Task<ImageSource> LoadImageAsync(int x, int y, int zoomLevel)
{
2021-11-10 22:31:06 +01:00
ImageSource image = null;
if (TileData != null)
{
var buffer = await TileData.ReadImageBufferAsync(x, y, zoomLevel);
if (buffer != null)
{
image = await ImageLoader.LoadImageAsync(buffer);
}
}
2021-11-10 22:31:06 +01:00
return image;
}
}
}