Changed map tile download order, added Pushpin class, changed default MapItem style.

This commit is contained in:
ClemensF 2012-06-13 17:33:23 +02:00
parent a0e0403c90
commit bb3a9cf8c3
9 changed files with 79 additions and 39 deletions

View file

@ -42,7 +42,8 @@ namespace MapControl
public static readonly DependencyProperty TileLayersProperty = DependencyProperty.Register(
"TileLayers", typeof(TileLayerCollection), typeof(Map), new FrameworkPropertyMetadata(
(o, e) => ((Map)o).TileLayersPropertyChanged((TileLayerCollection)e.NewValue)));
(o, e) => ((Map)o).TileLayersPropertyChanged((TileLayerCollection)e.NewValue),
(o, v) => ((Map)o).CoerceTileLayersProperty((TileLayerCollection)v)));
public static readonly DependencyProperty MainTileLayerProperty = DependencyProperty.Register(
"MainTileLayer", typeof(TileLayer), typeof(Map), new FrameworkPropertyMetadata(
@ -105,6 +106,7 @@ namespace MapControl
MinZoomLevel = 1;
MaxZoomLevel = 20;
AddVisualChild(tileContainer);
TileLayers = new TileLayerCollection();
SetValue(ParentMapProperty, this);
}
@ -482,20 +484,29 @@ namespace MapControl
}
private void TileLayersPropertyChanged(TileLayerCollection tileLayers)
{
if (tileLayers != null)
{
tileContainer.TileLayers = tileLayers;
MainTileLayer = tileLayers.Count > 0 ? tileLayers[0] : null;
}
}
private TileLayerCollection CoerceTileLayersProperty(TileLayerCollection tileLayers)
{
if (tileLayers == null)
{
tileLayers = new TileLayerCollection();
}
return tileLayers;
}
private void MainTileLayerPropertyChanged(TileLayer mainTileLayer)
{
if (mainTileLayer != null)
{
if (tileContainer.TileLayers == null)
{
TileLayers = new TileLayerCollection(mainTileLayer);
}
else if (tileContainer.TileLayers.Count == 0)
if (tileContainer.TileLayers.Count == 0)
{
tileContainer.TileLayers.Add(mainTileLayer);
}
@ -508,7 +519,7 @@ namespace MapControl
private TileLayer CoerceMainTileLayerProperty(TileLayer mainTileLayer)
{
if (mainTileLayer == null && tileContainer.TileLayers != null && tileContainer.TileLayers.Count > 0)
if (mainTileLayer == null && tileContainer.TileLayers.Count > 0)
{
mainTileLayer = tileContainer.TileLayers[0];
}

View file

@ -53,6 +53,7 @@
<Compile Include="MapPanel.cs" />
<Compile Include="MapPolygon.cs" />
<Compile Include="MapPolyline.cs" />
<Compile Include="Pushpin.cs" />
<Compile Include="TileImageLoader.cs" />
<Compile Include="MapTransform.cs" />
<Compile Include="Map.cs" />

View file

@ -36,11 +36,11 @@ namespace MapControl
static MapItem()
{
FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(MapItem),
new FrameworkPropertyMetadata(typeof(MapItem)));
FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(
typeof(MapItem), new FrameworkPropertyMetadata(typeof(MapItem)));
UIElement.IsEnabledProperty.OverrideMetadata(typeof(MapItem),
new FrameworkPropertyMetadata((o, e) => ((MapItem)o).CommonStateChanged()));
UIElement.IsEnabledProperty.OverrideMetadata(
typeof(MapItem), new FrameworkPropertyMetadata((o, e) => ((MapItem)o).CommonStateChanged()));
}
public event RoutedEventHandler Selected

29
MapControl/Pushpin.cs Normal file
View file

@ -0,0 +1,29 @@
using System;
using System.Windows;
using System.Windows.Controls;
namespace MapControl
{
/// <summary>
/// Displays a pushpin at the geographic location provided by the Location property.
/// </summary>
[TemplateVisualState(Name = "Normal", GroupName = "CommonStates")]
[TemplateVisualState(Name = "MouseOver", GroupName = "CommonStates")]
[TemplateVisualState(Name = "Disabled", GroupName = "CommonStates")]
public class Pushpin : ContentControl
{
public static readonly DependencyProperty LocationProperty = MapPanel.LocationProperty.AddOwner(typeof(Pushpin));
static Pushpin()
{
FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(
typeof(Pushpin), new FrameworkPropertyMetadata(typeof(Pushpin)));
}
public Location Location
{
get { return (Location)GetValue(LocationProperty); }
set { SetValue(LocationProperty, value); }
}
}
}

View file

@ -11,6 +11,20 @@
</Setter>
</Style>
<Style x:Key="{x:Type map:MapItem}" TargetType="{x:Type map:MapItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type map:MapItem}">
<ContentPresenter Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Cursor="{TemplateBinding Cursor}"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="{x:Type map:Pushpin}" TargetType="{x:Type map:Pushpin}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
@ -18,7 +32,7 @@
<Setter Property="Background" Value="White"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type map:MapItem}">
<ControlTemplate TargetType="{x:Type map:Pushpin}">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
@ -34,14 +48,6 @@
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected"/>
<VisualState x:Name="Selected"/>
</VisualStateGroup>
<VisualStateGroup x:Name="CurrentStates">
<VisualState x:Name="NonCurrent"/>
<VisualState x:Name="Current"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid.RowDefinitions>
<RowDefinition/>

View file

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

View file

@ -12,15 +12,6 @@ namespace MapControl
{
private string name;
public TileLayerCollection()
{
}
public TileLayerCollection(TileLayer tileLayer)
{
Add(tileLayer);
}
public string Name
{
get { return !string.IsNullOrEmpty(name) ? name : (Count > 0 ? this[0].Name : string.Empty); }

View file

@ -13,6 +13,8 @@
<local:SampleItemStyleSelector x:Key="SampleItemStyleSelector"/>
<Style x:Key="SamplePushpinItemStyle" TargetType="map:MapItem">
<Setter Property="map:MapPanel.Location" Value="{Binding Location}"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="Content" Value="{Binding Name}"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
@ -22,6 +24,8 @@
</Style>
<Style x:Key="SamplePointItemStyle" TargetType="map:MapItem">
<Setter Property="map:MapPanel.Location" Value="{Binding Location}"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="map:MapItem">
@ -68,6 +72,8 @@
</Style>
<Style x:Key="SampleShapeItemStyle" TargetType="map:MapItem">
<Setter Property="map:MapPanel.Location" Value="{Binding Location}"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="map:MapItem">
@ -118,8 +124,6 @@
</Setter>
</Style>
<Style x:Key="SamplePolylineItemStyle" TargetType="map:MapItem">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="map:MapItem">
@ -129,8 +133,6 @@
</Setter>
</Style>
<Style x:Key="SamplePolygonItemStyle" TargetType="map:MapItem">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="map:MapItem">
@ -155,6 +157,7 @@
<map:MapItemsControl ItemsSource="{StaticResource SampleItems}"
ItemContainerStyleSelector="{StaticResource SampleItemStyleSelector}"
IsSynchronizedWithCurrentItem="True"/>
<map:Pushpin Location="53.5,8.25" Content="N 53° 30' E 8° 15'"/>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="2,0,0,0" FontSize="10"
Text="{Binding ElementName=map, Path=MainTileLayer.Description}"/>
</map:Map>
@ -186,12 +189,12 @@
IsCached="False"/>
<map:TileLayer Name="MapQuest OSM" Description="MapQuest OSM - © {y} MapQuest &amp; OpenStreetMap Contributors"
TileSource="http://otile{n}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png"
IsCached="False" MaxZoomLevel="20"/>
IsCached="False"/>
<!--<map:TileLayer Name="Google Maps" Description="Google Maps - © {y} Google"
TileSource="http://mt{i}.google.com/vt/x={x}&amp;y={y}&amp;z={z}"
IsCached="False" MaxZoomLevel="20"/>
<map:TileLayer Name="Google Images" Description="Google Maps - © {y} Google"
TileSource="http://khm{i}.google.com/kh/v=108&amp;x={x}&amp;y={y}&amp;z={z}"
TileSource="http://khm{i}.google.com/kh/v=113&amp;x={x}&amp;y={y}&amp;z={z}"
IsCached="False" MaxZoomLevel="20" HasDarkBackground="True"/>
<map:TileLayer Name="Bing Maps" Description="Bing Maps - © {y} Microsoft Corporation"
TileSource="http://ecn.t{i}.tiles.virtualearth.net/tiles/r{q}.png?g=0&amp;stl=h"