mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
101 lines
4.5 KiB
C#
101 lines
4.5 KiB
C#
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
|
|
// © 2016 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
|
|
{
|
|
public partial class MapBase
|
|
{
|
|
public static readonly DependencyProperty ForegroundProperty =
|
|
Control.ForegroundProperty.AddOwner(typeof(MapBase));
|
|
|
|
public static readonly DependencyProperty CenterProperty = DependencyProperty.Register(
|
|
"Center", typeof(Location), typeof(MapBase), new FrameworkPropertyMetadata(
|
|
new Location(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
|
(o, e) => ((MapBase)o).CenterPropertyChanged((Location)e.NewValue)));
|
|
|
|
public static readonly DependencyProperty TargetCenterProperty = DependencyProperty.Register(
|
|
"TargetCenter", typeof(Location), typeof(MapBase), new FrameworkPropertyMetadata(
|
|
new Location(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
|
(o, e) => ((MapBase)o).TargetCenterPropertyChanged((Location)e.NewValue)));
|
|
|
|
public static readonly DependencyProperty ZoomLevelProperty = DependencyProperty.Register(
|
|
"ZoomLevel", typeof(double), typeof(MapBase), new FrameworkPropertyMetadata(
|
|
1d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
|
(o, e) => ((MapBase)o).ZoomLevelPropertyChanged((double)e.NewValue)));
|
|
|
|
public static readonly DependencyProperty TargetZoomLevelProperty = DependencyProperty.Register(
|
|
"TargetZoomLevel", typeof(double), typeof(MapBase), new FrameworkPropertyMetadata(
|
|
1d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
|
(o, e) => ((MapBase)o).TargetZoomLevelPropertyChanged((double)e.NewValue)));
|
|
|
|
public static readonly DependencyProperty HeadingProperty = DependencyProperty.Register(
|
|
"Heading", typeof(double), typeof(MapBase), new FrameworkPropertyMetadata(
|
|
0d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
|
(o, e) => ((MapBase)o).HeadingPropertyChanged((double)e.NewValue)));
|
|
|
|
public static readonly DependencyProperty TargetHeadingProperty = DependencyProperty.Register(
|
|
"TargetHeading", typeof(double), typeof(MapBase), new FrameworkPropertyMetadata(
|
|
0d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
|
(o, e) => ((MapBase)o).TargetHeadingPropertyChanged((double)e.NewValue)));
|
|
|
|
static MapBase()
|
|
{
|
|
ClipToBoundsProperty.OverrideMetadata(
|
|
typeof(MapBase), new FrameworkPropertyMetadata(true));
|
|
|
|
BackgroundProperty.OverrideMetadata(
|
|
typeof(MapBase), new FrameworkPropertyMetadata(Brushes.Transparent));
|
|
}
|
|
|
|
partial void RemoveAnimation(DependencyProperty property)
|
|
{
|
|
BeginAnimation(property, null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Changes the Center property according to the specified translation in viewport coordinates.
|
|
/// </summary>
|
|
public void TranslateMap(Vector translation)
|
|
{
|
|
TranslateMap((Point)translation);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Changes the Center, Heading and ZoomLevel properties according to the specified
|
|
/// viewport coordinate translation, rotation and scale delta values. Rotation and scaling
|
|
/// is performed relative to the specified origin point in viewport coordinates.
|
|
/// </summary>
|
|
public void TransformMap(Point origin, Vector translation, double rotation, double scale)
|
|
{
|
|
TransformMap(origin, (Point)translation, rotation, scale);
|
|
}
|
|
|
|
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
|
|
{
|
|
base.OnRenderSizeChanged(sizeInfo);
|
|
|
|
ResetTransformOrigin();
|
|
UpdateTransform();
|
|
}
|
|
|
|
private void SetViewportTransform(Location origin)
|
|
{
|
|
MapOrigin = mapTransform.Transform(origin);
|
|
ViewportScale = Math.Pow(2d, ZoomLevel) * (double)TileSource.TileSize / 360d;
|
|
|
|
var transform = new Matrix(1d, 0d, 0d, 1d, -MapOrigin.X, -MapOrigin.Y);
|
|
transform.Rotate(-Heading);
|
|
transform.Scale(ViewportScale, -ViewportScale);
|
|
transform.Translate(ViewportOrigin.X, ViewportOrigin.Y);
|
|
|
|
viewportTransform.Matrix = transform;
|
|
}
|
|
}
|
|
}
|