2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2020-01-09 19:40:10 +01:00
|
|
|
|
// © 2020 Clemens Fischer
|
2012-05-04 12:52:20 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2017-10-08 17:35:07 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
#if WINDOWS_UWP
|
|
|
|
|
|
using Windows.UI.Xaml.Media;
|
|
|
|
|
|
#else
|
2018-08-08 23:31:52 +02:00
|
|
|
|
using System.ComponentModel;
|
2017-10-08 17:35:07 +02:00
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
#endif
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// <summary>
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// Provides the download Uri or ImageSource of map tiles.
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// </summary>
|
2018-08-08 23:31:52 +02:00
|
|
|
|
#if !WINDOWS_UWP
|
|
|
|
|
|
[TypeConverter(typeof(TileSourceConverter))]
|
|
|
|
|
|
#endif
|
|
|
|
|
|
public class TileSource
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2017-10-09 19:17:04 +02:00
|
|
|
|
private string uriFormat;
|
|
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// <summary>
|
2020-10-23 23:35:48 +02:00
|
|
|
|
/// Gets or sets the format string to produce tile request Uris.
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// </summary>
|
2012-07-20 21:57:29 +02:00
|
|
|
|
public string UriFormat
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return uriFormat; }
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
2020-10-24 10:23:00 +02:00
|
|
|
|
uriFormat = value?.Replace("{c}", "{s}"); // for backwards compatibility since 5.4.0
|
2013-01-28 23:49:22 +01:00
|
|
|
|
|
2020-10-24 10:23:00 +02:00
|
|
|
|
if (Subdomains == null && uriFormat != null && uriFormat.Contains("{s}"))
|
2017-10-09 19:17:04 +02:00
|
|
|
|
{
|
2020-10-23 23:35:48 +02:00
|
|
|
|
Subdomains = new string[] { "a", "b", "c" }; // default OpenStreetMap subdomains
|
2017-10-09 19:17:04 +02:00
|
|
|
|
}
|
2012-07-20 21:57:29 +02:00
|
|
|
|
}
|
2013-01-29 17:55:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// <summary>
|
2020-10-23 23:35:48 +02:00
|
|
|
|
/// Gets or sets an array of request subdomain names that are replaced for the {s} format specifier.
|
2017-08-04 21:38:58 +02:00
|
|
|
|
/// </summary>
|
2020-10-23 23:35:48 +02:00
|
|
|
|
public string[] Subdomains { get; set; }
|
2012-07-20 21:57:29 +02:00
|
|
|
|
|
2020-10-23 23:35:48 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the image Uri for the specified tile indices and zoom level.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public virtual Uri GetUri(int x, int y, int zoomLevel)
|
2013-04-21 23:56:08 +02:00
|
|
|
|
{
|
2020-10-23 23:35:48 +02:00
|
|
|
|
Uri uri = null;
|
2013-04-21 23:56:08 +02:00
|
|
|
|
|
2020-10-23 23:35:48 +02:00
|
|
|
|
if (UriFormat != null)
|
2013-11-17 16:52:03 +01:00
|
|
|
|
{
|
2020-10-23 23:35:48 +02:00
|
|
|
|
var uriString = UriFormat
|
|
|
|
|
|
.Replace("{x}", x.ToString())
|
|
|
|
|
|
.Replace("{y}", y.ToString())
|
|
|
|
|
|
.Replace("{z}", zoomLevel.ToString());
|
2013-11-17 16:52:03 +01:00
|
|
|
|
|
2020-10-23 23:35:48 +02:00
|
|
|
|
if (Subdomains != null && Subdomains.Length > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
uriString = uriString.Replace("{s}", Subdomains[(x + y) % Subdomains.Length]);
|
|
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2020-10-23 23:35:48 +02:00
|
|
|
|
uri = new Uri(uriString, UriKind.RelativeOrAbsolute);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-23 23:35:48 +02:00
|
|
|
|
return uri;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
2020-10-25 17:35:33 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Loads a tile ImageSource asynchronously from GetUri(x, y, zoomLevel).
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public virtual Task<ImageSource> LoadImageAsync(int x, int y, int zoomLevel)
|
|
|
|
|
|
{
|
|
|
|
|
|
var uri = GetUri(x, y, zoomLevel);
|
|
|
|
|
|
|
|
|
|
|
|
return uri != null ? ImageLoader.LoadImageAsync(uri) : Task.FromResult((ImageSource)null);
|
|
|
|
|
|
}
|
2020-10-23 23:35:48 +02:00
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2020-10-23 23:35:48 +02:00
|
|
|
|
public class TmsTileSource : TileSource
|
|
|
|
|
|
{
|
|
|
|
|
|
public override Uri GetUri(int x, int y, int zoomLevel)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2020-10-23 23:35:48 +02:00
|
|
|
|
return base.GetUri(x, (1 << zoomLevel) - 1 - y, zoomLevel);
|
2013-05-07 18:12:25 +02:00
|
|
|
|
}
|
2020-10-23 23:35:48 +02:00
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|