Removed default TileLayer

This commit is contained in:
ClemensF 2012-06-12 20:30:05 +02:00
parent cc690c5445
commit a0e0403c90
6 changed files with 60 additions and 61 deletions

View file

@ -42,12 +42,12 @@ namespace MapControl
public static readonly DependencyProperty TileLayersProperty = DependencyProperty.Register( public static readonly DependencyProperty TileLayersProperty = DependencyProperty.Register(
"TileLayers", typeof(TileLayerCollection), typeof(Map), new FrameworkPropertyMetadata( "TileLayers", typeof(TileLayerCollection), typeof(Map), new FrameworkPropertyMetadata(
(o, e) => ((Map)o).SetTileLayers((TileLayerCollection)e.NewValue))); (o, e) => ((Map)o).TileLayersPropertyChanged((TileLayerCollection)e.NewValue)));
public static readonly DependencyProperty MainTileLayerProperty = DependencyProperty.Register( public static readonly DependencyProperty MainTileLayerProperty = DependencyProperty.Register(
"MainTileLayer", typeof(TileLayer), typeof(Map), new FrameworkPropertyMetadata( "MainTileLayer", typeof(TileLayer), typeof(Map), new FrameworkPropertyMetadata(
(o, e) => ((Map)o).SetMainTileLayer((TileLayer)e.NewValue), (o, e) => ((Map)o).MainTileLayerPropertyChanged((TileLayer)e.NewValue),
(o, v) => ((Map)o).CoerceMainTileLayer((TileLayer)v))); (o, v) => ((Map)o).CoerceMainTileLayerProperty((TileLayer)v)));
public static readonly DependencyProperty TileOpacityProperty = DependencyProperty.Register( public static readonly DependencyProperty TileOpacityProperty = DependencyProperty.Register(
"TileOpacity", typeof(double), typeof(Map), new FrameworkPropertyMetadata(1d, "TileOpacity", typeof(double), typeof(Map), new FrameworkPropertyMetadata(1d,
@ -55,33 +55,33 @@ namespace MapControl
public static readonly DependencyProperty CenterProperty = DependencyProperty.Register( public static readonly DependencyProperty CenterProperty = DependencyProperty.Register(
"Center", typeof(Location), typeof(Map), new FrameworkPropertyMetadata(new Location(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, "Center", typeof(Location), typeof(Map), new FrameworkPropertyMetadata(new Location(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
(o, e) => ((Map)o).SetCenter((Location)e.NewValue), (o, e) => ((Map)o).CenterPropertyChanged((Location)e.NewValue),
(o, v) => ((Map)o).CoerceCenter((Location)v))); (o, v) => ((Map)o).CoerceCenterProperty((Location)v)));
public static readonly DependencyProperty TargetCenterProperty = DependencyProperty.Register( public static readonly DependencyProperty TargetCenterProperty = DependencyProperty.Register(
"TargetCenter", typeof(Location), typeof(Map), new FrameworkPropertyMetadata(new Location(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, "TargetCenter", typeof(Location), typeof(Map), new FrameworkPropertyMetadata(new Location(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
(o, e) => ((Map)o).SetTargetCenter((Location)e.NewValue), (o, e) => ((Map)o).TargetCenterPropertyChanged((Location)e.NewValue),
(o, v) => ((Map)o).CoerceCenter((Location)v))); (o, v) => ((Map)o).CoerceCenterProperty((Location)v)));
public static readonly DependencyProperty ZoomLevelProperty = DependencyProperty.Register( public static readonly DependencyProperty ZoomLevelProperty = DependencyProperty.Register(
"ZoomLevel", typeof(double), typeof(Map), new FrameworkPropertyMetadata(1d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, "ZoomLevel", typeof(double), typeof(Map), new FrameworkPropertyMetadata(1d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
(o, e) => ((Map)o).SetZoomLevel((double)e.NewValue), (o, e) => ((Map)o).ZoomLevelPropertyChanged((double)e.NewValue),
(o, v) => ((Map)o).CoerceZoomLevel((double)v))); (o, v) => ((Map)o).CoerceZoomLevelProperty((double)v)));
public static readonly DependencyProperty TargetZoomLevelProperty = DependencyProperty.Register( public static readonly DependencyProperty TargetZoomLevelProperty = DependencyProperty.Register(
"TargetZoomLevel", typeof(double), typeof(Map), new FrameworkPropertyMetadata(1d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, "TargetZoomLevel", typeof(double), typeof(Map), new FrameworkPropertyMetadata(1d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
(o, e) => ((Map)o).SetTargetZoomLevel((double)e.NewValue), (o, e) => ((Map)o).TargetZoomLevelPropertyChanged((double)e.NewValue),
(o, v) => ((Map)o).CoerceZoomLevel((double)v))); (o, v) => ((Map)o).CoerceZoomLevelProperty((double)v)));
public static readonly DependencyProperty HeadingProperty = DependencyProperty.Register( public static readonly DependencyProperty HeadingProperty = DependencyProperty.Register(
"Heading", typeof(double), typeof(Map), new FrameworkPropertyMetadata(0d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, "Heading", typeof(double), typeof(Map), new FrameworkPropertyMetadata(0d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
(o, e) => ((Map)o).SetHeading((double)e.NewValue), (o, e) => ((Map)o).HeadingPropertyChanged((double)e.NewValue),
(o, v) => ((Map)o).CoerceHeading((double)v))); (o, v) => ((Map)o).CoerceHeadingProperty((double)v)));
public static readonly DependencyProperty TargetHeadingProperty = DependencyProperty.Register( public static readonly DependencyProperty TargetHeadingProperty = DependencyProperty.Register(
"TargetHeading", typeof(double), typeof(Map), new FrameworkPropertyMetadata(0d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, "TargetHeading", typeof(double), typeof(Map), new FrameworkPropertyMetadata(0d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
(o, e) => ((Map)o).SetTargetHeading((double)e.NewValue), (o, e) => ((Map)o).TargetHeadingPropertyChanged((double)e.NewValue),
(o, v) => ((Map)o).CoerceHeading((double)v))); (o, v) => ((Map)o).CoerceHeadingProperty((double)v)));
private static readonly DependencyPropertyKey CenterScalePropertyKey = DependencyProperty.RegisterReadOnly( private static readonly DependencyPropertyKey CenterScalePropertyKey = DependencyProperty.RegisterReadOnly(
"CenterScale", typeof(double), typeof(Map), null); "CenterScale", typeof(double), typeof(Map), null);
@ -104,15 +104,7 @@ namespace MapControl
{ {
MinZoomLevel = 1; MinZoomLevel = 1;
MaxZoomLevel = 20; MaxZoomLevel = 20;
AddVisualChild(tileContainer); AddVisualChild(tileContainer);
MainTileLayer = new TileLayer
{
Description = "© {y} OpenStreetMap Contributors, CC-BY-SA",
TileSource = new OpenStreetMapTileSource { UriFormat = "http://{c}.tile.openstreetmap.org/{z}/{x}/{y}.png" }
};
SetValue(ParentMapProperty, this); SetValue(ParentMapProperty, this);
} }
@ -354,7 +346,7 @@ namespace MapControl
{ {
viewportOrigin.X = Math.Min(Math.Max(origin.X, 0d), RenderSize.Width); viewportOrigin.X = Math.Min(Math.Max(origin.X, 0d), RenderSize.Width);
viewportOrigin.Y = Math.Min(Math.Max(origin.Y, 0d), RenderSize.Height); viewportOrigin.Y = Math.Min(Math.Max(origin.Y, 0d), RenderSize.Height);
transformOrigin = CoerceCenter(ViewportPointToLocation(viewportOrigin)); transformOrigin = CoerceCenterProperty(ViewportPointToLocation(viewportOrigin));
} }
/// <summary> /// <summary>
@ -392,7 +384,7 @@ namespace MapControl
Heading += rotation; Heading += rotation;
ZoomLevel += Math.Log(scale, 2d); ZoomLevel += Math.Log(scale, 2d);
updateTransform = true; updateTransform = true;
SetViewTransform(); UpdateViewTransform();
} }
TranslateMap(translation); TranslateMap(translation);
@ -436,7 +428,7 @@ namespace MapControl
base.OnRenderSizeChanged(sizeInfo); base.OnRenderSizeChanged(sizeInfo);
ResetTransformOrigin(); ResetTransformOrigin();
SetViewTransform(); UpdateViewTransform();
} }
protected override void OnRender(DrawingContext drawingContext) protected override void OnRender(DrawingContext drawingContext)
@ -489,47 +481,47 @@ namespace MapControl
} }
} }
private void SetTileLayers(TileLayerCollection tileLayers) private void TileLayersPropertyChanged(TileLayerCollection tileLayers)
{ {
MainTileLayer = tileLayers.Count > 0 ? tileLayers[0] : null;
tileContainer.TileLayers = tileLayers; tileContainer.TileLayers = tileLayers;
MainTileLayer = tileLayers.Count > 0 ? tileLayers[0] : null;
} }
private void SetMainTileLayer(TileLayer mainTileLayer) private void MainTileLayerPropertyChanged(TileLayer mainTileLayer)
{ {
if (mainTileLayer != null) if (mainTileLayer != null)
{ {
if (TileLayers == null) if (tileContainer.TileLayers == null)
{ {
TileLayers = new TileLayerCollection(mainTileLayer); TileLayers = new TileLayerCollection(mainTileLayer);
} }
else if (TileLayers.Count == 0) else if (tileContainer.TileLayers.Count == 0)
{ {
TileLayers.Add(mainTileLayer); tileContainer.TileLayers.Add(mainTileLayer);
} }
else if (TileLayers[0] != mainTileLayer) else if (tileContainer.TileLayers[0] != mainTileLayer)
{ {
TileLayers[0] = mainTileLayer; tileContainer.TileLayers[0] = mainTileLayer;
} }
} }
} }
private TileLayer CoerceMainTileLayer(TileLayer mainTileLayer) private TileLayer CoerceMainTileLayerProperty(TileLayer mainTileLayer)
{ {
if (mainTileLayer == null && TileLayers.Count > 0) if (mainTileLayer == null && tileContainer.TileLayers != null && tileContainer.TileLayers.Count > 0)
{ {
mainTileLayer = TileLayers[0]; mainTileLayer = tileContainer.TileLayers[0];
} }
return mainTileLayer; return mainTileLayer;
} }
private void SetCenter(Location center) private void CenterPropertyChanged(Location center)
{ {
if (updateTransform) if (updateTransform)
{ {
ResetTransformOrigin(); ResetTransformOrigin();
SetViewTransform(); UpdateViewTransform();
} }
if (centerAnimation == null) if (centerAnimation == null)
@ -538,7 +530,7 @@ namespace MapControl
} }
} }
private void SetTargetCenter(Location targetCenter) private void TargetCenterPropertyChanged(Location targetCenter)
{ {
if (targetCenter != Center) if (targetCenter != Center)
{ {
@ -571,18 +563,18 @@ namespace MapControl
centerAnimation = null; centerAnimation = null;
} }
private Location CoerceCenter(Location location) private Location CoerceCenterProperty(Location location)
{ {
location.Latitude = Math.Min(Math.Max(location.Latitude, -MapTransform.MaxLatitude), MapTransform.MaxLatitude); location.Latitude = Math.Min(Math.Max(location.Latitude, -MapTransform.MaxLatitude), MapTransform.MaxLatitude);
location.Longitude = Location.NormalizeLongitude(location.Longitude); location.Longitude = Location.NormalizeLongitude(location.Longitude);
return location; return location;
} }
private void SetZoomLevel(double zoomLevel) private void ZoomLevelPropertyChanged(double zoomLevel)
{ {
if (updateTransform) if (updateTransform)
{ {
SetViewTransform(); UpdateViewTransform();
} }
if (zoomLevelAnimation == null) if (zoomLevelAnimation == null)
@ -591,7 +583,7 @@ namespace MapControl
} }
} }
private void SetTargetZoomLevel(double targetZoomLevel) private void TargetZoomLevelPropertyChanged(double targetZoomLevel)
{ {
if (targetZoomLevel != ZoomLevel) if (targetZoomLevel != ZoomLevel)
{ {
@ -625,16 +617,16 @@ namespace MapControl
ResetTransformOrigin(); ResetTransformOrigin();
} }
private double CoerceZoomLevel(double zoomLevel) private double CoerceZoomLevelProperty(double zoomLevel)
{ {
return Math.Min(Math.Max(zoomLevel, MinZoomLevel), MaxZoomLevel); return Math.Min(Math.Max(zoomLevel, MinZoomLevel), MaxZoomLevel);
} }
private void SetHeading(double heading) private void HeadingPropertyChanged(double heading)
{ {
if (updateTransform) if (updateTransform)
{ {
SetViewTransform(); UpdateViewTransform();
} }
if (headingAnimation == null) if (headingAnimation == null)
@ -643,7 +635,7 @@ namespace MapControl
} }
} }
private void SetTargetHeading(double targetHeading) private void TargetHeadingPropertyChanged(double targetHeading)
{ {
if (targetHeading != Heading) if (targetHeading != Heading)
{ {
@ -687,12 +679,12 @@ namespace MapControl
headingAnimation = null; headingAnimation = null;
} }
private double CoerceHeading(double heading) private double CoerceHeadingProperty(double heading)
{ {
return ((heading % 360d) + 360d) % 360d; return ((heading % 360d) + 360d) % 360d;
} }
private void SetViewTransform() private void UpdateViewTransform()
{ {
double scale; double scale;

View file

@ -19,7 +19,7 @@
<DebugType>full</DebugType> <DebugType>full</DebugType>
<Optimize>false</Optimize> <Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath> <OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>TRACE;DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>

View file

@ -51,7 +51,6 @@ namespace MapControl
} }
Brush.ImageSource = value; Brush.ImageSource = value;
LoadState = TileLoadState.Loaded;
} }
} }

View file

@ -36,7 +36,7 @@ namespace MapControl
internal void BeginDownloadTiles(ICollection<Tile> tiles) internal void BeginDownloadTiles(ICollection<Tile> tiles)
{ {
ThreadPool.QueueUserWorkItem(BeginDownloadTiles, new List<Tile>(tiles.Reverse().Where(t => t.LoadState == TileLoadState.NotLoaded))); ThreadPool.QueueUserWorkItem(BeginDownloadTilesAsync, new List<Tile>(tiles.Where(t => t.LoadState == TileLoadState.NotLoaded)));
} }
internal void EndDownloadTiles() internal void EndDownloadTiles()
@ -47,7 +47,7 @@ namespace MapControl
} }
} }
private void BeginDownloadTiles(object newTilesList) private void BeginDownloadTilesAsync(object newTilesList)
{ {
List<Tile> newTiles = (List<Tile>)newTilesList; List<Tile> newTiles = (List<Tile>)newTilesList;
@ -65,6 +65,7 @@ namespace MapControl
if (image != null) if (image != null)
{ {
tile.LoadState = TileLoadState.Loaded;
Dispatcher.BeginInvoke((Action)(() => tile.Image = image)); Dispatcher.BeginInvoke((Action)(() => tile.Image = image));
if (cacheExpired) if (cacheExpired)
@ -97,16 +98,24 @@ namespace MapControl
tile.LoadState = TileLoadState.Loading; tile.LoadState = TileLoadState.Loading;
numDownloads++; numDownloads++;
ThreadPool.QueueUserWorkItem(DownloadTile, tile); ThreadPool.QueueUserWorkItem(DownloadTileAsync, tile);
} }
} }
private void DownloadTile(object t) private void DownloadTileAsync(object t)
{ {
Tile tile = (Tile)t; Tile tile = (Tile)t;
ImageSource image = DownloadImage(tile); ImageSource image = DownloadImage(tile);
Dispatcher.BeginInvoke((Action)(() => tile.Image = image)); if (image != null)
{
tile.LoadState = TileLoadState.Loaded;
Dispatcher.BeginInvoke((Action)(() => tile.Image = image));
}
else
{
tile.LoadState = TileLoadState.NotLoaded;
}
lock (pendingTiles) lock (pendingTiles)
{ {
@ -263,9 +272,7 @@ namespace MapControl
private static void TraceInformation(string format, params object[] args) private static void TraceInformation(string format, params object[] args)
{ {
#if TRACE
System.Diagnostics.Trace.TraceInformation("[{0:00}] {1}", Thread.CurrentThread.ManagedThreadId, string.Format(format, args)); System.Diagnostics.Trace.TraceInformation("[{0:00}] {1}", Thread.CurrentThread.ManagedThreadId, string.Format(format, args));
#endif
} }
} }
} }

View file

@ -8,6 +8,7 @@ using System.Linq;
using System.Windows; using System.Windows;
using System.Windows.Markup; using System.Windows.Markup;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Animation;
namespace MapControl namespace MapControl
{ {

View file

@ -148,7 +148,7 @@
<map:Map Name="map" IsManipulationEnabled="True" Margin="2" FontSize="10" <map:Map Name="map" IsManipulationEnabled="True" Margin="2" FontSize="10"
LightForeground="Black" LightBackground="White" DarkForeground="White" DarkBackground="#FF3F3F3F" LightForeground="Black" LightBackground="White" DarkForeground="White" DarkBackground="#FF3F3F3F"
Center="53.5,8.2" ZoomLevel="11" Center="53.5,8.2" ZoomLevel="11"
MainTileLayer="{Binding ElementName=tileSourceComboBox, Path=SelectedItem}" MainTileLayer="{Binding ElementName=tileLayerComboBox, Path=SelectedItem}"
ManipulationInertiaStarting="MapManipulationInertiaStarting" ManipulationInertiaStarting="MapManipulationInertiaStarting"
MouseMove="MapMouseMove" MouseLeave="MapMouseLeave"> MouseMove="MapMouseMove" MouseLeave="MapMouseLeave">
<map:MapGraticule Opacity="0.6"/> <map:MapGraticule Opacity="0.6"/>
@ -170,7 +170,7 @@
<Slider Name="headingSlider" ToolTip="Heading" Margin="4,0,4,0" Width="100" Minimum="0" Maximum="360" SmallChange="10" LargeChange="45" <Slider Name="headingSlider" ToolTip="Heading" Margin="4,0,4,0" Width="100" Minimum="0" Maximum="360" SmallChange="10" LargeChange="45"
Value="{Binding ElementName=map, Path=TargetHeading}"/> Value="{Binding ElementName=map, Path=TargetHeading}"/>
<CheckBox ToolTip="Map Overlay" Margin="4,0,4,0" VerticalAlignment="Center" Content="Seamarks" Click="SeamarksClick"/> <CheckBox ToolTip="Map Overlay" Margin="4,0,4,0" VerticalAlignment="Center" Content="Seamarks" Click="SeamarksClick"/>
<ComboBox Name="tileSourceComboBox" ToolTip="Main Tile Layer" Margin="4,0,4,0" DisplayMemberPath="Name" SelectedIndex="0"> <ComboBox Name="tileLayerComboBox" ToolTip="Main Tile Layer" Margin="4,0,4,0" DisplayMemberPath="Name" SelectedIndex="0">
<ComboBox.Items> <ComboBox.Items>
<map:TileLayer Name="OpenStreetMap" Description="© {y} OpenStreetMap Contributors, CC-BY-SA" <map:TileLayer Name="OpenStreetMap" Description="© {y} OpenStreetMap Contributors, CC-BY-SA"
TileSource="http://{c}.tile.openstreetmap.org/{z}/{x}/{y}.png" TileSource="http://{c}.tile.openstreetmap.org/{z}/{x}/{y}.png"