General map graticule for WPF

This commit is contained in:
Clemens 2022-03-04 22:28:18 +01:00
parent c998f19fe5
commit a07948be02
11 changed files with 414 additions and 122 deletions

View file

@ -31,7 +31,7 @@ namespace MapControl
var width = rect.Width / Wgs84MeterPerDegree;
var height = rect.Height / Wgs84MeterPerDegree;
return new CenteredBoundingBox(center, width, height);
return center != null ? new CenteredBoundingBox(center, width, height) : null;
}
/// <summary>

View file

@ -279,7 +279,7 @@ namespace MapControl
public void SetTransformCenter(Point center)
{
transformCenter = ViewToLocation(center);
viewCenter = center;
viewCenter = transformCenter != null ? center : new Point(RenderSize.Width / 2d, RenderSize.Height / 2d);
}
/// <summary>
@ -304,7 +304,11 @@ namespace MapControl
if (translation.X != 0d || translation.Y != 0d)
{
Center = ViewToLocation(viewCenter - translation);
var center = ViewToLocation(viewCenter - translation);
if (center != null)
{
Center = center;
}
}
}
@ -317,8 +321,8 @@ namespace MapControl
{
if (rotation != 0d || scale != 1d)
{
transformCenter = ViewToLocation(center);
viewCenter = center + translation;
SetTransformCenter(center);
viewCenter += translation;
if (rotation != 0d)
{
@ -367,11 +371,16 @@ namespace MapControl
{
var rect = MapProjection.BoundingBoxToRect(boundingBox);
var center = new Point(rect.X + rect.Width / 2d, rect.Y + rect.Height / 2d);
var scale = Math.Min(RenderSize.Width / rect.Width, RenderSize.Height / rect.Height);
var targetCenter = MapProjection.MapToLocation(center);
TargetZoomLevel = ViewTransform.ScaleToZoomLevel(scale);
TargetCenter = MapProjection.MapToLocation(center);
TargetHeading = 0d;
if (targetCenter != null)
{
var scale = Math.Min(RenderSize.Width / rect.Width, RenderSize.Height / rect.Height);
TargetZoomLevel = ViewTransform.ScaleToZoomLevel(scale);
TargetCenter = targetCenter;
TargetHeading = 0d;
}
}
internal double ConstrainedLongitude(double longitude)
@ -700,46 +709,58 @@ namespace MapControl
private void UpdateTransform(bool resetTransformCenter = false, bool projectionChanged = false)
{
var viewScale = ViewTransform.ZoomLevelToScale(ZoomLevel);
var center = transformCenter ?? Center;
var projection = MapProjection;
projection.Center = ProjectionCenter ?? Center;
ViewTransform.SetTransform(projection.LocationToMap(center), viewCenter, viewScale, Heading);
var mapCenter = projection.LocationToMap(transformCenter ?? Center);
if (transformCenter != null)
if (MapProjection.IsValid(mapCenter))
{
center = ViewToLocation(new Point(RenderSize.Width / 2d, RenderSize.Height / 2d));
center.Longitude = Location.NormalizeLongitude(center.Longitude);
ViewTransform.SetTransform(mapCenter, viewCenter, viewScale, Heading);
if (center.Latitude < -projection.MaxLatitude || center.Latitude > projection.MaxLatitude)
if (transformCenter != null)
{
center.Latitude = Math.Min(Math.Max(center.Latitude, -projection.MaxLatitude), projection.MaxLatitude);
resetTransformCenter = true;
var center = ViewToLocation(new Point(RenderSize.Width / 2d, RenderSize.Height / 2d));
if (center != null)
{
center.Longitude = Location.NormalizeLongitude(center.Longitude);
if (center.Latitude < -projection.MaxLatitude || center.Latitude > projection.MaxLatitude)
{
center.Latitude = Math.Min(Math.Max(center.Latitude, -projection.MaxLatitude), projection.MaxLatitude);
resetTransformCenter = true;
}
SetValueInternal(CenterProperty, center);
if (centerAnimation == null)
{
SetValueInternal(TargetCenterProperty, center);
}
if (resetTransformCenter)
{
ResetTransformCenter();
projection.Center = ProjectionCenter ?? center;
mapCenter = projection.LocationToMap(center);
if (MapProjection.IsValid(mapCenter))
{
ViewTransform.SetTransform(mapCenter, viewCenter, viewScale, Heading);
}
}
}
}
SetValueInternal(CenterProperty, center);
SetViewScale(ViewTransform.Scale);
if (centerAnimation == null)
{
SetValueInternal(TargetCenterProperty, center);
}
OnViewportChanged(new ViewportChangedEventArgs(projectionChanged, Center.Longitude - centerLongitude));
if (resetTransformCenter)
{
ResetTransformCenter();
projection.Center = ProjectionCenter ?? center;
ViewTransform.SetTransform(projection.LocationToMap(center), viewCenter, viewScale, Heading);
}
centerLongitude = Center.Longitude;
}
SetViewScale(ViewTransform.Scale);
OnViewportChanged(new ViewportChangedEventArgs(projectionChanged, Center.Longitude - centerLongitude));
centerLongitude = Center.Longitude;
}
protected override void OnViewportChanged(ViewportChangedEventArgs e)

View file

@ -33,15 +33,9 @@ namespace MapControl
private double GetLineDistance()
{
var pixelPerDegree = ParentMap.ViewTransform.Scale * MapProjection.Wgs84MeterPerDegree;
var minDistance = MinLineDistance / pixelPerDegree;
var scale = 1d;
if (minDistance < 1d)
{
scale = minDistance < 1d / 60d ? 3600d : 60d;
minDistance *= scale;
}
var minDistance = MinLineDistance / PixelPerLongitudeDegree(ParentMap.Center);
var scale = minDistance < 1d / 60d ? 3600d : minDistance < 1d ? 60d : 1d;
minDistance *= scale;
var lineDistances = new double[] { 1d, 2d, 5d, 10d, 15d, 30d, 60d };
var i = 0;
@ -51,7 +45,14 @@ namespace MapControl
i++;
}
return lineDistances[i] / scale;
return Math.Min(lineDistances[i] / scale, 30d);
}
private double PixelPerLongitudeDegree(Location location)
{
return Math.Max(1d, // a reasonable lower limit
ParentMap.GetScale(location).X *
Math.Cos(location.Latitude * Math.PI / 180d) * MapProjection.Wgs84MeterPerDegree);
}
private static string GetLabelFormat(double lineDistance)
@ -64,6 +65,8 @@ namespace MapControl
{
var hemisphere = hemispheres[0];
value = (value + 540d) % 360d - 180d;
if (value < -1e-8) // ~1mm
{
value = -value;

View file

@ -147,9 +147,11 @@ namespace MapControl
if (parentMap.MapProjection.IsNormalCylindrical && IsOutsideViewport(position))
{
var location = parentMap.MapProjection.MapToLocation(center);
location.Longitude = parentMap.ConstrainedLongitude(location.Longitude);
position = parentMap.LocationToView(location);
if (location != null)
{
location.Longitude = parentMap.ConstrainedLongitude(location.Longitude);
position = parentMap.LocationToView(location);
}
}
var width = rect.Width * parentMap.ViewTransform.Scale;

View file

@ -64,6 +64,7 @@ namespace MapControl
/// <summary>
/// Transforms a Point in cartesian map coordinates to a Location in geographic coordinates.
/// Returns null when the Point can not be transformed.
/// </summary>
public abstract Location MapToLocation(Point point);
@ -79,13 +80,16 @@ namespace MapControl
/// <summary>
/// Transforms a Rect in cartesian map coordinates to a BoundingBox in geographic coordinates.
/// Returns null when the Rect can not be transformed.
/// </summary>
public virtual BoundingBox RectToBoundingBox(Rect rect)
{
var sw = MapToLocation(new Point(rect.X, rect.Y));
var ne = MapToLocation(new Point(rect.X + rect.Width, rect.Y + rect.Height));
return new BoundingBox(sw.Latitude, sw.Longitude, ne.Latitude, ne.Longitude);
return sw != null && ne != null
? new BoundingBox(sw.Latitude, sw.Longitude, ne.Latitude, ne.Longitude)
: null;
}
/// <summary>
@ -106,5 +110,25 @@ namespace MapControl
return string.Format(CultureInfo.InvariantCulture,
"{0},{1},{2},{3}", rect.X, rect.Y, (rect.X + rect.Width), (rect.Y + rect.Height));
}
/// <summary>
/// Checks if the X and Y values of a Point are neither NaN nor Infinity.
/// </summary>
public static bool IsValid(Point point)
{
return !double.IsNaN(point.X) && !double.IsInfinity(point.X) &&
!double.IsNaN(point.Y) && !double.IsInfinity(point.Y);
}
/// <summary>
/// Checks if the X, Y, Width and Height values of a Rect are neither NaN nor Infinity.
/// </summary>
public static bool IsValid(Rect rect)
{
return !double.IsNaN(rect.X) && !double.IsInfinity(rect.X) &&
!double.IsNaN(rect.Y) && !double.IsInfinity(rect.Y) &&
!double.IsNaN(rect.Width) && !double.IsInfinity(rect.Width) &&
!double.IsNaN(rect.Height) && !double.IsInfinity(rect.Height);
}
}
}

View file

@ -33,6 +33,11 @@ namespace MapControl
var lat = location.Latitude * Math.PI / 180d;
var dLon = (location.Longitude - Center.Longitude) * Math.PI / 180d;
if (Math.Abs(lat - lat0) > Math.PI / 2d || Math.Abs(dLon) > Math.PI / 2d)
{
return new Point(double.NaN, double.NaN);
}
return new Point(
Wgs84EquatorialRadius * Math.Cos(lat) * Math.Sin(dLon),
Wgs84EquatorialRadius * (Math.Cos(lat0) * Math.Sin(lat) - Math.Sin(lat0) * Math.Cos(lat) * Math.Cos(dLon)));
@ -51,7 +56,7 @@ namespace MapControl
if (r2 > 1d)
{
return new Location(double.NaN, double.NaN);
return null;
}
var r = Math.Sqrt(r2);

View file

@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Media;
@ -12,6 +13,8 @@ namespace MapControl
{
public partial class MapGraticule
{
private const double LineInterpolationResolution = 2d;
private class Label
{
public readonly double Position;
@ -40,29 +43,30 @@ namespace MapControl
if (projection != null)
{
var lineDistance = GetLineDistance();
var labelFormat = GetLabelFormat(lineDistance);
if (projection.IsNormalCylindrical)
{
DrawCylindricalGraticule(drawingContext, lineDistance, labelFormat);
DrawCylindricalGraticule(drawingContext);
}
else
{
DrawGraticule(drawingContext);
}
}
}
private void DrawCylindricalGraticule(DrawingContext drawingContext, double lineDistance, string labelFormat)
private void DrawCylindricalGraticule(DrawingContext drawingContext)
{
var path = new PathGeometry();
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
var pixelsPerDip = VisualTreeHelper.GetDpi(this).PixelsPerDip;
var lineDistance = GetLineDistance();
var labelFormat = GetLabelFormat(lineDistance);
var boundingBox = ParentMap.ViewRectToBoundingBox(new Rect(ParentMap.RenderSize));
var latLabelStart = Math.Ceiling(boundingBox.South / lineDistance) * lineDistance;
var lonLabelStart = Math.Ceiling(boundingBox.West / lineDistance) * lineDistance;
var latLabels = new List<Label>((int)((boundingBox.North - latLabelStart) / lineDistance) + 1);
var lonLabels = new List<Label>((int)((boundingBox.East - lonLabelStart) / lineDistance) + 1);
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
var pixelsPerDip = VisualTreeHelper.GetDpi(this).PixelsPerDip;
var pen = CreatePen();
for (var lat = latLabelStart; lat <= boundingBox.North; lat += lineDistance)
{
@ -70,9 +74,15 @@ namespace MapControl
GetLabelText(lat, labelFormat, "NS"),
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip)));
drawingContext.DrawLine(pen,
ParentMap.LocationToView(new Location(lat, boundingBox.West)),
ParentMap.LocationToView(new Location(lat, boundingBox.East)));
var p1 = ParentMap.LocationToView(new Location(lat, boundingBox.West));
var p2 = ParentMap.LocationToView(new Location(lat, boundingBox.East));
if (MapProjection.IsValid(p1) && MapProjection.IsValid(p2))
{
var figure = new PathFigure { StartPoint = p1 };
figure.Segments.Add(new LineSegment(p2, true));
path.Figures.Add(figure);
}
}
for (var lon = lonLabelStart; lon <= boundingBox.East; lon += lineDistance)
@ -81,23 +91,224 @@ namespace MapControl
GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW"),
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip)));
drawingContext.DrawLine(pen,
ParentMap.LocationToView(new Location(boundingBox.South, lon)),
ParentMap.LocationToView(new Location(boundingBox.North, lon)));
var p1 = ParentMap.LocationToView(new Location(boundingBox.South, lon));
var p2 = ParentMap.LocationToView(new Location(boundingBox.North, lon));
if (MapProjection.IsValid(p1) && MapProjection.IsValid(p2))
{
var figure = new PathFigure { StartPoint = p1 };
figure.Segments.Add(new LineSegment(p2, true));
path.Figures.Add(figure);
}
}
drawingContext.DrawGeometry(null, CreatePen(), path);
foreach (var latLabel in latLabels)
{
foreach (var lonLabel in lonLabels)
{
var position = ParentMap.LocationToView(new Location(latLabel.Position, lonLabel.Position));
drawingContext.PushTransform(new RotateTransform(ParentMap.ViewTransform.Rotation, position.X, position.Y));
drawingContext.DrawText(latLabel.Text,
new Point(position.X + StrokeThickness / 2d + 2d, position.Y - StrokeThickness / 2d - latLabel.Text.Height));
drawingContext.DrawText(lonLabel.Text,
new Point(position.X + StrokeThickness / 2d + 2d, position.Y + StrokeThickness / 2d));
drawingContext.Pop();
if (MapProjection.IsValid(position))
{
DrawLabel(drawingContext, latLabel.Text, lonLabel.Text, position, ParentMap.ViewTransform.Rotation);
}
}
}
}
private void DrawGraticule(DrawingContext drawingContext)
{
var path = new PathGeometry();
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
var pixelsPerDip = VisualTreeHelper.GetDpi(this).PixelsPerDip;
var lineDistance = GetLineDistance();
var labelFormat = GetLabelFormat(lineDistance);
var centerLon = Math.Floor(ParentMap.Center.Longitude / lineDistance) * lineDistance;
var minLon = centerLon - lineDistance;
var maxLon = centerLon + lineDistance;
var minLat = 0d;
var maxLat = 0d;
GetLatitudeRange(lineDistance, ref minLat, ref maxLat);
var latSegments = (int)Math.Round(Math.Abs(maxLat - minLat) / lineDistance);
var interpolationCount = Math.Max(1, (int)Math.Ceiling(lineDistance / LineInterpolationResolution));
var interpolationDistance = lineDistance / interpolationCount;
var latPoints = latSegments * interpolationCount;
if (DrawMeridian(path.Figures, centerLon, minLat, interpolationDistance, latPoints))
{
while (minLon > centerLon - 180d &&
DrawMeridian(path.Figures, minLon, minLat, interpolationDistance, latPoints))
{
minLon -= lineDistance;
}
while (maxLon <= centerLon + 180d &&
DrawMeridian(path.Figures, maxLon, minLat, interpolationDistance, latPoints))
{
maxLon += lineDistance;
}
}
var lonSegments = (int)Math.Round(Math.Abs(maxLon - minLon) / lineDistance);
for (var s = minLat > -90d ? 0 : 1; s < latSegments; s++)
{
var lat = minLat + s * lineDistance;
var lon = minLon;
var points = new List<Point>();
var p = ParentMap.LocationToView(new Location(lat, lon));
if (MapProjection.IsValid(p))
{
points.Add(p);
}
for (int i = 0; i < lonSegments; i++)
{
for (int j = 1; j <= interpolationCount; j++)
{
lon = minLon + i * lineDistance + j * interpolationDistance;
p = ParentMap.LocationToView(new Location(lat, lon));
if (MapProjection.IsValid(p))
{
points.Add(p);
}
}
if (p.X >= 0d && p.X <= ParentMap.RenderSize.Width &&
p.Y >= 0d && p.Y <= ParentMap.RenderSize.Height)
{
DrawLabel(drawingContext, typeface, pixelsPerDip, p, new Location(lat, lon), labelFormat);
}
}
if (points.Count >= 2)
{
var figure = new PathFigure { StartPoint = points.First() };
figure.Segments.Add(new PolyLineSegment(points.Skip(1), true));
path.Figures.Add(figure);
}
}
drawingContext.DrawGeometry(null, CreatePen(), path);
}
private bool DrawMeridian(PathFigureCollection figures,
double longitude, double startLatitude, double deltaLatitude, int numPoints)
{
var points = new List<Point>();
var visible = false;
for (int i = 0; i <= numPoints; i++)
{
var p = ParentMap.LocationToView(new Location(startLatitude + i * deltaLatitude, longitude));
if (MapProjection.IsValid(p))
{
visible = visible ||
p.X >= 0d && p.X <= ParentMap.RenderSize.Width &&
p.Y >= 0d && p.Y <= ParentMap.RenderSize.Height;
points.Add(p);
}
}
if (points.Count >= 2)
{
var figure = new PathFigure { StartPoint = points.First() };
figure.Segments.Add(new PolyLineSegment(points.Skip(1), true));
figures.Add(figure);
}
return visible;
}
private void DrawLabel(DrawingContext drawingContext, Typeface typeface, double pixelsPerDip,
Point position, Location location, string labelFormat)
{
var latText = new FormattedText(GetLabelText(location.Latitude, labelFormat, "NS"),
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip);
var lonText = new FormattedText(GetLabelText(location.Longitude, labelFormat, "EW"),
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip);
location.Longitude += latText.Width / PixelPerLongitudeDegree(location);
var p = ParentMap.LocationToView(location);
if (MapProjection.IsValid(p))
{
DrawLabel(drawingContext, latText, lonText, position, Vector.AngleBetween(new Vector(1d, 0d), p - position));
}
}
private void DrawLabel(DrawingContext drawingContext,
FormattedText latitudeLabel, FormattedText longitudeLabel, Point position, double rotation)
{
var x = position.X + StrokeThickness / 2d + 2d;
var y1 = position.Y - StrokeThickness / 2d - latitudeLabel.Height;
var y2 = position.Y + StrokeThickness / 2d;
drawingContext.PushTransform(new RotateTransform(rotation, position.X, position.Y));
drawingContext.DrawText(latitudeLabel, new Point(x, y1));
drawingContext.DrawText(longitudeLabel, new Point(x, y2));
drawingContext.Pop();
}
private void GetLatitudeRange(double lineDistance, ref double minLatitude, ref double maxLatitude)
{
var width = ParentMap.RenderSize.Width;
var height = ParentMap.RenderSize.Height;
var northPole = ParentMap.LocationToView(new Location(90d, 0d));
var southPole = ParentMap.LocationToView(new Location(-90d, 0d));
if (northPole.X >= 0d && northPole.Y >= 0d && northPole.X <= width && northPole.Y <= height)
{
maxLatitude = 90d;
}
if (southPole.X >= 0d && southPole.Y >= 0d && southPole.X <= width && southPole.Y <= height)
{
minLatitude = -90d;
}
if (minLatitude > -90d || maxLatitude < 90d)
{
var locations = new Location[]
{
ParentMap.ViewToLocation(new Point(0d, 0d)),
ParentMap.ViewToLocation(new Point(width / 2d, 0d)),
ParentMap.ViewToLocation(new Point(width, 0d)),
ParentMap.ViewToLocation(new Point(width, height / 2d)),
ParentMap.ViewToLocation(new Point(width, height)),
ParentMap.ViewToLocation(new Point(width / 2d, height)),
ParentMap.ViewToLocation(new Point(0d, height)),
ParentMap.ViewToLocation(new Point(0d, height / 2)),
};
var latitudes = locations.Where(loc => loc != null).Select(loc => loc.Latitude);
var south = -90d;
var north = 90d;
if (latitudes.Distinct().Count() >= 2)
{
south = latitudes.Min();
north = latitudes.Max();
}
if (minLatitude > -90d)
{
minLatitude = Math.Max(Math.Floor(south / lineDistance) * lineDistance, -90d);
}
if (maxLatitude < 90d)
{
maxLatitude = Math.Min(Math.Ceiling(north / lineDistance) * lineDistance, 90d);
}
}
}

View file

@ -94,7 +94,7 @@ namespace MapControl.Projections
{
IsNormalCylindrical = true;
IsWebMercator = false;
scaleFactor = Wgs84MetersPerDegree;
scaleFactor = Wgs84MeterPerDegree;
bboxFormat = "{1},{0},{3},{2}";
}
}

View file

@ -86,28 +86,37 @@ namespace SampleApplication
private void MapPointerMoved(object sender, PointerRoutedEventArgs e)
{
var location = map.ViewToLocation(e.GetCurrentPoint(map).Position);
var latitude = (int)Math.Round(location.Latitude * 60000d);
var longitude = (int)Math.Round(Location.NormalizeLongitude(location.Longitude) * 60000d);
var latHemisphere = 'N';
var lonHemisphere = 'E';
if (latitude < 0)
if (location != null)
{
latitude = -latitude;
latHemisphere = 'S';
}
var latitude = (int)Math.Round(location.Latitude * 60000d);
var longitude = (int)Math.Round(Location.NormalizeLongitude(location.Longitude) * 60000d);
var latHemisphere = 'N';
var lonHemisphere = 'E';
if (longitude < 0)
if (latitude < 0)
{
latitude = -latitude;
latHemisphere = 'S';
}
if (longitude < 0)
{
longitude = -longitude;
lonHemisphere = 'W';
}
mouseLocation.Text = string.Format(CultureInfo.InvariantCulture,
"{0} {1:00} {2:00.000}\n{3} {4:000} {5:00.000}",
latHemisphere, latitude / 60000, (latitude % 60000) / 1000d,
lonHemisphere, longitude / 60000, (longitude % 60000) / 1000d);
mouseLocation.Visibility = Visibility.Visible;
}
else
{
longitude = -longitude;
lonHemisphere = 'W';
mouseLocation.Visibility = Visibility.Collapsed;
mouseLocation.Text = string.Empty;
}
mouseLocation.Text = string.Format(CultureInfo.InvariantCulture,
"{0} {1:00} {2:00.000}\n{3} {4:000} {5:00.000}",
latHemisphere, latitude / 60000, (latitude % 60000) / 1000d,
lonHemisphere, longitude / 60000, (longitude % 60000) / 1000d);
mouseLocation.Visibility = Visibility.Visible;
}
private void MapPointerExited(object sender, PointerRoutedEventArgs e)

View file

@ -103,28 +103,37 @@ namespace SampleApplication
private void MapPointerMoved(object sender, PointerRoutedEventArgs e)
{
var location = map.ViewToLocation(e.GetCurrentPoint(map).Position);
var latitude = (int)Math.Round(location.Latitude * 60000d);
var longitude = (int)Math.Round(Location.NormalizeLongitude(location.Longitude) * 60000d);
var latHemisphere = 'N';
var lonHemisphere = 'E';
if (latitude < 0)
if (location != null)
{
latitude = -latitude;
latHemisphere = 'S';
}
var latitude = (int)Math.Round(location.Latitude * 60000d);
var longitude = (int)Math.Round(Location.NormalizeLongitude(location.Longitude) * 60000d);
var latHemisphere = 'N';
var lonHemisphere = 'E';
if (longitude < 0)
if (latitude < 0)
{
latitude = -latitude;
latHemisphere = 'S';
}
if (longitude < 0)
{
longitude = -longitude;
lonHemisphere = 'W';
}
mouseLocation.Text = string.Format(CultureInfo.InvariantCulture,
"{0} {1:00} {2:00.000}\n{3} {4:000} {5:00.000}",
latHemisphere, latitude / 60000, (latitude % 60000) / 1000d,
lonHemisphere, longitude / 60000, (longitude % 60000) / 1000d);
mouseLocation.Visibility = Visibility.Visible;
}
else
{
longitude = -longitude;
lonHemisphere = 'W';
mouseLocation.Visibility = Visibility.Collapsed;
mouseLocation.Text = string.Empty;
}
mouseLocation.Text = string.Format(CultureInfo.InvariantCulture,
"{0} {1:00} {2:00.000}\n{3} {4:000} {5:00.000}",
latHemisphere, latitude / 60000, (latitude % 60000) / 1000d,
lonHemisphere, longitude / 60000, (longitude % 60000) / 1000d);
mouseLocation.Visibility = Visibility.Visible;
}
private void MapPointerExited(object sender, PointerRoutedEventArgs e)

View file

@ -95,27 +95,35 @@ namespace SampleApplication
private void MapMouseMove(object sender, MouseEventArgs e)
{
var location = map.ViewToLocation(e.GetPosition(map));
var latitude = (int)Math.Round(location.Latitude * 60000d);
var longitude = (int)Math.Round(Location.NormalizeLongitude(location.Longitude) * 60000d);
var latHemisphere = 'N';
var lonHemisphere = 'E';
if (latitude < 0)
if (location != null)
{
latitude = -latitude;
latHemisphere = 'S';
}
var latitude = (int)Math.Round(location.Latitude * 60000d);
var longitude = (int)Math.Round(Location.NormalizeLongitude(location.Longitude) * 60000d);
var latHemisphere = 'N';
var lonHemisphere = 'E';
if (longitude < 0)
if (latitude < 0)
{
latitude = -latitude;
latHemisphere = 'S';
}
if (longitude < 0)
{
longitude = -longitude;
lonHemisphere = 'W';
}
mouseLocation.Text = string.Format(CultureInfo.InvariantCulture,
"{0} {1:00} {2:00.000}\n{3} {4:000} {5:00.000}",
latHemisphere, latitude / 60000, (latitude % 60000) / 1000d,
lonHemisphere, longitude / 60000, (longitude % 60000) / 1000d);
}
else
{
longitude = -longitude;
lonHemisphere = 'W';
mouseLocation.Text = string.Empty;
}
mouseLocation.Text = string.Format(CultureInfo.InvariantCulture,
"{0} {1:00} {2:00.000}\n{3} {4:000} {5:00.000}",
latHemisphere, latitude / 60000, (latitude % 60000) / 1000d,
lonHemisphere, longitude / 60000, (longitude % 60000) / 1000d);
}
private void MapMouseLeave(object sender, MouseEventArgs e)