2018-02-09 17:43:47 +01:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
|
|
|
|
|
// © 2018 Clemens Fischer
|
|
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
#if WINDOWS_UWP
|
|
|
|
|
|
using Windows.Foundation;
|
|
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
|
using Windows.UI.Xaml.Media;
|
|
|
|
|
|
#else
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Base class for MapPolyline and MapPolygon.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public abstract partial class MapShape : IMapElement
|
|
|
|
|
|
{
|
|
|
|
|
|
public static readonly DependencyProperty LocationProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(Location), typeof(Location), typeof(MapShape),
|
|
|
|
|
|
new PropertyMetadata(null, (o, e) => ((MapShape)o).LocationPropertyChanged()));
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets an optional Location to constrain the viewport position 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); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-11 19:46:31 +01:00
|
|
|
|
private void LocationPropertyChanged()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (parentMap != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
OnViewportChanged(parentMap, new ViewportChangedEventArgs());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-09 17:43:47 +01:00
|
|
|
|
private MapBase parentMap;
|
|
|
|
|
|
|
|
|
|
|
|
public MapBase ParentMap
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return parentMap; }
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
if (parentMap != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
parentMap.ViewportChanged -= OnViewportChanged;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
parentMap = value;
|
|
|
|
|
|
|
|
|
|
|
|
if (parentMap != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
parentMap.ViewportChanged += OnViewportChanged;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-14 18:10:53 +01:00
|
|
|
|
SetDataTransform();
|
|
|
|
|
|
UpdateData();
|
2018-02-09 17:43:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-14 18:10:53 +01:00
|
|
|
|
protected MapShape()
|
|
|
|
|
|
: this(new PathGeometry())
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-13 20:11:16 +01:00
|
|
|
|
protected MapShape(Geometry data)
|
2018-02-09 17:43:47 +01:00
|
|
|
|
{
|
2018-02-13 20:11:16 +01:00
|
|
|
|
Data = data;
|
2018-02-09 17:43:47 +01:00
|
|
|
|
|
|
|
|
|
|
MapPanel.InitMapElement(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-14 18:10:53 +01:00
|
|
|
|
partial void SetDataTransform(); // WPF only
|
|
|
|
|
|
|
2018-02-09 17:43:47 +01:00
|
|
|
|
protected abstract void UpdateData();
|
|
|
|
|
|
|
|
|
|
|
|
protected Point LocationToPoint(Location location)
|
|
|
|
|
|
{
|
|
|
|
|
|
var point = parentMap.MapProjection.LocationToPoint(location);
|
|
|
|
|
|
|
|
|
|
|
|
if (point.Y == double.PositiveInfinity)
|
|
|
|
|
|
{
|
|
|
|
|
|
point.Y = 1e9;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (point.X == double.NegativeInfinity)
|
|
|
|
|
|
{
|
|
|
|
|
|
point.Y = -1e9;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return point;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-13 20:11:16 +01:00
|
|
|
|
protected Point LocationToViewportPoint(Location location)
|
|
|
|
|
|
{
|
|
|
|
|
|
return parentMap.MapProjection.ViewportTransform.Transform(LocationToPoint(location));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-09 17:43:47 +01:00
|
|
|
|
protected double GetLongitudeOffset()
|
|
|
|
|
|
{
|
|
|
|
|
|
var longitudeOffset = 0d;
|
|
|
|
|
|
|
|
|
|
|
|
if (parentMap.MapProjection.IsContinuous && Location != null)
|
|
|
|
|
|
{
|
2018-02-13 20:11:16 +01:00
|
|
|
|
var viewportPosition = LocationToViewportPoint(Location);
|
2018-02-09 17:43:47 +01:00
|
|
|
|
|
|
|
|
|
|
if (viewportPosition.X < 0d || viewportPosition.X > parentMap.RenderSize.Width ||
|
|
|
|
|
|
viewportPosition.Y < 0d || viewportPosition.Y > parentMap.RenderSize.Height)
|
|
|
|
|
|
{
|
|
|
|
|
|
var nearestLongitude = Location.NearestLongitude(Location.Longitude, parentMap.Center.Longitude);
|
|
|
|
|
|
|
|
|
|
|
|
longitudeOffset = nearestLongitude - Location.Longitude;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return longitudeOffset;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|