mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
Improved MapPanel.ViewportPosition, added moving item to sample application.
This commit is contained in:
parent
ac107641d2
commit
022e246ebe
|
|
@ -68,6 +68,7 @@
|
|||
<Compile Include="TileLayer.cs" />
|
||||
<Compile Include="TileLayerCollection.cs" />
|
||||
<Compile Include="TileSource.cs" />
|
||||
<Compile Include="ViewportPosition.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Page Include="Themes\Generic.xaml">
|
||||
|
|
|
|||
|
|
@ -121,8 +121,9 @@ namespace MapControl
|
|||
foreach (object item in Items)
|
||||
{
|
||||
MapItem mapItem = GetMapItem(item);
|
||||
ViewportPosition viewportPosition = MapPanel.GetViewportPosition(mapItem);
|
||||
|
||||
if (mapItem != null && MapPanel.HasViewportPosition(mapItem) && geometry.FillContains(MapPanel.GetViewportPosition(mapItem)))
|
||||
if (mapItem != null && viewportPosition != null && geometry.FillContains(viewportPosition.Position))
|
||||
{
|
||||
SelectedItems.Add(item);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@
|
|||
// Copyright © 2012 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
|
|
@ -12,8 +12,7 @@ namespace MapControl
|
|||
/// Positions child elements on a Map. A child element's position is specified by the
|
||||
/// attached property Location, given as geographic location with latitude and longitude.
|
||||
/// The attached property ViewportPosition gets a child element's position in viewport
|
||||
/// coordinates. IsInsideMapBounds indicates if the viewport coordinates are located
|
||||
/// inside the visible part of the map.
|
||||
/// coordinates and indicates if the coordinates are located inside the bounds of the ParentMap.
|
||||
/// </summary>
|
||||
public class MapPanel : Panel
|
||||
{
|
||||
|
|
@ -23,16 +22,13 @@ namespace MapControl
|
|||
|
||||
public static readonly DependencyProperty LocationProperty = DependencyProperty.RegisterAttached(
|
||||
"Location", typeof(Location), typeof(MapPanel),
|
||||
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender, LocationPropertyChanged));
|
||||
new FrameworkPropertyMetadata(LocationPropertyChanged));
|
||||
|
||||
private static readonly DependencyPropertyKey ViewportPositionPropertyKey = DependencyProperty.RegisterAttachedReadOnly(
|
||||
"ViewportPosition", typeof(Point), typeof(MapPanel), null);
|
||||
|
||||
private static readonly DependencyPropertyKey IsInsideMapBoundsPropertyKey = DependencyProperty.RegisterAttachedReadOnly(
|
||||
"IsInsideMapBounds", typeof(bool), typeof(MapPanel), null);
|
||||
"ViewportPosition", typeof(ViewportPosition), typeof(MapPanel),
|
||||
new FrameworkPropertyMetadata(ViewportPositionPropertyChanged));
|
||||
|
||||
public static readonly DependencyProperty ViewportPositionProperty = ViewportPositionPropertyKey.DependencyProperty;
|
||||
public static readonly DependencyProperty IsInsideMapBoundsProperty = IsInsideMapBoundsPropertyKey.DependencyProperty;
|
||||
|
||||
public MapPanel()
|
||||
{
|
||||
|
|
@ -59,19 +55,9 @@ namespace MapControl
|
|||
element.SetValue(LocationProperty, value);
|
||||
}
|
||||
|
||||
public static bool HasViewportPosition(UIElement element)
|
||||
public static ViewportPosition GetViewportPosition(UIElement element)
|
||||
{
|
||||
return element.ReadLocalValue(ViewportPositionProperty) != DependencyProperty.UnsetValue;
|
||||
}
|
||||
|
||||
public static Point GetViewportPosition(UIElement element)
|
||||
{
|
||||
return (Point)element.GetValue(ViewportPositionProperty);
|
||||
}
|
||||
|
||||
public static bool GetIsInsideMapBounds(UIElement element)
|
||||
{
|
||||
return (bool)element.GetValue(IsInsideMapBoundsProperty);
|
||||
return (ViewportPosition)element.GetValue(ViewportPositionProperty);
|
||||
}
|
||||
|
||||
protected override Size MeasureOverride(Size availableSize)
|
||||
|
|
@ -90,10 +76,9 @@ namespace MapControl
|
|||
{
|
||||
foreach (UIElement element in InternalChildren)
|
||||
{
|
||||
object viewportPosition = element.ReadLocalValue(ViewportPositionProperty);
|
||||
ViewportPosition viewportPosition = GetViewportPosition(element);
|
||||
|
||||
if (viewportPosition == DependencyProperty.UnsetValue ||
|
||||
!ArrangeElement(element, (Point)viewportPosition))
|
||||
if (viewportPosition == null || !ArrangeElement(element, viewportPosition))
|
||||
{
|
||||
ArrangeElement(element, finalSize);
|
||||
}
|
||||
|
|
@ -102,6 +87,11 @@ namespace MapControl
|
|||
return finalSize;
|
||||
}
|
||||
|
||||
protected virtual Point GetArrangePosition(ViewportPosition viewportPosition)
|
||||
{
|
||||
return viewportPosition.Position;
|
||||
}
|
||||
|
||||
protected virtual void OnViewportChanged()
|
||||
{
|
||||
Map parentMap = ParentMap;
|
||||
|
|
@ -140,42 +130,59 @@ namespace MapControl
|
|||
|
||||
private static void LocationPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs eventArgs)
|
||||
{
|
||||
UIElement element = (UIElement)obj;
|
||||
Location location = (Location)eventArgs.NewValue;
|
||||
Map parentMap;
|
||||
UIElement element = obj as UIElement;
|
||||
|
||||
if (location != null && (parentMap = GetParentMap(element)) != null)
|
||||
if (element != null)
|
||||
{
|
||||
SetViewportPosition(element, parentMap, location);
|
||||
}
|
||||
else
|
||||
{
|
||||
element.ClearValue(ViewportPositionPropertyKey);
|
||||
element.ClearValue(IsInsideMapBoundsPropertyKey);
|
||||
element.Arrange(new Rect());
|
||||
SetViewportPosition(element, GetParentMap(element), (Location)eventArgs.NewValue);
|
||||
}
|
||||
}
|
||||
|
||||
private static void SetViewportPosition(UIElement element, Map parentMap, Location location)
|
||||
{
|
||||
Point viewportPosition = parentMap.LocationToViewportPoint(location);
|
||||
ViewportPosition viewportPosition = null;
|
||||
|
||||
if (parentMap != null && location != null)
|
||||
{
|
||||
Point position = parentMap.LocationToViewportPoint(location);
|
||||
|
||||
viewportPosition = new ViewportPosition(position,
|
||||
position.X >= 0d && position.X <= parentMap.ActualWidth &&
|
||||
position.Y >= 0d && position.Y <= parentMap.ActualHeight);
|
||||
}
|
||||
|
||||
element.SetValue(ViewportPositionPropertyKey, viewportPosition);
|
||||
element.SetValue(IsInsideMapBoundsPropertyKey,
|
||||
viewportPosition.X >= 0d && viewportPosition.X <= parentMap.ActualWidth &&
|
||||
viewportPosition.Y >= 0d && viewportPosition.Y <= parentMap.ActualHeight);
|
||||
|
||||
ArrangeElement(element, viewportPosition);
|
||||
}
|
||||
|
||||
private static bool ArrangeElement(UIElement element, Point position)
|
||||
private static void ViewportPositionPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs eventArgs)
|
||||
{
|
||||
UIElement element = obj as UIElement;
|
||||
|
||||
if (element != null)
|
||||
{
|
||||
ViewportPosition position = (ViewportPosition)eventArgs.NewValue;
|
||||
|
||||
if (position != null)
|
||||
{
|
||||
ArrangeElement(element, position);
|
||||
}
|
||||
else
|
||||
{
|
||||
element.Arrange(new Rect());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static bool ArrangeElement(UIElement element, ViewportPosition viewportPosition)
|
||||
{
|
||||
MapPanel panel = VisualTreeHelper.GetParent(element) as MapPanel;
|
||||
Point position = panel != null ? panel.GetArrangePosition(viewportPosition) : viewportPosition.Position;
|
||||
Rect rect = new Rect(position, element.DesiredSize);
|
||||
FrameworkElement frameworkElement = element as FrameworkElement;
|
||||
|
||||
if (frameworkElement != null)
|
||||
{
|
||||
if (frameworkElement.HorizontalAlignment == HorizontalAlignment.Stretch &&
|
||||
if (frameworkElement.HorizontalAlignment == HorizontalAlignment.Stretch ||
|
||||
frameworkElement.VerticalAlignment == VerticalAlignment.Stretch)
|
||||
{
|
||||
return false; // do not arrange at position
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@
|
|||
</Setter>
|
||||
</Style>
|
||||
<Style x:Key="{x:Type map:MapItem}" TargetType="{x:Type map:MapItem}">
|
||||
<Setter Property="HorizontalAlignment" Value="Center"/>
|
||||
<Setter Property="VerticalAlignment" Value="Center"/>
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="{x:Type map:MapItem}">
|
||||
|
|
|
|||
23
MapControl/ViewportPosition.cs
Normal file
23
MapControl/ViewportPosition.cs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
// WPF MapControl - http://wpfmapcontrol.codeplex.com/
|
||||
// Copyright © 2012 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System.Windows;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
/// <summary>
|
||||
/// Viewport position of a MapPanel child element.
|
||||
/// </summary>
|
||||
public class ViewportPosition
|
||||
{
|
||||
public ViewportPosition(Point position, bool isInside)
|
||||
{
|
||||
Position = position;
|
||||
IsInside = isInside;
|
||||
}
|
||||
|
||||
public Point Position { get; private set; }
|
||||
public bool IsInside { get; private set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -65,7 +65,7 @@
|
|||
</Style>
|
||||
<Style x:Key="PushpinItemStyle" TargetType="map:MapItem">
|
||||
<Setter Property="map:MapPanel.Location" Value="{Binding Location}"/>
|
||||
<Setter Property="Visibility" Value="{Binding (map:MapPanel.IsInsideMapBounds), Converter={StaticResource BooleanToVisibilityConverter}, RelativeSource={RelativeSource Self}}"/>
|
||||
<Setter Property="Visibility" Value="{Binding (map:MapPanel.ViewportPosition).IsInside, Converter={StaticResource BooleanToVisibilityConverter}, RelativeSource={RelativeSource Self}}"/>
|
||||
<Setter Property="HorizontalAlignment" Value="Left"/>
|
||||
<Setter Property="VerticalAlignment" Value="Bottom"/>
|
||||
<Setter Property="Template">
|
||||
|
|
@ -103,7 +103,7 @@
|
|||
ItemContainerStyle="{StaticResource PushpinItemStyle}"
|
||||
IsSynchronizedWithCurrentItem="True"/>
|
||||
<map:Pushpin Location="53.5,8.2" Background="Yellow" Foreground="Blue" Content="N 53° 30' E 8° 12'"
|
||||
Visibility="{Binding (map:MapPanel.IsInsideMapBounds), Converter={StaticResource BooleanToVisibilityConverter}, RelativeSource={RelativeSource Self}}"/>
|
||||
Visibility="{Binding (map:MapPanel.ViewportPosition).IsInside, Converter={StaticResource BooleanToVisibilityConverter}, RelativeSource={RelativeSource Self}}"/>
|
||||
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="2,0,0,0" FontSize="10"
|
||||
Text="{Binding MainTileLayer.Description, ElementName=map}"/>
|
||||
</map:Map>
|
||||
|
|
@ -119,7 +119,7 @@
|
|||
<Slider Name="headingSlider" ToolTip="Heading" Margin="4,0,4,0" Width="100" Minimum="0" Maximum="360" SmallChange="10" LargeChange="45"
|
||||
Value="{Binding TargetHeading, ElementName=map}"/>
|
||||
<CheckBox ToolTip="Map Overlay" Margin="4,0,4,0" VerticalAlignment="Center" Content="Seamarks" Click="SeamarksClick"/>
|
||||
<ComboBox Name="tileLayerComboBox" ToolTip="Main Tile Layer" Margin="4,0,4,0" DisplayMemberPath="Name" SelectedIndex="0">
|
||||
<ComboBox Name="tileLayerComboBox" ToolTip="Main Tile Layer" Margin="4,0,0,0" DisplayMemberPath="Name" SelectedIndex="0">
|
||||
<ComboBox.Items>
|
||||
<map:TileLayer Name="OpenStreetMap" Description="© {y} OpenStreetMap Contributors, CC-BY-SA"
|
||||
TileSource="http://{c}.tile.openstreetmap.org/{z}/{x}/{y}.png"/>
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Threading;
|
||||
using Caching;
|
||||
using MapControl;
|
||||
|
||||
|
|
@ -9,6 +11,12 @@ namespace SampleApplication
|
|||
{
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
private SamplePoint movingPoint = new SamplePoint
|
||||
{
|
||||
Name = "Moving",
|
||||
Location = new Location(53.5, 8.25)
|
||||
};
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
if (Properties.Settings.Default.UsePersistentCache)
|
||||
|
|
@ -67,6 +75,7 @@ namespace SampleApplication
|
|||
Name = "Buhne 10",
|
||||
Location = new Location(53.49350, 8.15563)
|
||||
});
|
||||
points.Add(movingPoint);
|
||||
|
||||
ICollection<object> pushpins = (ICollection<object>)Resources["Pushpins"];
|
||||
pushpins.Add(
|
||||
|
|
@ -93,6 +102,22 @@ namespace SampleApplication
|
|||
Name = "Eckwarderhörne",
|
||||
Location = new Location(53.5207, 8.2323)
|
||||
});
|
||||
|
||||
DispatcherTimer timer = new DispatcherTimer();
|
||||
timer.Interval = TimeSpan.FromSeconds(0.05);
|
||||
timer.Tick += MovePoint;
|
||||
timer.Start();
|
||||
}
|
||||
|
||||
private void MovePoint(object sender, EventArgs e)
|
||||
{
|
||||
movingPoint.Location = new Location(movingPoint.Location.Latitude + 0.0005, movingPoint.Location.Longitude + 0.001);
|
||||
|
||||
if (movingPoint.Location.Latitude > 54d)
|
||||
{
|
||||
movingPoint.Name = "Stopped";
|
||||
((DispatcherTimer)sender).Stop();
|
||||
}
|
||||
}
|
||||
|
||||
private void MapManipulationInertiaStarting(object sender, ManipulationInertiaStartingEventArgs e)
|
||||
|
|
|
|||
|
|
@ -1,12 +1,43 @@
|
|||
using System.Collections.ObjectModel;
|
||||
using MapControl;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace SampleApplication
|
||||
{
|
||||
class SamplePoint
|
||||
class SamplePoint : INotifyPropertyChanged
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public Location Location { get; set; }
|
||||
private string name;
|
||||
private Location location;
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
public string Name
|
||||
{
|
||||
get { return name; }
|
||||
set
|
||||
{
|
||||
name = value;
|
||||
OnPropertyChanged("Name");
|
||||
}
|
||||
}
|
||||
|
||||
public Location Location
|
||||
{
|
||||
get { return location; }
|
||||
set
|
||||
{
|
||||
location = value;
|
||||
OnPropertyChanged("Location");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPropertyChanged(string propertyName)
|
||||
{
|
||||
if (PropertyChanged != null)
|
||||
{
|
||||
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SamplePolyline
|
||||
|
|
|
|||
Loading…
Reference in a new issue