XAML-Map-Control/MapControl/Shared/UriTileSource.cs

54 lines
1.3 KiB
C#
Raw Normal View History

2025-12-02 15:29:45 +01:00
using System;
2026-04-13 17:14:49 +02:00
namespace MapControl;
public class UriTileSource : TileSource
2025-12-02 15:29:45 +01:00
{
2026-04-13 17:14:49 +02:00
private string uriFormat;
2025-12-02 15:29:45 +01:00
2026-04-13 17:14:49 +02:00
public string UriTemplate
{
get;
set
2025-12-02 15:29:45 +01:00
{
2026-04-13 17:14:49 +02:00
field = value;
uriFormat = field
.Replace("{z}", "{0}")
.Replace("{x}", "{1}")
.Replace("{y}", "{2}")
.Replace("{s}", "{3}");
if (Subdomains == null && field.Contains("{s}"))
2025-12-02 15:29:45 +01:00
{
2026-04-13 17:14:49 +02:00
Subdomains = ["a", "b", "c"]; // default OpenStreetMap subdomains
2025-12-02 15:29:45 +01:00
}
}
2026-04-13 17:14:49 +02:00
}
2025-12-02 15:29:45 +01:00
2026-04-13 17:14:49 +02:00
public string[] Subdomains { get; set; }
2025-12-02 15:29:45 +01:00
2026-04-13 17:14:49 +02:00
public override Uri GetUri(int zoomLevel, int column, int row)
{
Uri uri = null;
2025-12-02 15:29:45 +01:00
2026-04-13 17:14:49 +02:00
if (uriFormat != null)
{
var uriString = Subdomains?.Length > 0
? string.Format(uriFormat, zoomLevel, column, row, Subdomains[(column + row) % Subdomains.Length])
: string.Format(uriFormat, zoomLevel, column, row);
2025-12-02 15:29:45 +01:00
2026-04-13 17:14:49 +02:00
uri = new Uri(uriString, UriKind.RelativeOrAbsolute);
2025-12-02 15:29:45 +01:00
}
2026-04-13 17:14:49 +02:00
return uri;
2025-12-02 15:29:45 +01:00
}
2026-04-13 17:14:49 +02:00
}
2025-12-02 15:29:45 +01:00
2026-04-13 17:14:49 +02:00
public class TmsTileSource : UriTileSource
{
public override Uri GetUri(int zoomLevel, int column, int row)
2025-12-02 15:29:45 +01:00
{
2026-04-13 17:14:49 +02:00
return base.GetUri(zoomLevel, column, (1 << zoomLevel) - 1 - row);
2025-12-02 15:29:45 +01:00
}
}