2013-11-12 21:14:53 +01:00
|
|
|
|
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
|
2014-01-10 20:11:39 +01:00
|
|
|
|
// Copyright © 2014 Clemens Fischer
|
2013-11-12 21:14:53 +01:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
|
|
2013-11-12 21:14:53 +01:00
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Provides the image of a map tile. ImageTileSource bypasses image downloading
|
|
|
|
|
|
/// and optional caching in TileImageLoader. By overriding the LoadImage method,
|
|
|
|
|
|
/// an application can provide tile images from an arbitrary source.
|
|
|
|
|
|
/// If the IsAsync property is true, LoadImage will be called from a separate,
|
|
|
|
|
|
/// non-UI thread and must therefore return a frozen ImageSource.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class ImageTileSource : TileSource
|
2013-11-12 21:14:53 +01:00
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
public bool IsAsync { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public virtual ImageSource LoadImage(int x, int y, int zoomLevel)
|
2013-11-12 21:14:53 +01:00
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
ImageSource image = null;
|
|
|
|
|
|
|
|
|
|
|
|
var uri = GetUri(x, y, zoomLevel);
|
|
|
|
|
|
|
|
|
|
|
|
if (uri != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (IsAsync)
|
|
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
var request = HttpWebRequest.CreateHttp(uri);
|
|
|
|
|
|
request.UserAgent = TileImageLoader.HttpUserAgent;
|
2014-07-01 18:57:44 +02:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
using (var response = (HttpWebResponse)request.GetResponse())
|
|
|
|
|
|
using (var responseStream = response.GetResponseStream())
|
|
|
|
|
|
using (var memoryStream = new MemoryStream())
|
2014-07-01 18:57:44 +02:00
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
responseStream.CopyTo(memoryStream);
|
|
|
|
|
|
memoryStream.Position = 0;
|
|
|
|
|
|
image = BitmapFrame.Create(memoryStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
|
2014-07-01 18:57:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
image = BitmapFrame.Create(uri);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return image;
|
2013-11-12 21:14:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|