2012-11-22 21:42:29 +01:00
|
|
|
|
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
|
2016-02-23 20:07:30 +01:00
|
|
|
|
// © 2016 Clemens Fischer
|
2012-11-22 21:42:29 +01:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
2015-08-09 20:04:44 +02:00
|
|
|
|
#if NETFX_CORE
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using Windows.Foundation;
|
2013-04-12 19:59:16 +02:00
|
|
|
|
using Windows.UI.Xaml;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
using Windows.UI.Xaml.Controls;
|
|
|
|
|
|
using Windows.UI.Xaml.Media;
|
2013-04-12 19:59:16 +02:00
|
|
|
|
using Windows.UI.Xaml.Shapes;
|
|
|
|
|
|
using Windows.UI.Xaml.Data;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
#else
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
using System.Windows.Media;
|
2013-04-12 19:59:16 +02:00
|
|
|
|
using System.Windows.Shapes;
|
|
|
|
|
|
using System.Windows.Data;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
public partial class MapGraticule
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2013-04-12 19:59:16 +02:00
|
|
|
|
private readonly Path path;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
private Location graticuleStart;
|
|
|
|
|
|
private Location graticuleEnd;
|
|
|
|
|
|
|
2013-04-12 19:59:16 +02:00
|
|
|
|
public MapGraticule()
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2013-04-12 19:59:16 +02:00
|
|
|
|
IsHitTestVisible = false;
|
2014-07-22 20:02:30 +02:00
|
|
|
|
StrokeThickness = 0.5;
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
|
|
|
|
|
path = new Path
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
Data = new PathGeometry()
|
2013-04-12 19:59:16 +02:00
|
|
|
|
};
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
path.SetBinding(Shape.StrokeProperty, new Binding
|
|
|
|
|
|
{
|
|
|
|
|
|
Source = this,
|
|
|
|
|
|
Path = new PropertyPath("Stroke")
|
|
|
|
|
|
});
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
path.SetBinding(Shape.StrokeThicknessProperty, new Binding
|
|
|
|
|
|
{
|
|
|
|
|
|
Source = this,
|
|
|
|
|
|
Path = new PropertyPath("StrokeThickness")
|
|
|
|
|
|
});
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
Children.Add(path);
|
2013-04-12 19:59:16 +02:00
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
protected override void OnViewportChanged()
|
2013-04-12 19:59:16 +02:00
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
var bounds = ParentMap.ViewportTransform.Inverse.TransformBounds(new Rect(new Point(), ParentMap.RenderSize));
|
|
|
|
|
|
var start = ParentMap.MapTransform.Transform(new Point(bounds.X, bounds.Y));
|
|
|
|
|
|
var end = ParentMap.MapTransform.Transform(new Point(bounds.X + bounds.Width, bounds.Y + bounds.Height));
|
2015-11-11 19:48:50 +01:00
|
|
|
|
var lineDistance = GetLineDistance();
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
var labelStart = new Location(
|
2015-11-11 19:48:50 +01:00
|
|
|
|
Math.Ceiling(start.Latitude / lineDistance) * lineDistance,
|
|
|
|
|
|
Math.Ceiling(start.Longitude / lineDistance) * lineDistance);
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
var labelEnd = new Location(
|
2015-11-11 19:48:50 +01:00
|
|
|
|
Math.Floor(end.Latitude / lineDistance) * lineDistance,
|
|
|
|
|
|
Math.Floor(end.Longitude / lineDistance) * lineDistance);
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
var lineStart = new Location(
|
2015-11-11 19:48:50 +01:00
|
|
|
|
Math.Min(Math.Max(labelStart.Latitude - lineDistance, -ParentMap.MapTransform.MaxLatitude), ParentMap.MapTransform.MaxLatitude),
|
|
|
|
|
|
labelStart.Longitude - lineDistance);
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
var lineEnd = new Location(
|
2015-11-11 19:48:50 +01:00
|
|
|
|
Math.Min(Math.Max(labelEnd.Latitude + lineDistance, -ParentMap.MapTransform.MaxLatitude), ParentMap.MapTransform.MaxLatitude),
|
|
|
|
|
|
labelEnd.Longitude + lineDistance);
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
if (!lineStart.Equals(graticuleStart) || !lineEnd.Equals(graticuleEnd))
|
2013-04-12 19:59:16 +02:00
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
graticuleStart = lineStart;
|
|
|
|
|
|
graticuleEnd = lineEnd;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2014-07-09 21:27:28 +02:00
|
|
|
|
var geometry = (PathGeometry)path.Data;
|
2014-07-01 18:57:44 +02:00
|
|
|
|
geometry.Figures.Clear();
|
|
|
|
|
|
geometry.Transform = ParentMap.ViewportTransform;
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2015-11-11 19:48:50 +01:00
|
|
|
|
for (var lat = labelStart.Latitude; lat <= end.Latitude; lat += lineDistance)
|
2013-04-12 19:59:16 +02:00
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
var figure = new PathFigure
|
|
|
|
|
|
{
|
2014-07-09 21:27:28 +02:00
|
|
|
|
StartPoint = ParentMap.MapTransform.Transform(new Location(lat, lineStart.Longitude)),
|
2014-07-01 18:57:44 +02:00
|
|
|
|
IsClosed = false,
|
|
|
|
|
|
IsFilled = false
|
|
|
|
|
|
};
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
figure.Segments.Add(new LineSegment
|
|
|
|
|
|
{
|
2014-07-09 21:27:28 +02:00
|
|
|
|
Point = ParentMap.MapTransform.Transform(new Location(lat, lineEnd.Longitude)),
|
2014-07-01 18:57:44 +02:00
|
|
|
|
});
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
geometry.Figures.Add(figure);
|
|
|
|
|
|
}
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2015-11-11 19:48:50 +01:00
|
|
|
|
for (var lon = labelStart.Longitude; lon <= end.Longitude; lon += lineDistance)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
var figure = new PathFigure
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
StartPoint = ParentMap.MapTransform.Transform(new Location(lineStart.Latitude, lon)),
|
|
|
|
|
|
IsClosed = false,
|
|
|
|
|
|
IsFilled = false
|
|
|
|
|
|
};
|
2013-11-18 19:49:17 +01:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
figure.Segments.Add(new LineSegment
|
|
|
|
|
|
{
|
|
|
|
|
|
Point = ParentMap.MapTransform.Transform(new Location(lineEnd.Latitude, lon)),
|
|
|
|
|
|
});
|
2013-11-18 19:49:17 +01:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
geometry.Figures.Add(figure);
|
|
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2015-11-11 19:48:50 +01:00
|
|
|
|
var labelFormat = GetLabelFormat(lineDistance);
|
2014-07-01 18:57:44 +02:00
|
|
|
|
var childIndex = 1; // 0 for Path
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2015-11-11 19:48:50 +01:00
|
|
|
|
for (var lat = labelStart.Latitude; lat <= end.Latitude; lat += lineDistance)
|
2014-07-01 18:57:44 +02:00
|
|
|
|
{
|
2015-11-11 19:48:50 +01:00
|
|
|
|
for (var lon = labelStart.Longitude; lon <= end.Longitude; lon += lineDistance)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
TextBlock label;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
if (childIndex < Children.Count)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
label = (TextBlock)Children[childIndex];
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2014-07-09 21:27:28 +02:00
|
|
|
|
var renderTransform = new TransformGroup();
|
|
|
|
|
|
renderTransform.Children.Add(new TranslateTransform());
|
|
|
|
|
|
renderTransform.Children.Add(ParentMap.RotateTransform);
|
|
|
|
|
|
renderTransform.Children.Add(new TranslateTransform());
|
|
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
label = new TextBlock
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2014-07-09 21:27:28 +02:00
|
|
|
|
RenderTransform = renderTransform
|
2014-07-01 18:57:44 +02:00
|
|
|
|
};
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
label.SetBinding(TextBlock.ForegroundProperty, new Binding
|
|
|
|
|
|
{
|
|
|
|
|
|
Source = this,
|
|
|
|
|
|
Path = new PropertyPath("Foreground")
|
|
|
|
|
|
});
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
Children.Add(label);
|
|
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
childIndex++;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
if (FontFamily != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
label.FontFamily = FontFamily;
|
|
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2014-07-01 18:57:44 +02:00
|
|
|
|
label.FontSize = FontSize;
|
|
|
|
|
|
label.FontStyle = FontStyle;
|
|
|
|
|
|
label.FontStretch = FontStretch;
|
|
|
|
|
|
label.FontWeight = FontWeight;
|
2015-11-11 19:48:50 +01:00
|
|
|
|
label.Text = GetLabelText(lat, labelFormat, "NS") + "\n" + GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW");
|
2014-07-09 21:27:28 +02:00
|
|
|
|
label.Tag = new Location(lat, lon);
|
|
|
|
|
|
label.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2014-07-09 21:27:28 +02:00
|
|
|
|
var translateTransform = (TranslateTransform)((TransformGroup)label.RenderTransform).Children[0];
|
2014-07-01 18:57:44 +02:00
|
|
|
|
translateTransform.X = StrokeThickness / 2d + 2d;
|
|
|
|
|
|
translateTransform.Y = -label.DesiredSize.Height / 2d;
|
2013-04-12 19:59:16 +02:00
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
2014-07-01 18:57:44 +02:00
|
|
|
|
|
|
|
|
|
|
while (Children.Count > childIndex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Children.RemoveAt(Children.Count - 1);
|
|
|
|
|
|
}
|
2013-04-12 19:59:16 +02:00
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2014-07-09 21:27:28 +02:00
|
|
|
|
// don't use MapPanel.Location because labels may be at more than 180° distance from map center
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < Children.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
var label = (TextBlock)Children[i];
|
|
|
|
|
|
var location = (Location)label.Tag;
|
|
|
|
|
|
var viewportTransform = (TranslateTransform)((TransformGroup)label.RenderTransform).Children[2];
|
|
|
|
|
|
var viewportPosition = ParentMap.LocationToViewportPoint(location);
|
|
|
|
|
|
viewportTransform.X = viewportPosition.X;
|
|
|
|
|
|
viewportTransform.Y = viewportPosition.Y;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-22 21:42:29 +01:00
|
|
|
|
base.OnViewportChanged();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|