mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-05 14:37:01 +00:00
Minor tile layer updates
This commit is contained in:
parent
ea992d3d0f
commit
ddf77441f2
10 changed files with 124 additions and 86 deletions
42
MapControl/Shared/BoundingBoxTileSource.cs
Normal file
42
MapControl/Shared/BoundingBoxTileSource.cs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// © 2020 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public class BoundingBoxTileSource : TileSource
|
||||
{
|
||||
public override Uri GetUri(int x, int y, int zoomLevel)
|
||||
{
|
||||
Uri uri = null;
|
||||
|
||||
if (UriFormat != null)
|
||||
{
|
||||
var tileSize = 360d / (1 << zoomLevel); // tile width in degrees
|
||||
var west = MapProjection.Wgs84MetersPerDegree * (x * tileSize - 180d);
|
||||
var east = MapProjection.Wgs84MetersPerDegree * ((x + 1) * tileSize - 180d);
|
||||
var south = MapProjection.Wgs84MetersPerDegree * (180d - (y + 1) * tileSize);
|
||||
var north = MapProjection.Wgs84MetersPerDegree * (180d - y * tileSize);
|
||||
|
||||
if (UriFormat.Contains("{bbox}"))
|
||||
{
|
||||
uri = new Uri(UriFormat.Replace("{bbox}", string.Format(CultureInfo.InvariantCulture,
|
||||
"{0:F2},{1:F2},{2:F2},{3:F2}", west, south, east, north)));
|
||||
}
|
||||
else
|
||||
{
|
||||
uri = new Uri(UriFormat
|
||||
.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)));
|
||||
}
|
||||
}
|
||||
|
||||
return uri;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -216,7 +216,7 @@ namespace MapControl
|
|||
}
|
||||
}
|
||||
|
||||
protected virtual async Task UpdateImageAsync()
|
||||
protected async Task UpdateImageAsync()
|
||||
{
|
||||
updateTimer.Stop();
|
||||
|
||||
|
|
|
|||
|
|
@ -37,8 +37,7 @@ namespace MapControl
|
|||
{
|
||||
TileSource = new TileSource { UriFormat = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" },
|
||||
SourceName = "OpenStreetMap",
|
||||
Description = "© [OpenStreetMap Contributors](http://www.openstreetmap.org/copyright)",
|
||||
MaxZoomLevel = 19
|
||||
Description = "© [OpenStreetMap Contributors](http://www.openstreetmap.org/copyright)"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -47,7 +46,7 @@ namespace MapControl
|
|||
nameof(MinZoomLevel), typeof(int), typeof(MapTileLayer), new PropertyMetadata(0));
|
||||
|
||||
public static readonly DependencyProperty MaxZoomLevelProperty = DependencyProperty.Register(
|
||||
nameof(MaxZoomLevel), typeof(int), typeof(MapTileLayer), new PropertyMetadata(18));
|
||||
nameof(MaxZoomLevel), typeof(int), typeof(MapTileLayer), new PropertyMetadata(19));
|
||||
|
||||
public MapTileLayer()
|
||||
: this(new TileImageLoader())
|
||||
|
|
@ -73,7 +72,7 @@ namespace MapControl
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maximum zoom level supported by the MapTileLayer. Default value is 18.
|
||||
/// Maximum zoom level supported by the MapTileLayer. Default value is 19.
|
||||
/// </summary>
|
||||
public int MaxZoomLevel
|
||||
{
|
||||
|
|
@ -92,8 +91,6 @@ namespace MapControl
|
|||
|
||||
protected override void UpdateTileLayer()
|
||||
{
|
||||
UpdateTimer.Stop();
|
||||
|
||||
if (ParentMap == null || !ParentMap.MapProjection.IsWebMercator)
|
||||
{
|
||||
TileMatrix = null;
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ namespace MapControl
|
|||
|
||||
public static readonly DependencyProperty UpdateIntervalProperty = DependencyProperty.Register(
|
||||
nameof(UpdateInterval), typeof(TimeSpan), typeof(MapTileLayerBase),
|
||||
new PropertyMetadata(TimeSpan.FromSeconds(0.2), (o, e) => ((MapTileLayerBase)o).UpdateTimer.Interval = (TimeSpan)e.NewValue));
|
||||
new PropertyMetadata(TimeSpan.FromSeconds(0.2), (o, e) => ((MapTileLayerBase)o).updateTimer.Interval = (TimeSpan)e.NewValue));
|
||||
|
||||
public static readonly DependencyProperty UpdateWhileViewportChangingProperty = DependencyProperty.Register(
|
||||
nameof(UpdateWhileViewportChanging), typeof(bool), typeof(MapTileLayerBase), new PropertyMetadata(false));
|
||||
|
|
@ -50,6 +50,7 @@ namespace MapControl
|
|||
public static readonly DependencyProperty MapForegroundProperty = DependencyProperty.Register(
|
||||
nameof(MapForeground), typeof(Brush), typeof(MapTileLayerBase), new PropertyMetadata(null));
|
||||
|
||||
private readonly DispatcherTimer updateTimer;
|
||||
private MapBase parentMap;
|
||||
|
||||
protected MapTileLayerBase(ITileImageLoader tileImageLoader)
|
||||
|
|
@ -57,16 +58,19 @@ namespace MapControl
|
|||
RenderTransform = new MatrixTransform();
|
||||
TileImageLoader = tileImageLoader;
|
||||
|
||||
UpdateTimer = new DispatcherTimer { Interval = UpdateInterval };
|
||||
UpdateTimer.Tick += (s, e) => UpdateTileLayer();
|
||||
updateTimer = new DispatcherTimer { Interval = UpdateInterval };
|
||||
|
||||
updateTimer.Tick += (s, e) =>
|
||||
{
|
||||
updateTimer.Stop();
|
||||
UpdateTileLayer();
|
||||
};
|
||||
|
||||
MapPanel.InitMapElement(this);
|
||||
}
|
||||
|
||||
public ITileImageLoader TileImageLoader { get; }
|
||||
|
||||
public DispatcherTimer UpdateTimer { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Provides map tile URIs or images.
|
||||
/// </summary>
|
||||
|
|
@ -145,6 +149,8 @@ namespace MapControl
|
|||
get { return parentMap; }
|
||||
set
|
||||
{
|
||||
updateTimer.Stop();
|
||||
|
||||
if (parentMap != null)
|
||||
{
|
||||
parentMap.ViewportChanged -= OnViewportChanged;
|
||||
|
|
@ -165,20 +171,21 @@ namespace MapControl
|
|||
{
|
||||
if (Children.Count == 0 || e.ProjectionChanged || Math.Abs(e.LongitudeOffset) > 180d)
|
||||
{
|
||||
updateTimer.Stop();
|
||||
UpdateTileLayer(); // update immediately when projection has changed or center has moved across 180° longitude
|
||||
}
|
||||
else
|
||||
{
|
||||
SetRenderTransform();
|
||||
|
||||
if (UpdateTimer.IsEnabled && !UpdateWhileViewportChanging)
|
||||
if (updateTimer.IsEnabled && !UpdateWhileViewportChanging)
|
||||
{
|
||||
UpdateTimer.Stop(); // restart
|
||||
updateTimer.Stop(); // restart
|
||||
}
|
||||
|
||||
if (!UpdateTimer.IsEnabled)
|
||||
if (!updateTimer.IsEnabled)
|
||||
{
|
||||
UpdateTimer.Start();
|
||||
updateTimer.Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Threading.Tasks;
|
||||
#if WINDOWS_UWP
|
||||
using Windows.UI.Xaml.Media;
|
||||
|
|
@ -89,29 +88,4 @@ namespace MapControl
|
|||
return base.GetUri(x, (1 << zoomLevel) - 1 - y, zoomLevel);
|
||||
}
|
||||
}
|
||||
|
||||
public class BoundingBoxTileSource : TileSource
|
||||
{
|
||||
public override Uri GetUri(int x, int y, int zoomLevel)
|
||||
{
|
||||
Uri uri = null;
|
||||
|
||||
if (UriFormat != null)
|
||||
{
|
||||
var tileSize = 360d / (1 << zoomLevel); // tile width in degrees
|
||||
var west = MapProjection.Wgs84MetersPerDegree * (x * tileSize - 180d);
|
||||
var east = MapProjection.Wgs84MetersPerDegree * ((x + 1) * tileSize - 180d);
|
||||
var south = MapProjection.Wgs84MetersPerDegree * (180d - (y + 1) * tileSize);
|
||||
var north = MapProjection.Wgs84MetersPerDegree * (180d - y * tileSize);
|
||||
|
||||
uri = new Uri(UriFormat
|
||||
.Replace("{west}", west.ToString(CultureInfo.InvariantCulture))
|
||||
.Replace("{south}", south.ToString(CultureInfo.InvariantCulture))
|
||||
.Replace("{east}", east.ToString(CultureInfo.InvariantCulture))
|
||||
.Replace("{north}", north.ToString(CultureInfo.InvariantCulture)));
|
||||
}
|
||||
|
||||
return uri;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -90,8 +90,6 @@ namespace MapControl
|
|||
|
||||
protected override void UpdateTileLayer()
|
||||
{
|
||||
UpdateTimer.Stop();
|
||||
|
||||
if (ParentMap == null ||
|
||||
!TileMatrixSets.TryGetValue(ParentMap.MapProjection.CrsId, out WmtsTileMatrixSet tileMatrixSet))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -59,6 +59,9 @@
|
|||
<Compile Include="..\Shared\BoundingBox.cs">
|
||||
<Link>BoundingBox.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Shared\BoundingBoxTileSource.cs">
|
||||
<Link>BoundingBoxTileSource.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Shared\CenteredBoundingBox.cs">
|
||||
<Link>CenteredBoundingBox.cs</Link>
|
||||
</Compile>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue