diff --git a/FileDbCache/UWP/FileDbCache.cs b/FileDbCache/UWP/FileDbCache.cs index c4a220a7..fb44fdb7 100644 --- a/FileDbCache/UWP/FileDbCache.cs +++ b/FileDbCache/UWP/FileDbCache.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/FileDbCache/UWP/Properties/AssemblyInfo.cs b/FileDbCache/UWP/Properties/AssemblyInfo.cs index 5d8c1fc0..d1482c16 100644 --- a/FileDbCache/UWP/Properties/AssemblyInfo.cs +++ b/FileDbCache/UWP/Properties/AssemblyInfo.cs @@ -5,10 +5,10 @@ using System.Runtime.InteropServices; [assembly: AssemblyDescription("IImageCache implementation based on EzTools FileDb")] [assembly: AssemblyProduct("XAML Map Control")] [assembly: AssemblyCompany("Clemens Fischer")] -[assembly: AssemblyCopyright("© 2017 Clemens Fischer")] +[assembly: AssemblyCopyright("© 2018 Clemens Fischer")] [assembly: AssemblyTrademark("")] -[assembly: AssemblyVersion("4.3.0")] -[assembly: AssemblyFileVersion("4.3.0")] +[assembly: AssemblyVersion("4.4.0")] +[assembly: AssemblyFileVersion("4.4.0")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] diff --git a/FileDbCache/WPF/FileDbCache.cs b/FileDbCache/WPF/FileDbCache.cs index 1a5f7bd6..a98eb019 100644 --- a/FileDbCache/WPF/FileDbCache.cs +++ b/FileDbCache/WPF/FileDbCache.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using FileDbNs; diff --git a/FileDbCache/WPF/Properties/AssemblyInfo.cs b/FileDbCache/WPF/Properties/AssemblyInfo.cs index 87638e84..11d32ffc 100644 --- a/FileDbCache/WPF/Properties/AssemblyInfo.cs +++ b/FileDbCache/WPF/Properties/AssemblyInfo.cs @@ -5,10 +5,10 @@ using System.Runtime.InteropServices; [assembly: AssemblyDescription("ObjectCache implementation based on EzTools FileDb")] [assembly: AssemblyProduct("XAML Map Control")] [assembly: AssemblyCompany("Clemens Fischer")] -[assembly: AssemblyCopyright("© 2017 Clemens Fischer")] +[assembly: AssemblyCopyright("© 2018 Clemens Fischer")] [assembly: AssemblyTrademark("")] -[assembly: AssemblyVersion("4.3.0")] -[assembly: AssemblyFileVersion("4.3.0")] +[assembly: AssemblyVersion("4.4.0")] +[assembly: AssemblyFileVersion("4.4.0")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] diff --git a/MBTiles/UWP/Properties/AssemblyInfo.cs b/MBTiles/UWP/Properties/AssemblyInfo.cs index 9691294b..9f0caf66 100644 --- a/MBTiles/UWP/Properties/AssemblyInfo.cs +++ b/MBTiles/UWP/Properties/AssemblyInfo.cs @@ -5,10 +5,10 @@ using System.Runtime.InteropServices; [assembly: AssemblyDescription("MBTiles Support Library for XAML Map Control")] [assembly: AssemblyProduct("XAML Map Control")] [assembly: AssemblyCompany("Clemens Fischer")] -[assembly: AssemblyCopyright("© 2017 Clemens Fischer")] +[assembly: AssemblyCopyright("© 2018 Clemens Fischer")] [assembly: AssemblyTrademark("")] -[assembly: AssemblyVersion("4.3.0")] -[assembly: AssemblyFileVersion("4.3.0")] +[assembly: AssemblyVersion("4.4.0")] +[assembly: AssemblyFileVersion("4.4.0")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] diff --git a/MBTiles/WPF/Properties/AssemblyInfo.cs b/MBTiles/WPF/Properties/AssemblyInfo.cs index 8598b154..35124928 100644 --- a/MBTiles/WPF/Properties/AssemblyInfo.cs +++ b/MBTiles/WPF/Properties/AssemblyInfo.cs @@ -5,10 +5,10 @@ using System.Runtime.InteropServices; [assembly: AssemblyDescription("MBTiles Support Library for XAML Map Control")] [assembly: AssemblyProduct("XAML Map Control")] [assembly: AssemblyCompany("Clemens Fischer")] -[assembly: AssemblyCopyright("© 2017 Clemens Fischer")] +[assembly: AssemblyCopyright("© 2018 Clemens Fischer")] [assembly: AssemblyTrademark("")] -[assembly: AssemblyVersion("4.3.0")] -[assembly: AssemblyFileVersion("4.3.0")] +[assembly: AssemblyVersion("4.4.0")] +[assembly: AssemblyFileVersion("4.4.0")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] diff --git a/MapControl/Shared/AzimuthalEquidistantProjection.cs b/MapControl/Shared/AzimuthalEquidistantProjection.cs index f915cdd8..7565961c 100644 --- a/MapControl/Shared/AzimuthalEquidistantProjection.cs +++ b/MapControl/Shared/AzimuthalEquidistantProjection.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/Shared/AzimuthalProjection.cs b/MapControl/Shared/AzimuthalProjection.cs index faa3b663..380b6a75 100644 --- a/MapControl/Shared/AzimuthalProjection.cs +++ b/MapControl/Shared/AzimuthalProjection.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; @@ -21,13 +21,8 @@ namespace MapControl public AzimuthalProjection() { + IsContinuous = false; IsAzimuthal = true; - LongitudeScale = double.NaN; - } - - public override double GetViewportScale(double zoomLevel) - { - return DegreesToViewportScale(zoomLevel) / MetersPerDegree; } public override Point GetMapScale(Location location) @@ -37,7 +32,7 @@ namespace MapControl public override Location TranslateLocation(Location location, Point translation) { - var scaleY = ViewportScale * MetersPerDegree; + var scaleY = ViewportScale * TrueScale; var scaleX = scaleY * Math.Cos(location.Latitude * Math.PI / 180d); return new Location( @@ -112,7 +107,7 @@ namespace MapControl var cosDistance = sinLat1 * sinLat2 + cosLat1 * cosLat2 * cosLon12; azimuth = Math.Atan2(sinLon12, cosLat1 * sinLat2 / cosLat2 - sinLat1 * cosLon12); - distance = Math.Acos(Math.Max(Math.Min(cosDistance, 1d), -1d)); + distance = Math.Acos(Math.Min(Math.Max(cosDistance, -1d), 1d)); } /// @@ -128,10 +123,10 @@ namespace MapControl var cosLat1 = Math.Cos(lat1); var sinLat1 = Math.Sin(lat1); var sinLat2 = sinLat1 * cosDistance + cosLat1 * sinDistance * cosAzimuth; - var lat2 = Math.Asin(Math.Max(Math.Min(sinLat2, 1d), -1d)); + var lat2 = Math.Asin(Math.Min(Math.Max(sinLat2, -1d), 1d)); var dLon = Math.Atan2(sinDistance * sinAzimuth, cosLat1 * cosDistance - sinLat1 * sinDistance * cosAzimuth); - return new Location(180d / Math.PI * lat2, location.Longitude + 180d / Math.PI * dLon); + return new Location(lat2 * 180d / Math.PI, location.Longitude + dLon * 180d / Math.PI); } } } diff --git a/MapControl/Shared/BingMapsTileLayer.cs b/MapControl/Shared/BingMapsTileLayer.cs index d2048c4a..7e5818f1 100644 --- a/MapControl/Shared/BingMapsTileLayer.cs +++ b/MapControl/Shared/BingMapsTileLayer.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/Shared/BingMapsTileSource.cs b/MapControl/Shared/BingMapsTileSource.cs index 7c93c607..52f9650f 100644 --- a/MapControl/Shared/BingMapsTileSource.cs +++ b/MapControl/Shared/BingMapsTileSource.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/Shared/BoundingBox.cs b/MapControl/Shared/BoundingBox.cs index 906a2e0d..810dc34d 100644 --- a/MapControl/Shared/BoundingBox.cs +++ b/MapControl/Shared/BoundingBox.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/Shared/CenteredBoundingBox.cs b/MapControl/Shared/CenteredBoundingBox.cs index 07cab657..08881af3 100644 --- a/MapControl/Shared/CenteredBoundingBox.cs +++ b/MapControl/Shared/CenteredBoundingBox.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) namespace MapControl diff --git a/MapControl/Shared/EquirectangularProjection.cs b/MapControl/Shared/EquirectangularProjection.cs index 4645b6d7..42f10cc3 100644 --- a/MapControl/Shared/EquirectangularProjection.cs +++ b/MapControl/Shared/EquirectangularProjection.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; @@ -20,6 +20,7 @@ namespace MapControl public EquirectangularProjection() : this("EPSG:4326") { + TrueScale = 1; } public EquirectangularProjection(string crsId) diff --git a/MapControl/Shared/GnomonicProjection.cs b/MapControl/Shared/GnomonicProjection.cs index 09859756..468f5033 100644 --- a/MapControl/Shared/GnomonicProjection.cs +++ b/MapControl/Shared/GnomonicProjection.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/Shared/HyperlinkText.cs b/MapControl/Shared/HyperlinkText.cs index 01c0d589..eba21278 100644 --- a/MapControl/Shared/HyperlinkText.cs +++ b/MapControl/Shared/HyperlinkText.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/Shared/Location.cs b/MapControl/Shared/Location.cs index a911fcb5..eeba9eb0 100644 --- a/MapControl/Shared/Location.cs +++ b/MapControl/Shared/Location.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/Shared/LocationCollection.cs b/MapControl/Shared/LocationCollection.cs index b4f58941..43d3ec86 100644 --- a/MapControl/Shared/LocationCollection.cs +++ b/MapControl/Shared/LocationCollection.cs @@ -1,18 +1,17 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; using System.Collections.Generic; -using System.Collections.ObjectModel; using System.Linq; namespace MapControl { /// - /// An ObservableCollection of Location with support for parsing. + /// A collection of Locations with support for parsing. /// - public partial class LocationCollection : ObservableCollection + public partial class LocationCollection : List { public LocationCollection() { @@ -23,7 +22,7 @@ namespace MapControl { } - public LocationCollection(List locations) + public LocationCollection(params Location[] locations) : base(locations) { } diff --git a/MapControl/Shared/MapBase.cs b/MapControl/Shared/MapBase.cs index 15148b29..567766c5 100644 --- a/MapControl/Shared/MapBase.cs +++ b/MapControl/Shared/MapBase.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; @@ -291,8 +291,8 @@ namespace MapControl { if (Heading != 0d) { - var cos = Math.Cos(Heading / 180d * Math.PI); - var sin = Math.Sin(Heading / 180d * Math.PI); + var cos = Math.Cos(Heading * Math.PI / 180d); + var sin = Math.Sin(Heading * Math.PI / 180d); translation = new Point( translation.X * cos + translation.Y * sin, @@ -352,7 +352,7 @@ namespace MapControl { SetTransformCenter(center); - if (double.IsNaN(MapProjection.LongitudeScale)) + if (MapProjection.IsAzimuthal) { ZoomLevel = zoomLevel; ResetTransformCenter(); @@ -374,13 +374,10 @@ namespace MapControl { var rect = MapProjection.BoundingBoxToRect(boundingBox); var center = new Point(rect.X + rect.Width / 2d, rect.Y + rect.Height / 2d); - var scale0 = 1d / MapProjection.GetViewportScale(0d); - var lonScale = scale0 * RenderSize.Width / rect.Width; - var latScale = scale0 * RenderSize.Height / rect.Height; - var lonZoom = Math.Log(lonScale, 2d); - var latZoom = Math.Log(latScale, 2d); + var scale = Math.Min(RenderSize.Width / rect.Width, RenderSize.Height / rect.Height) + * MapProjection.TrueScale / MapProjection.PixelPerDegree; - TargetZoomLevel = Math.Min(lonZoom, latZoom); + TargetZoomLevel = Math.Log(scale, 2d); TargetCenter = MapProjection.PointToLocation(center); TargetHeading = 0d; } @@ -479,6 +476,10 @@ namespace MapControl if (!targetCenter.Equals(Center)) { + var targetCenterLongitude = MapProjection.IsContinuous + ? Location.NearestLongitude(targetCenter.Longitude, Center.Longitude) + : targetCenter.Longitude; + if (centerAnimation != null) { centerAnimation.Completed -= CenterAnimationCompleted; @@ -487,7 +488,7 @@ namespace MapControl centerAnimation = new PointAnimation { From = new Point(Center.Longitude, Center.Latitude), - To = new Point(Location.NearestLongitude(targetCenter.Longitude, Center.Longitude), targetCenter.Latitude), + To = new Point(targetCenterLongitude, targetCenter.Latitude), Duration = AnimationDuration, EasingFunction = AnimationEasingFunction }; diff --git a/MapControl/Shared/MapGraticule.cs b/MapControl/Shared/MapGraticule.cs index 02516aa0..069deb74 100644 --- a/MapControl/Shared/MapGraticule.cs +++ b/MapControl/Shared/MapGraticule.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; @@ -30,7 +30,8 @@ namespace MapControl private double GetLineDistance() { - var minDistance = MinLineDistance / MapProjection.DegreesToViewportScale(ParentMap.ZoomLevel); + var pixelPerDegree = ParentMap.MapProjection.ViewportScale * ParentMap.MapProjection.TrueScale; + var minDistance = MinLineDistance / pixelPerDegree; var scale = 1d; if (minDistance < 1d) diff --git a/MapControl/Shared/MapImageLayer.cs b/MapControl/Shared/MapImageLayer.cs index e70c6c2f..dfc1de39 100644 --- a/MapControl/Shared/MapImageLayer.cs +++ b/MapControl/Shared/MapImageLayer.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; @@ -23,7 +23,7 @@ namespace MapControl { /// /// Map image layer. Fills the entire viewport with a map image, e.g. provided by a Web Map Service (WMS). - /// The image must be provided by the abstract UpdateImage(BoundingBox) method. + /// The image must be provided by the abstract GetImageAsync method. /// public abstract class MapImageLayer : MapPanel, IMapLayer { @@ -71,7 +71,7 @@ namespace MapControl Children.Add(new Image { Opacity = 0d, Stretch = Stretch.Fill }); updateTimer = new DispatcherTimer { Interval = UpdateInterval }; - updateTimer.Tick += async (s, e) => await UpdateImage(); + updateTimer.Tick += async (s, e) => await UpdateImageAsync(); } /// @@ -181,7 +181,7 @@ namespace MapControl /// /// Returns an ImageSource for the specified bounding box. /// - protected abstract Task GetImage(BoundingBox boundingBox); + protected abstract Task GetImageAsync(BoundingBox boundingBox); protected override void OnViewportChanged(ViewportChangedEventArgs e) { @@ -191,7 +191,7 @@ namespace MapControl base.OnViewportChanged(e); - var task = UpdateImage(); + var task = UpdateImageAsync(); } else { @@ -211,7 +211,7 @@ namespace MapControl } } - protected virtual async Task UpdateImage() + protected virtual async Task UpdateImageAsync() { updateTimer.Stop(); @@ -231,7 +231,7 @@ namespace MapControl { try { - imageSource = await GetImage(boundingBox); + imageSource = await GetImageAsync(boundingBox); } catch (Exception ex) { diff --git a/MapControl/Shared/MapItemsControl.cs b/MapControl/Shared/MapItemsControl.cs index 12f49643..17c72261 100644 --- a/MapControl/Shared/MapItemsControl.cs +++ b/MapControl/Shared/MapItemsControl.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) #if WINDOWS_UWP diff --git a/MapControl/Shared/MapOverlay.cs b/MapControl/Shared/MapOverlay.cs index 2d239ce1..da7d785b 100644 --- a/MapControl/Shared/MapOverlay.cs +++ b/MapControl/Shared/MapOverlay.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) #if WINDOWS_UWP diff --git a/MapControl/Shared/MapPanel.cs b/MapControl/Shared/MapPanel.cs index 0e608d8b..a7b1878b 100644 --- a/MapControl/Shared/MapPanel.cs +++ b/MapControl/Shared/MapPanel.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; @@ -203,8 +203,9 @@ namespace MapControl { viewportPosition = parentMap.MapProjection.LocationToViewportPoint(location); - if (viewportPosition.X < 0d || viewportPosition.X > parentMap.RenderSize.Width || - viewportPosition.Y < 0d || viewportPosition.Y > parentMap.RenderSize.Height) + if (parentMap.MapProjection.IsContinuous && + (viewportPosition.X < 0d || viewportPosition.X > parentMap.RenderSize.Width || + viewportPosition.Y < 0d || viewportPosition.Y > parentMap.RenderSize.Height)) { viewportPosition = parentMap.MapProjection.LocationToViewportPoint(new Location( location.Latitude, @@ -262,8 +263,9 @@ namespace MapControl rotation = parentMap.Heading; viewportPosition = projection.ViewportTransform.Transform(center); - if (viewportPosition.X < 0d || viewportPosition.X > parentMap.RenderSize.Width || - viewportPosition.Y < 0d || viewportPosition.Y > parentMap.RenderSize.Height) + if (parentMap.MapProjection.IsContinuous && + (viewportPosition.X < 0d || viewportPosition.X > parentMap.RenderSize.Width || + viewportPosition.Y < 0d || viewportPosition.Y > parentMap.RenderSize.Height)) { var location = projection.PointToLocation(center); location.Longitude = Location.NearestLongitude(location.Longitude, parentMap.Center.Longitude); diff --git a/MapControl/Shared/MapPath.cs b/MapControl/Shared/MapPath.cs deleted file mode 100644 index d36a0bc3..00000000 --- a/MapControl/Shared/MapPath.cs +++ /dev/null @@ -1,117 +0,0 @@ -// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 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 -{ - /// - /// Base class for map shapes. The shape geometry is given by the Data property, - /// which must contain a Geometry defined in cartesian (projected) map coordinates. - /// Optionally, the Location property can by set to adjust the viewport position to the - /// visible map viewport, as done for elements where the MapPanel.Location property is set. - /// - 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).LocationPropertyChanged())); - - public MapPath() - { - MapPanel.InitMapElement(this); - } - - public Location Location - { - get { return (Location)GetValue(LocationProperty); } - set { SetValue(LocationProperty, value); } - } - - private readonly TransformGroup viewportTransform = new TransformGroup(); - private MapBase parentMap; - - public MapBase ParentMap - { - get { return parentMap; } - set - { - if (parentMap != null) - { - parentMap.ViewportChanged -= OnViewportChanged; - } - - viewportTransform.Children.Clear(); - parentMap = value; - - if (parentMap != null) - { - viewportTransform.Children.Add(new TranslateTransform()); - viewportTransform.Children.Add(parentMap.MapProjection.ViewportTransform); - parentMap.ViewportChanged += OnViewportChanged; - } - - UpdateData(); - } - } - - protected virtual void UpdateData() - { - } - - protected virtual void OnViewportChanged(ViewportChangedEventArgs e) - { - double longitudeScale = parentMap.MapProjection.LongitudeScale; - - if (e.ProjectionChanged) - { - viewportTransform.Children[1] = parentMap.MapProjection.ViewportTransform; - } - - if (e.ProjectionChanged || double.IsNaN(longitudeScale)) - { - UpdateData(); - } - - if (!double.IsNaN(longitudeScale)) // a normal cylindrical projection - { - var longitudeOffset = 0d; - - if (Location != null) - { - var viewportPosition = parentMap.MapProjection.LocationToViewportPoint(Location); - - 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 = longitudeScale * (nearestLongitude - Location.Longitude); - } - } - - ((TranslateTransform)viewportTransform.Children[0]).X = longitudeOffset; - } - } - - private void OnViewportChanged(object sender, ViewportChangedEventArgs e) - { - OnViewportChanged(e); - } - - private void LocationPropertyChanged() - { - if (parentMap != null) - { - OnViewportChanged(new ViewportChangedEventArgs()); - } - } - } -} diff --git a/MapControl/Shared/MapPolyline.cs b/MapControl/Shared/MapPolyline.cs deleted file mode 100644 index bcada6ad..00000000 --- a/MapControl/Shared/MapPolyline.cs +++ /dev/null @@ -1,71 +0,0 @@ -// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer -// Licensed under the Microsoft Public License (Ms-PL) - -using System.Collections.Generic; -using System.Collections.Specialized; -#if WINDOWS_UWP -using Windows.UI.Xaml; -using Windows.UI.Xaml.Media; -#else -using System.Windows; -using System.Windows.Media; -#endif - -namespace MapControl -{ - /// - /// A polyline or polygon created from a collection of Locations. - /// - public partial class MapPolyline : MapPath - { - public static readonly DependencyProperty LocationsProperty = DependencyProperty.Register( - nameof(Locations), typeof(IEnumerable), typeof(MapPolyline), - new PropertyMetadata(null, (o, e) => ((MapPolyline)o).LocationsPropertyChanged(e))); - - public static readonly DependencyProperty IsClosedProperty = DependencyProperty.Register( - nameof(IsClosed), typeof(bool), typeof(MapPolyline), - new PropertyMetadata(false, (o, e) => ((MapPolyline)o).UpdateData())); - - /// - /// Gets or sets a value that indicates if the polyline is closed, i.e. is a polygon. - /// - public bool IsClosed - { - get { return (bool)GetValue(IsClosedProperty); } - set { SetValue(IsClosedProperty, value); } - } - - /// - /// Gets or sets the FillRule of the PathGeometry that represents the polyline. - /// - public FillRule FillRule - { - get { return (FillRule)GetValue(FillRuleProperty); } - set { SetValue(FillRuleProperty, value); } - } - - private void LocationsPropertyChanged(DependencyPropertyChangedEventArgs e) - { - var oldCollection = e.OldValue as INotifyCollectionChanged; - var newCollection = e.NewValue as INotifyCollectionChanged; - - if (oldCollection != null) - { - oldCollection.CollectionChanged -= LocationCollectionChanged; - } - - if (newCollection != null) - { - newCollection.CollectionChanged += LocationCollectionChanged; - } - - UpdateData(); - } - - private void LocationCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) - { - UpdateData(); - } - } -} diff --git a/MapControl/Shared/MapProjection.cs b/MapControl/Shared/MapProjection.cs index e8d3c689..fbdd92f0 100644 --- a/MapControl/Shared/MapProjection.cs +++ b/MapControl/Shared/MapProjection.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; @@ -19,21 +19,11 @@ namespace MapControl /// public abstract class MapProjection { - public const double Wgs84EquatorialRadius = 6378137d; - public const double Wgs84Flattening = 1d / 298.257223563; - public static readonly double Wgs84Eccentricity = Math.Sqrt((2d - Wgs84Flattening) * Wgs84Flattening); - - public const double MetersPerDegree = Wgs84EquatorialRadius * Math.PI / 180d; - public const int TileSize = 256; + public const double PixelPerDegree = TileSize / 360d; - /// - /// Gets the scaling factor from cartesian map coordinates in degrees to viewport coordinates for the specified zoom level. - /// - public static double DegreesToViewportScale(double zoomLevel) - { - return Math.Pow(2d, zoomLevel) * TileSize / 360d; - } + public const double Wgs84EquatorialRadius = 6378137d; + public const double MetersPerDegree = Wgs84EquatorialRadius * Math.PI / 180d; /// /// Gets or sets the WMS 1.3.0 CRS Identifier. @@ -41,9 +31,9 @@ namespace MapControl public string CrsId { get; set; } /// - /// Indicates if this is a web mercator projection, i.e. compatible with MapTileLayer. + /// Indicates if the map can be moved infinitely in longitudinal direction. /// - public bool IsWebMercator { get; protected set; } = false; + public bool IsContinuous { get; protected set; } = true; /// /// Indicates if this is an azimuthal projection. @@ -51,33 +41,30 @@ namespace MapControl public bool IsAzimuthal { get; protected set; } = false; /// - /// Gets the scale factor from longitude to x values of a normal cylindrical projection. - /// Returns NaN if this is not a normal cylindrical projection. + /// Indicates if this is a web mercator projection, i.e. compatible with MapTileLayer. /// - public double LongitudeScale { get; protected set; } = 1d; + public bool IsWebMercator { get; protected set; } = false; + + /// + /// Gets the scale factor from geographic to cartesian coordinates, on the line of true scale + /// of a cylindrical projection, or at the projection center of an azimuthal projection. + /// + public double TrueScale { get; protected set; } = MetersPerDegree; /// /// Gets the absolute value of the minimum and maximum latitude that can be transformed. /// public double MaxLatitude { get; protected set; } = 90d; - /// - /// Gets the scaling factor from cartesian map coordinates to viewport coordinates. - /// - public double ViewportScale { get; protected set; } - /// /// Gets the transformation from cartesian map coordinates to viewport coordinates (pixels). /// public MatrixTransform ViewportTransform { get; } = new MatrixTransform(); /// - /// Gets the scaling factor from cartesian map coordinates to viewport coordinates for the specified zoom level. + /// Gets the scaling factor from cartesian map coordinates to viewport coordinates. /// - public virtual double GetViewportScale(double zoomLevel) - { - return DegreesToViewportScale(zoomLevel); - } + public double ViewportScale { get; protected set; } /// /// Gets the map scale at the specified Location as viewport coordinate units per meter (px/m). @@ -149,7 +136,7 @@ namespace MapControl /// public virtual void SetViewportTransform(Location projectionCenter, Location mapCenter, Point viewportCenter, double zoomLevel, double heading) { - ViewportScale = GetViewportScale(zoomLevel); + ViewportScale = Math.Pow(2d, zoomLevel) * PixelPerDegree / TrueScale; var center = LocationToPoint(mapCenter); diff --git a/MapControl/Shared/MapScale.cs b/MapControl/Shared/MapScale.cs index 0d94ed61..9245fcf2 100644 --- a/MapControl/Shared/MapScale.cs +++ b/MapControl/Shared/MapScale.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/Shared/MapShape.cs b/MapControl/Shared/MapShape.cs new file mode 100644 index 00000000..cd64abf1 --- /dev/null +++ b/MapControl/Shared/MapShape.cs @@ -0,0 +1,111 @@ +// 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 +{ + /// + /// Base class for MapPolyline and MapPolygon. + /// + 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())); + + /// + /// 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. + /// + public Location Location + { + get { return (Location)GetValue(LocationProperty); } + set { SetValue(LocationProperty, value); } + } + + private MapBase parentMap; + + public MapBase ParentMap + { + get { return parentMap; } + set + { + if (parentMap != null) + { + parentMap.ViewportChanged -= OnViewportChanged; + } + + parentMap = value; + + if (parentMap != null) + { + parentMap.ViewportChanged += OnViewportChanged; + } + + ParentMapChanged(); + } + } + + protected MapShape() + { + Data = new PathGeometry(); + + MapPanel.InitMapElement(this); + } + + 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; + } + + protected double GetLongitudeOffset() + { + var longitudeOffset = 0d; + + if (parentMap.MapProjection.IsContinuous && Location != null) + { + var viewportPosition = parentMap.MapProjection.LocationToViewportPoint(Location); + + 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; + } + + private void LocationPropertyChanged() + { + if (parentMap != null) + { + OnViewportChanged(parentMap, new ViewportChangedEventArgs()); + } + } + } +} diff --git a/MapControl/Shared/MapTileLayer.cs b/MapControl/Shared/MapTileLayer.cs index b17e4ee8..3238ad1d 100644 --- a/MapControl/Shared/MapTileLayer.cs +++ b/MapControl/Shared/MapTileLayer.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/Shared/OrthographicProjection.cs b/MapControl/Shared/OrthographicProjection.cs index 80faeffb..d650d6b1 100644 --- a/MapControl/Shared/OrthographicProjection.cs +++ b/MapControl/Shared/OrthographicProjection.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/Shared/PolygonCollection.cs b/MapControl/Shared/PolygonCollection.cs new file mode 100644 index 00000000..084433f2 --- /dev/null +++ b/MapControl/Shared/PolygonCollection.cs @@ -0,0 +1,74 @@ +// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control +// © 2018 Clemens Fischer +// Licensed under the Microsoft Public License (Ms-PL) + +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Collections.Specialized; +using System.Linq; +using System.Windows; + +namespace MapControl +{ + /// + /// An ObservableCollection of IEnumerable of Location. PolygonCollection adds a CollectionChanged + /// listener to each element that implements INotifyCollectionChanged and, when such an element changes, + /// fires its own CollectionChanged event with NotifyCollectionChangedAction.Replace for that element. + /// + public class PolygonCollection : ObservableCollection>, IWeakEventListener + { + protected override void InsertItem(int index, IEnumerable polygon) + { + var observablePolygon = polygon as INotifyCollectionChanged; + + if (observablePolygon != null) + { + CollectionChangedEventManager.AddListener(observablePolygon, this); + } + + base.InsertItem(index, polygon); + } + + protected override void SetItem(int index, IEnumerable polygon) + { + var observablePolygon = this[index] as INotifyCollectionChanged; + + if (observablePolygon != null) + { + CollectionChangedEventManager.RemoveListener(observablePolygon, this); + } + + base.SetItem(index, polygon); + } + + protected override void RemoveItem(int index) + { + var observablePolygon = this[index] as INotifyCollectionChanged; + + if (observablePolygon != null) + { + CollectionChangedEventManager.RemoveListener(observablePolygon, this); + } + + base.RemoveItem(index); + } + + protected override void ClearItems() + { + foreach (var observablePolygon in this.OfType()) + { + CollectionChangedEventManager.RemoveListener(observablePolygon, this); + } + + base.ClearItems(); + } + + bool IWeakEventListener.ReceiveWeakEvent(Type managerType, object sender, EventArgs e) + { + OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, sender, sender)); + + return true; + } + } +} diff --git a/MapControl/Shared/Pushpin.cs b/MapControl/Shared/Pushpin.cs index 97bb14e9..b7410979 100644 --- a/MapControl/Shared/Pushpin.cs +++ b/MapControl/Shared/Pushpin.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) #if WINDOWS_UWP @@ -21,5 +21,11 @@ namespace MapControl MapPanel.InitMapElement(this); } + + public Location Location + { + get { return MapPanel.GetLocation(this); } + set { MapPanel.SetLocation(this, value); } + } } } diff --git a/MapControl/Shared/StereographicProjection.cs b/MapControl/Shared/StereographicProjection.cs index f22f400c..9abb283b 100644 --- a/MapControl/Shared/StereographicProjection.cs +++ b/MapControl/Shared/StereographicProjection.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/Shared/Tile.cs b/MapControl/Shared/Tile.cs index c4a3788f..e72c7e96 100644 --- a/MapControl/Shared/Tile.cs +++ b/MapControl/Shared/Tile.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/Shared/TileGrid.cs b/MapControl/Shared/TileGrid.cs index 00cc2e3a..297c0b02 100644 --- a/MapControl/Shared/TileGrid.cs +++ b/MapControl/Shared/TileGrid.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/Shared/TileImageLoader.cs b/MapControl/Shared/TileImageLoader.cs index d889a586..30df2cd2 100644 --- a/MapControl/Shared/TileImageLoader.cs +++ b/MapControl/Shared/TileImageLoader.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/Shared/TileSource.cs b/MapControl/Shared/TileSource.cs index 3522f2cf..cdac0647 100644 --- a/MapControl/Shared/TileSource.cs +++ b/MapControl/Shared/TileSource.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/Shared/ViewportChangedEventArgs.cs b/MapControl/Shared/ViewportChangedEventArgs.cs index f1f66e09..45491069 100644 --- a/MapControl/Shared/ViewportChangedEventArgs.cs +++ b/MapControl/Shared/ViewportChangedEventArgs.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/Shared/WebMercatorProjection.cs b/MapControl/Shared/WebMercatorProjection.cs index 68d0c89d..094ff2de 100644 --- a/MapControl/Shared/WebMercatorProjection.cs +++ b/MapControl/Shared/WebMercatorProjection.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; @@ -13,7 +13,7 @@ namespace MapControl { /// /// Transforms map coordinates according to the Web (or Pseudo) Mercator Projection, EPSG:3857. - /// Longitude values are transformed linearly to X values in meters, by multiplying with MetersPerDegree. + /// Longitude values are transformed linearly to X values in meters, by multiplying with TrueScale. /// Latitude values in the interval [-MaxLatitude .. MaxLatitude] are transformed to Y values in meters /// in the interval [-R*pi .. R*pi], R=Wgs84EquatorialRadius. /// @@ -28,15 +28,9 @@ namespace MapControl { CrsId = crsId; IsWebMercator = true; - LongitudeScale = MetersPerDegree; MaxLatitude = YToLatitude(180d); } - public override double GetViewportScale(double zoomLevel) - { - return DegreesToViewportScale(zoomLevel) / MetersPerDegree; - } - public override Point GetMapScale(Location location) { var scale = ViewportScale / Math.Cos(location.Latitude * Math.PI / 180d); @@ -47,20 +41,20 @@ namespace MapControl public override Point LocationToPoint(Location location) { return new Point( - MetersPerDegree * location.Longitude, - MetersPerDegree * LatitudeToY(location.Latitude)); + TrueScale * location.Longitude, + TrueScale * LatitudeToY(location.Latitude)); } public override Location PointToLocation(Point point) { return new Location( - YToLatitude(point.Y / MetersPerDegree), - point.X / MetersPerDegree); + YToLatitude(point.Y / TrueScale), + point.X / TrueScale); } public override Location TranslateLocation(Location location, Point translation) { - var scaleX = MetersPerDegree * ViewportScale; + var scaleX = TrueScale * ViewportScale; var scaleY = scaleX / Math.Cos(location.Latitude * Math.PI / 180d); return new Location( @@ -70,9 +64,17 @@ namespace MapControl public static double LatitudeToY(double latitude) { - return latitude <= -90d ? double.NegativeInfinity - : latitude >= 90d ? double.PositiveInfinity - : Math.Log(Math.Tan((latitude + 90d) * Math.PI / 360d)) / Math.PI * 180d; + if (latitude <= -90d) + { + return double.NegativeInfinity; + } + + if (latitude >= 90d) + { + return double.PositiveInfinity; + } + + return Math.Log(Math.Tan((latitude + 90d) * Math.PI / 360d)) / Math.PI * 180d; } public static double YToLatitude(double y) diff --git a/MapControl/Shared/WmsImageLayer.cs b/MapControl/Shared/WmsImageLayer.cs index a173ebdd..72183afd 100644 --- a/MapControl/Shared/WmsImageLayer.cs +++ b/MapControl/Shared/WmsImageLayer.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; @@ -23,27 +23,27 @@ namespace MapControl { public static readonly DependencyProperty ServerUriProperty = DependencyProperty.Register( nameof(ServerUri), typeof(Uri), typeof(WmsImageLayer), - new PropertyMetadata(null, async (o, e) => await ((WmsImageLayer)o).UpdateImage())); + new PropertyMetadata(null, async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync())); public static readonly DependencyProperty VersionProperty = DependencyProperty.Register( nameof(Version), typeof(string), typeof(WmsImageLayer), - new PropertyMetadata("1.3.0", async (o, e) => await ((WmsImageLayer)o).UpdateImage())); + new PropertyMetadata("1.3.0", async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync())); public static readonly DependencyProperty LayersProperty = DependencyProperty.Register( nameof(Layers), typeof(string), typeof(WmsImageLayer), - new PropertyMetadata(string.Empty, async (o, e) => await ((WmsImageLayer)o).UpdateImage())); + new PropertyMetadata(string.Empty, async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync())); public static readonly DependencyProperty StylesProperty = DependencyProperty.Register( nameof(Styles), typeof(string), typeof(WmsImageLayer), - new PropertyMetadata(string.Empty, async (o, e) => await ((WmsImageLayer)o).UpdateImage())); + new PropertyMetadata(string.Empty, async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync())); public static readonly DependencyProperty FormatProperty = DependencyProperty.Register( nameof(Format), typeof(string), typeof(WmsImageLayer), - new PropertyMetadata("image/png", async (o, e) => await ((WmsImageLayer)o).UpdateImage())); + new PropertyMetadata("image/png", async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync())); public static readonly DependencyProperty TransparentProperty = DependencyProperty.Register( nameof(Transparent), typeof(bool), typeof(WmsImageLayer), - new PropertyMetadata(false, async (o, e) => await ((WmsImageLayer)o).UpdateImage())); + new PropertyMetadata(false, async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync())); private string layers = string.Empty; @@ -83,7 +83,7 @@ namespace MapControl set { SetValue(TransparentProperty, value); } } - protected override async Task GetImage(BoundingBox boundingBox) + protected override async Task GetImageAsync(BoundingBox boundingBox) { ImageSource imageSource = null; @@ -94,8 +94,11 @@ namespace MapControl if (!string.IsNullOrEmpty(projectionParameters)) { var uri = GetRequestUri("GetMap" - + "&LAYERS=" + Layers + "&STYLES=" + Styles + "&FORMAT=" + Format - + "&TRANSPARENT=" + (Transparent ? "TRUE" : "FALSE") + "&" + projectionParameters); + + "&LAYERS=" + Layers + + "&STYLES=" + Styles + + "&FORMAT=" + Format + + "&TRANSPARENT=" + (Transparent ? "TRUE" : "FALSE") + + "&" + projectionParameters); try { diff --git a/MapControl/Shared/WorldMercatorProjection.cs b/MapControl/Shared/WorldMercatorProjection.cs index f7c4df51..72dae059 100644 --- a/MapControl/Shared/WorldMercatorProjection.cs +++ b/MapControl/Shared/WorldMercatorProjection.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; @@ -13,12 +13,15 @@ namespace MapControl { /// /// Transforms map coordinates according to the "World Mercator" Projection, EPSG:3395. - /// Longitude values are transformed linearly to X values in meters, by multiplying with MetersPerDegree. + /// Longitude values are transformed linearly to X values in meters, by multiplying with TrueScale. /// Latitude values are transformed according to the elliptical versions of the Mercator equations, /// as shown in "Map Projections - A Working Manual" (https://pubs.usgs.gov/pp/1395/report.pdf), p.44. /// public class WorldMercatorProjection : MapProjection { + public const double Wgs84Flattening = 1d / 298.257223563; + public static readonly double Wgs84Eccentricity = Math.Sqrt((2d - Wgs84Flattening) * Wgs84Flattening); + public static double MinLatitudeDelta = 1d / Wgs84EquatorialRadius; // corresponds to 1 meter public static int MaxIterations = 10; @@ -30,15 +33,9 @@ namespace MapControl public WorldMercatorProjection(string crsId) { CrsId = crsId; - LongitudeScale = MetersPerDegree; MaxLatitude = YToLatitude(180d); } - public override double GetViewportScale(double zoomLevel) - { - return DegreesToViewportScale(zoomLevel) / MetersPerDegree; - } - public override Point GetMapScale(Location location) { var lat = location.Latitude * Math.PI / 180d; @@ -51,20 +48,20 @@ namespace MapControl public override Point LocationToPoint(Location location) { return new Point( - MetersPerDegree * location.Longitude, - MetersPerDegree * LatitudeToY(location.Latitude)); + TrueScale * location.Longitude, + TrueScale * LatitudeToY(location.Latitude)); } public override Location PointToLocation(Point point) { return new Location( - YToLatitude(point.Y / MetersPerDegree), - point.X / MetersPerDegree); + YToLatitude(point.Y / TrueScale), + point.X / TrueScale); } public override Location TranslateLocation(Location location, Point translation) { - var scaleX = MetersPerDegree * ViewportScale; + var scaleX = TrueScale * ViewportScale; var scaleY = scaleX / Math.Cos(location.Latitude * Math.PI / 180d); return new Location( diff --git a/MapControl/UWP/Extensions.UWP.cs b/MapControl/UWP/Extensions.UWP.cs index dfd3b0ac..1783309a 100644 --- a/MapControl/UWP/Extensions.UWP.cs +++ b/MapControl/UWP/Extensions.UWP.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using Windows.Foundation; diff --git a/MapControl/UWP/ImageCache.UWP.cs b/MapControl/UWP/ImageCache.UWP.cs index ac2c780e..849e032f 100644 --- a/MapControl/UWP/ImageCache.UWP.cs +++ b/MapControl/UWP/ImageCache.UWP.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/UWP/ImageFileCache.UWP.cs b/MapControl/UWP/ImageFileCache.UWP.cs index 8825cf66..072ec3b8 100644 --- a/MapControl/UWP/ImageFileCache.UWP.cs +++ b/MapControl/UWP/ImageFileCache.UWP.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/UWP/ImageLoader.UWP.cs b/MapControl/UWP/ImageLoader.UWP.cs index c261fc3e..a602cd37 100644 --- a/MapControl/UWP/ImageLoader.UWP.cs +++ b/MapControl/UWP/ImageLoader.UWP.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/UWP/Map.UWP.cs b/MapControl/UWP/Map.UWP.cs index 19d517c1..6e725894 100644 --- a/MapControl/UWP/Map.UWP.cs +++ b/MapControl/UWP/Map.UWP.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/UWP/MapBase.UWP.cs b/MapControl/UWP/MapBase.UWP.cs index df99b3b5..d96691a3 100644 --- a/MapControl/UWP/MapBase.UWP.cs +++ b/MapControl/UWP/MapBase.UWP.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using Windows.Foundation; diff --git a/MapControl/UWP/MapControl.UWP.csproj b/MapControl/UWP/MapControl.UWP.csproj index c0d70712..cf6b0b51 100644 --- a/MapControl/UWP/MapControl.UWP.csproj +++ b/MapControl/UWP/MapControl.UWP.csproj @@ -91,18 +91,15 @@ MapPanel.cs - - MapPath.cs - - - MapPolyline.cs - MapProjection.cs MapScale.cs + + MapShape.cs + MapTileLayer.cs @@ -147,8 +144,8 @@ - + diff --git a/MapControl/UWP/MapGraticule.UWP.cs b/MapControl/UWP/MapGraticule.UWP.cs index 7d2de1e3..d4621b4e 100644 --- a/MapControl/UWP/MapGraticule.UWP.cs +++ b/MapControl/UWP/MapGraticule.UWP.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; @@ -28,7 +28,7 @@ namespace MapControl { var projection = ParentMap.MapProjection; - if (!double.IsNaN(projection.LongitudeScale)) + if (!projection.IsAzimuthal) { if (path == null) { diff --git a/MapControl/UWP/MapOverlay.UWP.cs b/MapControl/UWP/MapOverlay.UWP.cs index 04aa4b5f..0487894d 100644 --- a/MapControl/UWP/MapOverlay.UWP.cs +++ b/MapControl/UWP/MapOverlay.UWP.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using Windows.UI.Text; diff --git a/MapControl/UWP/MapPanel.UWP.cs b/MapControl/UWP/MapPanel.UWP.cs index 3571c282..7063f29d 100644 --- a/MapControl/UWP/MapPanel.UWP.cs +++ b/MapControl/UWP/MapPanel.UWP.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using Windows.UI.Xaml; diff --git a/MapControl/UWP/MapPath.UWP.cs b/MapControl/UWP/MapPath.UWP.cs deleted file mode 100644 index 1f6ee158..00000000 --- a/MapControl/UWP/MapPath.UWP.cs +++ /dev/null @@ -1,41 +0,0 @@ -// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer -// Licensed under the Microsoft Public License (Ms-PL) - -using Windows.Foundation; -using Windows.UI.Xaml.Media; -using Windows.UI.Xaml.Shapes; - -namespace MapControl -{ - public partial class MapPath : Path - { - private Geometry data; - - protected override Size MeasureOverride(Size availableSize) - { - if (Stretch != Stretch.None) - { - Stretch = Stretch.None; - } - - // Workaround for missing PropertyChangedCallback for the Data property. - if (data != Data) - { - if (data != null) - { - data.ClearValue(Geometry.TransformProperty); - } - - data = Data; - - if (data != null) - { - data.Transform = viewportTransform; - } - } - - return new Size(); - } - } -} diff --git a/MapControl/UWP/MapPolyline.UWP.cs b/MapControl/UWP/MapPolyline.UWP.cs index 77e6f260..83e4df6d 100644 --- a/MapControl/UWP/MapPolyline.UWP.cs +++ b/MapControl/UWP/MapPolyline.UWP.cs @@ -1,28 +1,26 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System.Collections.Generic; +using System.Collections.Specialized; using System.Linq; using Windows.UI.Xaml; using Windows.UI.Xaml.Media; namespace MapControl { - public partial class MapPolyline + /// + /// A polyline defined by a collection of Locations. + /// + public class MapPolyline : MapShape { - public static readonly DependencyProperty FillRuleProperty = DependencyProperty.Register( - nameof(FillRule), typeof(FillRule), typeof(MapPolyline), - new PropertyMetadata(FillRule.EvenOdd, (o, e) => ((PathGeometry)((MapPolyline)o).Data).FillRule = (FillRule)e.NewValue)); - - - public MapPolyline() - { - Data = new PathGeometry(); - } + public static readonly DependencyProperty LocationsProperty = DependencyProperty.Register( + nameof(Locations), typeof(IEnumerable), typeof(MapPolyline), + new PropertyMetadata(null, (o, e) => ((MapPolyline)o).LocationsPropertyChanged(e))); /// - /// Gets or sets the locations that define the polyline points. + /// Gets or sets the Locations that define the polyline points. /// public IEnumerable Locations { @@ -37,25 +35,67 @@ namespace MapControl if (ParentMap != null && Locations != null && Locations.Any()) { - var points = Locations.Select(l => ParentMap.MapProjection.LocationToPoint(l)); + PathFigure figure = null; + PolyLineSegment segment = null; + var size = ParentMap.RenderSize; + var offset = GetLongitudeOffset(); + var locations = Locations; - var figure = new PathFigure + if (offset != 0d) { - StartPoint = points.First(), - IsClosed = IsClosed, - IsFilled = IsClosed - }; - - var segment = new PolyLineSegment(); - - foreach (var point in points.Skip(1)) - { - segment.Points.Add(point); + locations = locations.Select(loc => new Location(loc.Latitude, loc.Longitude + offset)); } - figure.Segments.Add(segment); - geometry.Figures.Add(figure); + var points = locations.Select(loc => ParentMap.MapProjection.LocationToViewportPoint(loc)); + var p1 = points.First(); + + foreach (var p2 in points.Skip(1)) + { + if ((p1.X <= 0 && p2.X <= 0) || (p1.X >= size.Width && p2.X >= size.Width) || + (p1.Y <= 0 && p2.Y <= 0) || (p1.Y >= size.Height && p2.Y >= size.Height)) + { + // line (p1,p2) is out of visible bounds, end figure + figure = null; + } + else + { + if (figure == null) + { + figure = new PathFigure { StartPoint = p1, IsClosed = false, IsFilled = false }; + segment = new PolyLineSegment(); + figure.Segments.Add(segment); + geometry.Figures.Add(figure); + } + + segment.Points.Add(p2); + } + + p1 = p2; + } } } + + private void LocationsPropertyChanged(DependencyPropertyChangedEventArgs e) + { + var oldCollection = e.OldValue as INotifyCollectionChanged; + var newCollection = e.NewValue as INotifyCollectionChanged; + + if (oldCollection != null) + { + oldCollection.CollectionChanged -= LocationCollectionChanged; + } + + if (newCollection != null) + { + newCollection.CollectionChanged += LocationCollectionChanged; + } + + UpdateData(); + } + + private void LocationCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) + { + UpdateData(); + } } } diff --git a/MapControl/UWP/MapShape.UWP.cs b/MapControl/UWP/MapShape.UWP.cs new file mode 100644 index 00000000..f1f604ba --- /dev/null +++ b/MapControl/UWP/MapShape.UWP.cs @@ -0,0 +1,21 @@ +// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control +// © 2018 Clemens Fischer +// Licensed under the Microsoft Public License (Ms-PL) + +using Windows.UI.Xaml.Shapes; + +namespace MapControl +{ + public abstract partial class MapShape : Path + { + private void ParentMapChanged() + { + UpdateData(); + } + + private void OnViewportChanged(object sender, ViewportChangedEventArgs e) + { + UpdateData(); + } + } +} diff --git a/MapControl/UWP/MatrixEx.UWP.cs b/MapControl/UWP/MatrixEx.UWP.cs index 19ee5793..4f0a2ca0 100644 --- a/MapControl/UWP/MatrixEx.UWP.cs +++ b/MapControl/UWP/MatrixEx.UWP.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/UWP/Properties/AssemblyInfo.cs b/MapControl/UWP/Properties/AssemblyInfo.cs index a0b6ee82..3c3e9e03 100644 --- a/MapControl/UWP/Properties/AssemblyInfo.cs +++ b/MapControl/UWP/Properties/AssemblyInfo.cs @@ -5,10 +5,10 @@ using System.Runtime.InteropServices; [assembly: AssemblyDescription("XAML Map Control Library")] [assembly: AssemblyProduct("XAML Map Control")] [assembly: AssemblyCompany("Clemens Fischer")] -[assembly: AssemblyCopyright("© 2017 Clemens Fischer")] +[assembly: AssemblyCopyright("© 2018 Clemens Fischer")] [assembly: AssemblyTrademark("")] -[assembly: AssemblyVersion("4.3.0")] -[assembly: AssemblyFileVersion("4.3.0")] +[assembly: AssemblyVersion("4.4.0")] +[assembly: AssemblyFileVersion("4.4.0")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] diff --git a/MapControl/UWP/Tile.UWP.cs b/MapControl/UWP/Tile.UWP.cs index 491af051..058f65f2 100644 --- a/MapControl/UWP/Tile.UWP.cs +++ b/MapControl/UWP/Tile.UWP.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/UWP/TileImageLoader.UWP.cs b/MapControl/UWP/TileImageLoader.UWP.cs index d9224209..335832ed 100644 --- a/MapControl/UWP/TileImageLoader.UWP.cs +++ b/MapControl/UWP/TileImageLoader.UWP.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/UWP/TileSource.UWP.cs b/MapControl/UWP/TileSource.UWP.cs index e854a3e8..3110d51f 100644 --- a/MapControl/UWP/TileSource.UWP.cs +++ b/MapControl/UWP/TileSource.UWP.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/WPF/ImageFileCache.WPF.cs b/MapControl/WPF/ImageFileCache.WPF.cs index 6e8c410a..0859fc54 100644 --- a/MapControl/WPF/ImageFileCache.WPF.cs +++ b/MapControl/WPF/ImageFileCache.WPF.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/WPF/ImageLoader.WPF.cs b/MapControl/WPF/ImageLoader.WPF.cs index 019a9bde..5ffdbb14 100644 --- a/MapControl/WPF/ImageLoader.WPF.cs +++ b/MapControl/WPF/ImageLoader.WPF.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/WPF/MapBase.WPF.cs b/MapControl/WPF/MapBase.WPF.cs index 3e03b415..6ba52a65 100644 --- a/MapControl/WPF/MapBase.WPF.cs +++ b/MapControl/WPF/MapBase.WPF.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System.Windows; diff --git a/MapControl/WPF/MapControl.WPF.csproj b/MapControl/WPF/MapControl.WPF.csproj index c24b1e31..9ba7d387 100644 --- a/MapControl/WPF/MapControl.WPF.csproj +++ b/MapControl/WPF/MapControl.WPF.csproj @@ -110,24 +110,24 @@ MapPanel.cs - - MapPath.cs - - - MapPolyline.cs - MapProjection.cs MapScale.cs + + MapShape.cs + MapTileLayer.cs OrthographicProjection.cs + + PolygonCollection.cs + Pushpin.cs @@ -162,10 +162,12 @@ + - + + diff --git a/MapControl/WPF/MapGraticule.WPF.cs b/MapControl/WPF/MapGraticule.WPF.cs index 6528027e..20781e22 100644 --- a/MapControl/WPF/MapGraticule.WPF.cs +++ b/MapControl/WPF/MapGraticule.WPF.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; @@ -39,7 +39,7 @@ namespace MapControl { var projection = ParentMap?.MapProjection; - if (projection != null && !double.IsNaN(projection.LongitudeScale)) + if (projection != null && !projection.IsAzimuthal) { var bounds = projection.ViewportRectToBoundingBox(new Rect(ParentMap.RenderSize)); diff --git a/MapControl/WPF/MapMultiPolygon.WPF.cs b/MapControl/WPF/MapMultiPolygon.WPF.cs new file mode 100644 index 00000000..2bd986db --- /dev/null +++ b/MapControl/WPF/MapMultiPolygon.WPF.cs @@ -0,0 +1,51 @@ +// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control +// © 2018 Clemens Fischer +// Licensed under the Microsoft Public License (Ms-PL) + +using System.Collections.Generic; +using System.Linq; +using System.Windows; +using System.Windows.Media; + +namespace MapControl +{ + /// + /// A multi-polygon defined by a collection of collections of Locations. + /// With a FillRule of EvenOdd, this allows to draw polygons with holes. + /// + /// A PolygonCollection (with ObservableCollection of Location elements) may be used + /// for the Polygons property if collection changes of the property itself and its + /// elements are both supposed to trigger a UI update. + /// + public class MapMultiPolygon : MapShape, IWeakEventListener + { + public static readonly DependencyProperty PolygonsProperty = DependencyProperty.Register( + nameof(Polygons), typeof(IEnumerable>), typeof(MapMultiPolygon), + new PropertyMetadata(null, (o, e) => ((MapMultiPolygon)o).DataCollectionPropertyChanged(e))); + + /// + /// Gets or sets the Locations that define the multi-polygon points. + /// + public IEnumerable> Polygons + { + get { return (IEnumerable>)GetValue(PolygonsProperty); } + set { SetValue(PolygonsProperty, value); } + } + + protected override void UpdateData() + { + Data.Figures.Clear(); + + if (ParentMap != null && Polygons != null) + { + foreach (var polygon in Polygons.Where(p => p.Any())) + { + var points = polygon.Select(loc => LocationToPoint(loc)); + var polyline = new PolyLineSegment(points.Skip(1), true); + + Data.Figures.Add(new PathFigure(points.First(), new PathSegment[] { polyline }, true)); + } + } + } + } +} diff --git a/MapControl/WPF/MapOverlay.WPF.cs b/MapControl/WPF/MapOverlay.WPF.cs index a68d1388..2ae253d8 100644 --- a/MapControl/WPF/MapOverlay.WPF.cs +++ b/MapControl/WPF/MapOverlay.WPF.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System.Windows; diff --git a/MapControl/WPF/MapPanel.WPF.cs b/MapControl/WPF/MapPanel.WPF.cs index cf5a754f..4a3ca4e5 100644 --- a/MapControl/WPF/MapPanel.WPF.cs +++ b/MapControl/WPF/MapPanel.WPF.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System.Windows; @@ -9,8 +9,8 @@ namespace MapControl public partial class MapPanel { private static readonly DependencyPropertyKey ParentMapPropertyKey = DependencyProperty.RegisterAttachedReadOnly( - "ParentMap", typeof(MapBase), typeof(MapPanel), - new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits, ParentMapPropertyChanged)); + "ParentMap", typeof(MapBase), typeof(MapPanel), new FrameworkPropertyMetadata( + null, FrameworkPropertyMetadataOptions.Inherits, ParentMapPropertyChanged)); public static readonly DependencyProperty ParentMapProperty = ParentMapPropertyKey.DependencyProperty; diff --git a/MapControl/WPF/MapPath.WPF.cs b/MapControl/WPF/MapPath.WPF.cs deleted file mode 100644 index 9ee35d5e..00000000 --- a/MapControl/WPF/MapPath.WPF.cs +++ /dev/null @@ -1,64 +0,0 @@ -// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer -// Licensed under the Microsoft Public License (Ms-PL) - -using System.Windows; -using System.Windows.Media; -using System.Windows.Shapes; - -namespace MapControl -{ - public partial class MapPath : Shape - { - public static readonly DependencyProperty DataProperty = DependencyProperty.Register( - nameof(Data), typeof(Geometry), typeof(MapPath), new FrameworkPropertyMetadata( - null, FrameworkPropertyMetadataOptions.AffectsRender, DataPropertyChanged, CoerceDataProperty)); - - static MapPath() - { - StretchProperty.OverrideMetadata(typeof(MapPath), - new FrameworkPropertyMetadata { CoerceValueCallback = (o, v) => Stretch.None }); - } - - public Geometry Data - { - get { return (Geometry)GetValue(DataProperty); } - set { SetValue(DataProperty, value); } - } - - protected override Geometry DefiningGeometry - { - get { return Data; } - } - - protected override Size MeasureOverride(Size availableSize) - { - return new Size(); - } - - private static void DataPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) - { - if (!ReferenceEquals(e.OldValue, e.NewValue)) - { - var mapPath = (MapPath)obj; - - if (e.OldValue != null) - { - ((Geometry)e.OldValue).ClearValue(Geometry.TransformProperty); - } - - if (e.NewValue != null) - { - ((Geometry)e.NewValue).Transform = mapPath.viewportTransform; - } - } - } - - private static object CoerceDataProperty(DependencyObject obj, object value) - { - var data = (Geometry)value; - - return (data != null && data.IsFrozen) ? data.CloneCurrentValue() : data; - } - } -} diff --git a/MapControl/WPF/MapPolygon.WPF.cs b/MapControl/WPF/MapPolygon.WPF.cs new file mode 100644 index 00000000..3478a50d --- /dev/null +++ b/MapControl/WPF/MapPolygon.WPF.cs @@ -0,0 +1,45 @@ +// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control +// © 2018 Clemens Fischer +// Licensed under the Microsoft Public License (Ms-PL) + +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Windows; +using System.Windows.Media; + +namespace MapControl +{ + /// + /// A polygon defined by a collection of Locations. + /// + public class MapPolygon : MapShape + { + public static readonly DependencyProperty LocationsProperty = DependencyProperty.Register( + nameof(Locations), typeof(IEnumerable), typeof(MapPolygon), + new PropertyMetadata(null, (o, e) => ((MapPolygon)o).DataCollectionPropertyChanged(e))); + + /// + /// Gets or sets the Locations that define the polygon points. + /// + [TypeConverter(typeof(LocationCollectionConverter))] + public IEnumerable Locations + { + get { return (IEnumerable)GetValue(LocationsProperty); } + set { SetValue(LocationsProperty, value); } + } + + protected override void UpdateData() + { + Data.Figures.Clear(); + + if (ParentMap != null && Locations != null && Locations.Any()) + { + var points = Locations.Select(loc => LocationToPoint(loc)); + var polyline = new PolyLineSegment(points.Skip(1), true); + + Data.Figures.Add(new PathFigure(points.First(), new PathSegment[] { polyline }, true)); + } + } + } +} diff --git a/MapControl/WPF/MapPolyline.WPF.cs b/MapControl/WPF/MapPolyline.WPF.cs index 7d4540cc..5dae093c 100644 --- a/MapControl/WPF/MapPolyline.WPF.cs +++ b/MapControl/WPF/MapPolyline.WPF.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System.Collections.Generic; @@ -10,20 +10,17 @@ using System.Windows.Media; namespace MapControl { - public partial class MapPolyline + /// + /// A polyline defined by a collection of Locations. + /// + public class MapPolyline : MapShape { - public static readonly DependencyProperty FillRuleProperty = DependencyProperty.Register( - nameof(FillRule), typeof(FillRule), typeof(MapPolyline), new FrameworkPropertyMetadata( - FillRule.EvenOdd, FrameworkPropertyMetadataOptions.AffectsRender, - (o, e) => ((StreamGeometry)((MapPolyline)o).Data).FillRule = (FillRule)e.NewValue)); - - public MapPolyline() - { - Data = new StreamGeometry(); - } + public static readonly DependencyProperty LocationsProperty = DependencyProperty.Register( + nameof(Locations), typeof(IEnumerable), typeof(MapPolyline), + new PropertyMetadata(null, (o, e) => ((MapPolyline)o).DataCollectionPropertyChanged(e))); /// - /// Gets or sets the locations that define the polyline points. + /// Gets or sets the Locations that define the polyline points. /// [TypeConverter(typeof(LocationCollectionConverter))] public IEnumerable Locations @@ -34,21 +31,14 @@ namespace MapControl protected override void UpdateData() { - var geometry = (StreamGeometry)Data; + Data.Figures.Clear(); if (ParentMap != null && Locations != null && Locations.Any()) { - using (var context = geometry.Open()) - { - var points = Locations.Select(l => ParentMap.MapProjection.LocationToPoint(l)); + var points = Locations.Select(loc => LocationToPoint(loc)); + var polyline = new PolyLineSegment(points.Skip(1), true); - context.BeginFigure(points.First(), IsClosed, IsClosed); - context.PolyLineTo(points.Skip(1).ToList(), true, false); - } - } - else - { - geometry.Clear(); + Data.Figures.Add(new PathFigure(points.First(), new PathSegment[] { polyline }, false)); } } } diff --git a/MapControl/WPF/MapShape.WPF.cs b/MapControl/WPF/MapShape.WPF.cs new file mode 100644 index 00000000..aee38ff0 --- /dev/null +++ b/MapControl/WPF/MapShape.WPF.cs @@ -0,0 +1,100 @@ +// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control +// © 2018 Clemens Fischer +// Licensed under the Microsoft Public License (Ms-PL) + +using System; +using System.Collections.Specialized; +using System.Windows; +using System.Windows.Media; +using System.Windows.Shapes; + +namespace MapControl +{ + public abstract partial class MapShape : Shape, IWeakEventListener + { + public static readonly DependencyProperty FillRuleProperty = DependencyProperty.Register( + nameof(FillRule), typeof(FillRule), typeof(MapShape), + new FrameworkPropertyMetadata(FillRule.EvenOdd, FrameworkPropertyMetadataOptions.AffectsRender, + (o, e) => ((MapShape)o).Data.FillRule = (FillRule)e.NewValue)); + + /// + /// Gets or sets the FillRule of the StreamGeometry that represents the polyline. + /// + public FillRule FillRule + { + get { return (FillRule)GetValue(FillRuleProperty); } + set { SetValue(FillRuleProperty, value); } + } + + protected PathGeometry Data { get; } + + protected override Geometry DefiningGeometry + { + get { return Data; } + } + + private void ParentMapChanged() + { + if (parentMap != null) + { + var transform = new TransformGroup(); + transform.Children.Add(new TranslateTransform(GetLongitudeOffset() * parentMap.MapProjection.TrueScale, 0d)); + transform.Children.Add(parentMap.MapProjection.ViewportTransform); + + Data.Transform = transform; + } + else + { + Data.Transform = Transform.Identity; + } + + UpdateData(); + } + + private void OnViewportChanged(object sender, ViewportChangedEventArgs e) + { + var transform = (TransformGroup)Data.Transform; + var offset = (TranslateTransform)transform.Children[0]; + + offset.X = GetLongitudeOffset() * parentMap.MapProjection.TrueScale; + + if (e.ProjectionChanged) + { + transform.Children[1] = parentMap.MapProjection.ViewportTransform; + } + + if (e.ProjectionChanged || parentMap.MapProjection.IsAzimuthal) + { + UpdateData(); + } + else if (Fill != null) + { + InvalidateVisual(); // Fill brush may be rendered only partially or not at all + } + } + + protected void DataCollectionPropertyChanged(DependencyPropertyChangedEventArgs e) + { + INotifyCollectionChanged locations; + + if ((locations = e.OldValue as INotifyCollectionChanged) != null) + { + CollectionChangedEventManager.RemoveListener(locations, this); + } + + if ((locations = e.NewValue as INotifyCollectionChanged) != null) + { + CollectionChangedEventManager.AddListener(locations, this); + } + + UpdateData(); + } + + bool IWeakEventListener.ReceiveWeakEvent(Type managerType, object sender, EventArgs e) + { + UpdateData(); + + return true; + } + } +} diff --git a/MapControl/WPF/MatrixEx.WPF.cs b/MapControl/WPF/MatrixEx.WPF.cs index 7532cebd..3998b222 100644 --- a/MapControl/WPF/MatrixEx.WPF.cs +++ b/MapControl/WPF/MatrixEx.WPF.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System.Windows.Media; diff --git a/MapControl/WPF/Properties/AssemblyInfo.cs b/MapControl/WPF/Properties/AssemblyInfo.cs index e1062526..a4242f10 100644 --- a/MapControl/WPF/Properties/AssemblyInfo.cs +++ b/MapControl/WPF/Properties/AssemblyInfo.cs @@ -6,10 +6,10 @@ using System.Windows; [assembly: AssemblyDescription("XAML Map Control Library")] [assembly: AssemblyProduct("XAML Map Control")] [assembly: AssemblyCompany("Clemens Fischer")] -[assembly: AssemblyCopyright("© 2017 Clemens Fischer")] +[assembly: AssemblyCopyright("© 2018 Clemens Fischer")] [assembly: AssemblyTrademark("")] -[assembly: AssemblyVersion("4.3.0")] -[assembly: AssemblyFileVersion("4.3.0")] +[assembly: AssemblyVersion("4.4.0")] +[assembly: AssemblyFileVersion("4.4.0")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] diff --git a/MapControl/WPF/Tile.WPF.cs b/MapControl/WPF/Tile.WPF.cs index 68542982..27b1663b 100644 --- a/MapControl/WPF/Tile.WPF.cs +++ b/MapControl/WPF/Tile.WPF.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/WPF/TileImageLoader.WPF.cs b/MapControl/WPF/TileImageLoader.WPF.cs index a4299207..ab6973a6 100644 --- a/MapControl/WPF/TileImageLoader.WPF.cs +++ b/MapControl/WPF/TileImageLoader.WPF.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/WPF/TypeConverters.WPF.cs b/MapControl/WPF/TypeConverters.WPF.cs index f6db6d29..99684021 100644 --- a/MapControl/WPF/TypeConverters.WPF.cs +++ b/MapControl/WPF/TypeConverters.WPF.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/MapControl/WPF/XmlDocument.WPF.cs b/MapControl/WPF/XmlDocument.WPF.cs index 28a5a62a..2750ee45 100644 --- a/MapControl/WPF/XmlDocument.WPF.cs +++ b/MapControl/WPF/XmlDocument.WPF.cs @@ -1,5 +1,5 @@ // XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control -// © 2017 Clemens Fischer +// © 2018 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; diff --git a/SampleApps/UniversalApp/MainPage.xaml b/SampleApps/UniversalApp/MainPage.xaml index fc20d5f9..4fb4e62e 100644 --- a/SampleApps/UniversalApp/MainPage.xaml +++ b/SampleApps/UniversalApp/MainPage.xaml @@ -130,15 +130,6 @@ - - - - - - - - - diff --git a/SampleApps/UniversalApp/Properties/AssemblyInfo.cs b/SampleApps/UniversalApp/Properties/AssemblyInfo.cs index e60dd609..aa195a5e 100644 --- a/SampleApps/UniversalApp/Properties/AssemblyInfo.cs +++ b/SampleApps/UniversalApp/Properties/AssemblyInfo.cs @@ -5,10 +5,10 @@ using System.Runtime.InteropServices; [assembly: AssemblyDescription("XAML Map Control Universal Windows Sample Application")] [assembly: AssemblyProduct("XAML Map Control")] [assembly: AssemblyCompany("Clemens Fischer")] -[assembly: AssemblyCopyright("© 2017 Clemens Fischer")] +[assembly: AssemblyCopyright("© 2018 Clemens Fischer")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -[assembly: AssemblyVersion("4.3.0")] -[assembly: AssemblyFileVersion("4.3.0")] +[assembly: AssemblyVersion("4.4.0")] +[assembly: AssemblyFileVersion("4.4.0")] [assembly: AssemblyConfiguration("")] [assembly: ComVisible(false)] diff --git a/SampleApps/WpfApplication/Properties/AssemblyInfo.cs b/SampleApps/WpfApplication/Properties/AssemblyInfo.cs index 9b4005ff..ce4de64e 100644 --- a/SampleApps/WpfApplication/Properties/AssemblyInfo.cs +++ b/SampleApps/WpfApplication/Properties/AssemblyInfo.cs @@ -5,10 +5,10 @@ using System.Runtime.InteropServices; [assembly: AssemblyDescription("XAML Map Control WPF Sample Application")] [assembly: AssemblyProduct("XAML Map Control")] [assembly: AssemblyCompany("Clemens Fischer")] -[assembly: AssemblyCopyright("© 2017 Clemens Fischer")] +[assembly: AssemblyCopyright("© 2018 Clemens Fischer")] [assembly: AssemblyTrademark("")] -[assembly: AssemblyVersion("4.3.0")] -[assembly: AssemblyFileVersion("4.3.0")] +[assembly: AssemblyVersion("4.4.0")] +[assembly: AssemblyFileVersion("4.4.0")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCulture("")] [assembly: ComVisible(false)]