mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-06 06:55:04 +00:00
Version 5.0: Reworked MapBase and MapPath
This commit is contained in:
parent
06fd31c17b
commit
49e15ce424
41 changed files with 466 additions and 1068 deletions
142
MapControl/Shared/MapPath.cs
Normal file
142
MapControl/Shared/MapPath.cs
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// © 2020 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
#if WINDOWS_UWP
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Media;
|
||||
#else
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
#endif
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
/// <summary>
|
||||
/// A path element with a Data property that holds a Geometry in cartesian map coordinates
|
||||
/// or view coordinates. Cartesian coordinates can optionally be relative to an origin Location.
|
||||
/// </summary>
|
||||
public partial class MapPath : IMapElement
|
||||
{
|
||||
public static readonly DependencyProperty LocationProperty = DependencyProperty.Register(
|
||||
nameof(Location), typeof(Location), typeof(MapPath),
|
||||
new PropertyMetadata(null, (o, e) => ((MapPath)o).LocationOrViewportChanged()));
|
||||
|
||||
private MapBase parentMap;
|
||||
private MatrixTransform dataTransform;
|
||||
private double longitudeOffset;
|
||||
|
||||
public MapPath()
|
||||
{
|
||||
MapPanel.InitMapElement(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a Location that is used as
|
||||
/// - either the origin point of a geometry specified in cartesian map units (meters)
|
||||
/// - or as an optional value to constrain the view position of MapPaths with multiple
|
||||
/// Locations (like MapPolyline or MapPolygon) to the visible map viewport, as done
|
||||
/// for elements where the MapPanel.Location property is set.
|
||||
/// </summary>
|
||||
public Location Location
|
||||
{
|
||||
get { return (Location)GetValue(LocationProperty); }
|
||||
set { SetValue(LocationProperty, value); }
|
||||
}
|
||||
|
||||
public MapBase ParentMap
|
||||
{
|
||||
get { return parentMap; }
|
||||
set
|
||||
{
|
||||
if (parentMap != null)
|
||||
{
|
||||
parentMap.ViewportChanged -= OnViewportChanged;
|
||||
}
|
||||
|
||||
parentMap = value;
|
||||
|
||||
if (parentMap != null)
|
||||
{
|
||||
parentMap.ViewportChanged += OnViewportChanged;
|
||||
}
|
||||
|
||||
LocationOrViewportChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
|
||||
{
|
||||
LocationOrViewportChanged();
|
||||
}
|
||||
|
||||
private void LocationOrViewportChanged()
|
||||
{
|
||||
longitudeOffset = 0d;
|
||||
|
||||
if (parentMap != null && parentMap.MapProjection.IsNormalCylindrical && Location != null)
|
||||
{
|
||||
var viewPos = LocationToView(Location);
|
||||
|
||||
if (viewPos.X < 0d || viewPos.X > parentMap.RenderSize.Width ||
|
||||
viewPos.Y < 0d || viewPos.Y > parentMap.RenderSize.Height)
|
||||
{
|
||||
longitudeOffset = Location.NearestLongitude(Location.Longitude, parentMap.Center.Longitude) - Location.Longitude;
|
||||
}
|
||||
}
|
||||
|
||||
UpdateData();
|
||||
}
|
||||
|
||||
protected virtual void UpdateData()
|
||||
{
|
||||
if (parentMap != null && Data != null)
|
||||
{
|
||||
if (dataTransform == null)
|
||||
{
|
||||
Data.Transform = dataTransform = new MatrixTransform();
|
||||
}
|
||||
|
||||
if (Location != null)
|
||||
{
|
||||
var viewPos = LocationToView(Location);
|
||||
var scale = parentMap.GetScale(Location);
|
||||
var matrix = new Matrix(scale.X, 0, 0, scale.Y, 0, 0);
|
||||
matrix.Rotate(parentMap.Heading);
|
||||
matrix.Translate(viewPos.X, viewPos.Y);
|
||||
dataTransform.Matrix = matrix;
|
||||
}
|
||||
else
|
||||
{
|
||||
dataTransform.Matrix = parentMap.ViewTransform.MapToViewMatrix;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected Point LocationToMap(Location location)
|
||||
{
|
||||
if (longitudeOffset != 0d)
|
||||
{
|
||||
location = new Location(location.Latitude, location.Longitude + longitudeOffset);
|
||||
}
|
||||
|
||||
var point = parentMap.MapProjection.LocationToMap(location);
|
||||
|
||||
if (point.Y == double.PositiveInfinity)
|
||||
{
|
||||
point.Y = 1e9;
|
||||
}
|
||||
else if (point.X == double.NegativeInfinity)
|
||||
{
|
||||
point.Y = -1e9;
|
||||
}
|
||||
|
||||
return point;
|
||||
}
|
||||
|
||||
protected Point LocationToView(Location location)
|
||||
{
|
||||
return parentMap.ViewTransform.MapToView(LocationToMap(location));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue