mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
Update BoundingBoxTileSource.cs
This commit is contained in:
parent
1b8a89a504
commit
4a44702652
|
|
@ -4,39 +4,61 @@
|
|||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public class BoundingBoxTileSource : TileSource
|
||||
{
|
||||
public override Uri GetUri(int column, int row, int zoomLevel)
|
||||
{
|
||||
GetBounds(column, row, zoomLevel, out double west, out double south, out double east, out double north);
|
||||
|
||||
return GetUri(west, south, east, north);
|
||||
}
|
||||
|
||||
public override Task<ImageSource> LoadImageAsync(int column, int row, int zoomLevel)
|
||||
{
|
||||
GetBounds(column, row, zoomLevel, out double west, out double south, out double east, out double north);
|
||||
|
||||
return LoadImageAsync(west, south, east, north);
|
||||
}
|
||||
|
||||
protected virtual Uri GetUri(double west, double south, double east, double north)
|
||||
{
|
||||
Uri uri = null;
|
||||
|
||||
if (UriTemplate != null)
|
||||
{
|
||||
var tileSize = 360d / (1 << zoomLevel); // tile size in degrees
|
||||
var west = MapProjection.Wgs84MeterPerDegree * (column * tileSize - 180d);
|
||||
var east = MapProjection.Wgs84MeterPerDegree * ((column + 1) * tileSize - 180d);
|
||||
var south = MapProjection.Wgs84MeterPerDegree * (180d - (row + 1) * tileSize);
|
||||
var north = MapProjection.Wgs84MeterPerDegree * (180d - row * tileSize);
|
||||
var w = west.ToString("F2", CultureInfo.InvariantCulture);
|
||||
var e = east.ToString("F2", CultureInfo.InvariantCulture);
|
||||
var s = south.ToString("F2", CultureInfo.InvariantCulture);
|
||||
var n = north.ToString("F2", CultureInfo.InvariantCulture);
|
||||
|
||||
if (UriTemplate.Contains("{bbox}"))
|
||||
{
|
||||
uri = new Uri(UriTemplate.Replace("{bbox}",
|
||||
string.Format(CultureInfo.InvariantCulture, "{0:F2},{1:F2},{2:F2},{3:F2}", west, south, east, north)));
|
||||
}
|
||||
else
|
||||
{
|
||||
uri = new Uri(UriTemplate
|
||||
.Replace("{west}", west.ToString("F2", CultureInfo.InvariantCulture))
|
||||
.Replace("{south}", south.ToString("F2", CultureInfo.InvariantCulture))
|
||||
.Replace("{east}", east.ToString("F2", CultureInfo.InvariantCulture))
|
||||
.Replace("{north}", north.ToString("F2", CultureInfo.InvariantCulture)));
|
||||
}
|
||||
uri = UriTemplate.Contains("{bbox}")
|
||||
? new Uri(UriTemplate.Replace("{bbox}", $"{w},{s},{e},{n}"))
|
||||
: new Uri(UriTemplate.Replace("{west}", w).Replace("{south}", s).Replace("{east}", e).Replace("{north}", n));
|
||||
}
|
||||
|
||||
return uri;
|
||||
}
|
||||
|
||||
protected virtual Task<ImageSource> LoadImageAsync(double west, double south, double east, double north)
|
||||
{
|
||||
var uri = GetUri(west, south, east, north);
|
||||
|
||||
return uri != null ? ImageLoader.LoadImageAsync(uri) : Task.FromResult((ImageSource)null);
|
||||
}
|
||||
|
||||
protected void GetBounds(int column, int row, int zoomLevel, out double west, out double south, out double east, out double north)
|
||||
{
|
||||
var tileSize = 360d / (1 << zoomLevel); // tile size in degrees
|
||||
|
||||
west = MapProjection.Wgs84MeterPerDegree * (column * tileSize - 180d);
|
||||
east = MapProjection.Wgs84MeterPerDegree * ((column + 1) * tileSize - 180d);
|
||||
south = MapProjection.Wgs84MeterPerDegree * (180d - (row + 1) * tileSize);
|
||||
north = MapProjection.Wgs84MeterPerDegree * (180d - row * tileSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue