2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2018-02-09 17:43:47 +01:00
|
|
|
|
// © 2018 Clemens Fischer
|
2012-11-22 21:42:29 +01:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2014-07-01 18:57:44 +02:00
|
|
|
|
public partial class MapGraticule
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2016-08-08 20:36:02 +02:00
|
|
|
|
private Path path;
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
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;
|
2016-08-08 20:36:02 +02:00
|
|
|
|
}
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2017-02-05 18:35:30 +01:00
|
|
|
|
protected override void OnViewportChanged(ViewportChangedEventArgs e)
|
2016-08-08 20:36:02 +02:00
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
var projection = ParentMap.MapProjection;
|
|
|
|
|
|
|
2018-02-09 17:43:47 +01:00
|
|
|
|
if (!projection.IsAzimuthal)
|
2012-11-22 21:42:29 +01:00
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
if (path == null)
|
2016-08-08 20:36:02 +02:00
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
path = new Path
|
2016-08-08 20:36:02 +02:00
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
Data = new PathGeometry()
|
|
|
|
|
|
};
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2018-04-30 23:13:50 +02:00
|
|
|
|
SetStrokeBindings(path);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
Children.Add(path);
|
|
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
var bounds = projection.ViewportRectToBoundingBox(new Rect(0d, 0d, ParentMap.RenderSize.Width, ParentMap.RenderSize.Height));
|
|
|
|
|
|
var lineDistance = GetLineDistance();
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
var labelStart = new Location(
|
|
|
|
|
|
Math.Ceiling(bounds.South / lineDistance) * lineDistance,
|
|
|
|
|
|
Math.Ceiling(bounds.West / lineDistance) * lineDistance);
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
var labelEnd = new Location(
|
|
|
|
|
|
Math.Floor(bounds.North / lineDistance) * lineDistance,
|
|
|
|
|
|
Math.Floor(bounds.East / lineDistance) * lineDistance);
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
var lineStart = new Location(
|
|
|
|
|
|
Math.Min(Math.Max(labelStart.Latitude - lineDistance, -projection.MaxLatitude), projection.MaxLatitude),
|
|
|
|
|
|
labelStart.Longitude - lineDistance);
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
var lineEnd = new Location(
|
|
|
|
|
|
Math.Min(Math.Max(labelEnd.Latitude + lineDistance, -projection.MaxLatitude), projection.MaxLatitude),
|
|
|
|
|
|
labelEnd.Longitude + lineDistance);
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2018-03-06 22:22:58 +01:00
|
|
|
|
var geometry = (PathGeometry)path.Data;
|
|
|
|
|
|
geometry.Figures.Clear();
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2018-03-06 22:22:58 +01:00
|
|
|
|
for (var lat = labelStart.Latitude; lat <= bounds.North; lat += lineDistance)
|
|
|
|
|
|
{
|
|
|
|
|
|
var figure = new PathFigure
|
2014-07-01 18:57:44 +02:00
|
|
|
|
{
|
2018-03-06 22:22:58 +01:00
|
|
|
|
StartPoint = projection.LocationToViewportPoint(new Location(lat, lineStart.Longitude)),
|
|
|
|
|
|
IsClosed = false,
|
|
|
|
|
|
IsFilled = false
|
|
|
|
|
|
};
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2018-03-06 22:22:58 +01:00
|
|
|
|
figure.Segments.Add(new LineSegment
|
|
|
|
|
|
{
|
|
|
|
|
|
Point = projection.LocationToViewportPoint(new Location(lat, lineEnd.Longitude))
|
|
|
|
|
|
});
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2018-03-06 22:22:58 +01:00
|
|
|
|
geometry.Figures.Add(figure);
|
|
|
|
|
|
}
|
2013-11-18 19:49:17 +01:00
|
|
|
|
|
2018-03-06 22:22:58 +01:00
|
|
|
|
for (var lon = labelStart.Longitude; lon <= bounds.East; lon += lineDistance)
|
|
|
|
|
|
{
|
|
|
|
|
|
var figure = new PathFigure
|
2014-07-01 18:57:44 +02:00
|
|
|
|
{
|
2018-03-06 22:22:58 +01:00
|
|
|
|
StartPoint = projection.LocationToViewportPoint(new Location(lineStart.Latitude, lon)),
|
|
|
|
|
|
IsClosed = false,
|
|
|
|
|
|
IsFilled = false
|
|
|
|
|
|
};
|
2013-11-18 19:49:17 +01:00
|
|
|
|
|
2018-03-06 22:22:58 +01:00
|
|
|
|
figure.Segments.Add(new LineSegment
|
|
|
|
|
|
{
|
|
|
|
|
|
Point = projection.LocationToViewportPoint(new Location(lineEnd.Latitude, lon))
|
|
|
|
|
|
});
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2018-03-06 22:22:58 +01:00
|
|
|
|
geometry.Figures.Add(figure);
|
|
|
|
|
|
}
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2018-03-06 22:22:58 +01:00
|
|
|
|
var labelFormat = GetLabelFormat(lineDistance);
|
|
|
|
|
|
var childIndex = 1; // 0 for Path
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2018-03-06 22:22:58 +01:00
|
|
|
|
for (var lat = labelStart.Latitude; lat <= bounds.North; lat += lineDistance)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (var lon = labelStart.Longitude; lon <= bounds.East; lon += lineDistance)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2018-03-06 22:22:58 +01:00
|
|
|
|
TextBlock label;
|
|
|
|
|
|
|
|
|
|
|
|
if (childIndex < Children.Count)
|
|
|
|
|
|
{
|
|
|
|
|
|
label = (TextBlock)Children[childIndex];
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2014-07-01 18:57:44 +02:00
|
|
|
|
{
|
2018-03-06 22:22:58 +01:00
|
|
|
|
var renderTransform = new TransformGroup();
|
|
|
|
|
|
renderTransform.Children.Add(new TranslateTransform());
|
|
|
|
|
|
renderTransform.Children.Add(ParentMap.RotateTransform);
|
|
|
|
|
|
renderTransform.Children.Add(new TranslateTransform());
|
2014-07-09 21:27:28 +02:00
|
|
|
|
|
2018-03-06 22:22:58 +01:00
|
|
|
|
label = new TextBlock
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2018-03-06 22:22:58 +01:00
|
|
|
|
RenderTransform = renderTransform
|
|
|
|
|
|
};
|
2013-04-12 19:59:16 +02:00
|
|
|
|
|
2018-03-29 19:35:52 +02:00
|
|
|
|
label.SetBinding(TextBlock.ForegroundProperty, new Binding
|
|
|
|
|
|
{
|
|
|
|
|
|
Source = this,
|
|
|
|
|
|
Path = new PropertyPath("Foreground")
|
|
|
|
|
|
});
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2018-03-06 22:22:58 +01:00
|
|
|
|
Children.Add(label);
|
|
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2018-03-06 22:22:58 +01:00
|
|
|
|
childIndex++;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2018-03-06 22:22:58 +01:00
|
|
|
|
if (FontFamily != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
label.FontFamily = FontFamily;
|
2014-07-01 18:57:44 +02:00
|
|
|
|
}
|
2018-03-06 22:22:58 +01:00
|
|
|
|
|
|
|
|
|
|
label.FontSize = FontSize;
|
|
|
|
|
|
label.FontStyle = FontStyle;
|
|
|
|
|
|
label.FontStretch = FontStretch;
|
|
|
|
|
|
label.FontWeight = FontWeight;
|
|
|
|
|
|
label.Text = GetLabelText(lat, labelFormat, "NS") + "\n" + GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW");
|
|
|
|
|
|
label.Tag = new Location(lat, lon);
|
|
|
|
|
|
label.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
|
|
|
|
|
|
|
|
|
|
|
|
var translateTransform = (TranslateTransform)((TransformGroup)label.RenderTransform).Children[0];
|
|
|
|
|
|
translateTransform.X = StrokeThickness / 2d + 2d;
|
|
|
|
|
|
translateTransform.Y = -label.DesiredSize.Height / 2d;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
2012-11-22 21:42:29 +01:00
|
|
|
|
|
2017-06-25 23:05:48 +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-01 18:57:44 +02:00
|
|
|
|
|
2017-06-25 23:05:48 +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++)
|
2014-07-01 18:57:44 +02:00
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
var label = (TextBlock)Children[i];
|
|
|
|
|
|
var location = (Location)label.Tag;
|
|
|
|
|
|
var viewportTransform = (TranslateTransform)((TransformGroup)label.RenderTransform).Children[2];
|
|
|
|
|
|
var viewportPosition = projection.LocationToViewportPoint(location);
|
|
|
|
|
|
viewportTransform.X = viewportPosition.X;
|
|
|
|
|
|
viewportTransform.Y = viewportPosition.Y;
|
2014-07-01 18:57:44 +02:00
|
|
|
|
}
|
2013-04-12 19:59:16 +02:00
|
|
|
|
}
|
2017-06-25 23:05:48 +02:00
|
|
|
|
else if (path != null)
|
2014-07-09 21:27:28 +02:00
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
path = null;
|
|
|
|
|
|
Children.Clear();
|
2014-07-09 21:27:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-05 18:35:30 +01:00
|
|
|
|
base.OnViewportChanged(e);
|
2012-11-22 21:42:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|