2013-05-07 18:12:25 +02:00
|
|
|
|
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
|
2014-01-10 20:11:39 +01:00
|
|
|
|
// Copyright © 2014 Clemens Fischer
|
2013-05-07 18:12:25 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Globalization;
|
2014-04-05 14:53:58 +02:00
|
|
|
|
#if NETFX_CORE
|
|
|
|
|
|
using Windows.Foundation;
|
|
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
|
using Windows.UI.Xaml.Media.Imaging;
|
|
|
|
|
|
using Windows.UI.Xaml.Media.Animation;
|
|
|
|
|
|
#else
|
2013-05-07 18:12:25 +02:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Media.Animation;
|
|
|
|
|
|
using System.Windows.Media.Imaging;
|
2014-04-05 14:53:58 +02:00
|
|
|
|
#endif
|
2013-05-07 18:12:25 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Map image overlay. Fills the entire viewport with a map image from a web request,
|
|
|
|
|
|
/// for example from a Web Map Service (WMS).
|
2014-04-05 14:53:58 +02:00
|
|
|
|
/// The image request Uri is specified by the UriFormat property.
|
2013-05-07 18:12:25 +02:00
|
|
|
|
/// </summary>
|
2014-04-05 14:53:58 +02:00
|
|
|
|
public partial class MapImageLayer : MapPanel
|
2013-05-07 18:12:25 +02:00
|
|
|
|
{
|
2014-04-05 14:53:58 +02:00
|
|
|
|
public static readonly DependencyProperty UriFormatProperty = DependencyProperty.Register(
|
|
|
|
|
|
"UriFormat", typeof(string), typeof(MapImageLayer),
|
|
|
|
|
|
new PropertyMetadata(null, (o, e) => ((MapImageLayer)o).UpdateImage()));
|
|
|
|
|
|
|
2013-12-05 17:56:04 +01:00
|
|
|
|
public static readonly DependencyProperty RelativeImageSizeProperty = DependencyProperty.Register(
|
2013-05-15 15:58:07 +02:00
|
|
|
|
"RelativeImageSize", typeof(double), typeof(MapImageLayer), new PropertyMetadata(1d));
|
|
|
|
|
|
|
2013-05-07 18:12:25 +02:00
|
|
|
|
private int currentImageIndex;
|
2013-12-05 17:56:04 +01:00
|
|
|
|
private bool updateInProgress;
|
2013-05-07 18:12:25 +02:00
|
|
|
|
|
|
|
|
|
|
public MapImageLayer()
|
|
|
|
|
|
{
|
|
|
|
|
|
Children.Add(new MapImage { Opacity = 0d });
|
|
|
|
|
|
Children.Add(new MapImage { Opacity = 0d });
|
|
|
|
|
|
|
2014-04-05 14:53:58 +02:00
|
|
|
|
updateTimer.Interval = TileContainer.UpdateInterval;
|
|
|
|
|
|
updateTimer.Tick += (o, e) => UpdateImage();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The format string of the image request Uri. The format must contain {X} and {Y}
|
|
|
|
|
|
/// format specifiers for the map width and height in pixels, and either
|
|
|
|
|
|
/// {w},{s},{e},{n} for the bounding box in lat/lon (like for example EPSG:4326) or
|
|
|
|
|
|
/// {W},{S},{E},{N} for the bounding box in meters (like for example EPSG:3857).
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string UriFormat
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (string)GetValue(UriFormatProperty); }
|
|
|
|
|
|
set { SetValue(UriFormatProperty, value); }
|
2013-05-07 18:12:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-05-15 16:28:57 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Relative size of the map images in relation to the current viewport size.
|
|
|
|
|
|
/// Setting a value greater than one will let MapImageLayer request images that
|
|
|
|
|
|
/// are larger than the viewport, in order to support smooth panning.
|
|
|
|
|
|
/// </summary>
|
2013-05-15 15:58:07 +02:00
|
|
|
|
public double RelativeImageSize
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (double)GetValue(RelativeImageSizeProperty); }
|
|
|
|
|
|
set { SetValue(RelativeImageSizeProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-05-07 18:12:25 +02:00
|
|
|
|
protected override void OnViewportChanged()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.OnViewportChanged();
|
|
|
|
|
|
|
|
|
|
|
|
updateTimer.Stop();
|
|
|
|
|
|
updateTimer.Start();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-04-05 14:53:58 +02:00
|
|
|
|
protected virtual BitmapSource GetBitmap(double west, double east, double south, double north, int width, int height)
|
2013-05-13 23:49:48 +02:00
|
|
|
|
{
|
2014-04-05 14:53:58 +02:00
|
|
|
|
BitmapImage image = null;
|
2013-05-13 23:49:48 +02:00
|
|
|
|
|
2014-04-05 14:53:58 +02:00
|
|
|
|
if (UriFormat != null && width > 0 && height > 0)
|
2013-05-13 23:49:48 +02:00
|
|
|
|
{
|
2014-04-05 14:53:58 +02:00
|
|
|
|
var uri = UriFormat.Replace("{X}", width.ToString()).Replace("{Y}", height.ToString());
|
2013-05-13 23:49:48 +02:00
|
|
|
|
|
2013-06-07 15:09:59 +02:00
|
|
|
|
if (uri.Contains("{W}") && uri.Contains("{S}") && uri.Contains("{E}") && uri.Contains("{N}"))
|
2013-05-15 15:58:07 +02:00
|
|
|
|
{
|
|
|
|
|
|
var p1 = ParentMap.MapTransform.Transform(new Location(south, west));
|
|
|
|
|
|
var p2 = ParentMap.MapTransform.Transform(new Location(north, east));
|
|
|
|
|
|
var arc = TileSource.EarthRadius * Math.PI / 180d;
|
|
|
|
|
|
|
|
|
|
|
|
uri = uri.
|
|
|
|
|
|
Replace("{W}", (arc * p1.X).ToString(CultureInfo.InvariantCulture)).
|
|
|
|
|
|
Replace("{S}", (arc * p1.Y).ToString(CultureInfo.InvariantCulture)).
|
|
|
|
|
|
Replace("{E}", (arc * p2.X).ToString(CultureInfo.InvariantCulture)).
|
|
|
|
|
|
Replace("{N}", (arc * p2.Y).ToString(CultureInfo.InvariantCulture));
|
|
|
|
|
|
}
|
2013-06-07 15:09:59 +02:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
uri = uri.
|
|
|
|
|
|
Replace("{w}", west.ToString(CultureInfo.InvariantCulture)).
|
|
|
|
|
|
Replace("{s}", south.ToString(CultureInfo.InvariantCulture)).
|
|
|
|
|
|
Replace("{e}", east.ToString(CultureInfo.InvariantCulture)).
|
|
|
|
|
|
Replace("{n}", north.ToString(CultureInfo.InvariantCulture));
|
|
|
|
|
|
}
|
2013-05-13 23:49:48 +02:00
|
|
|
|
|
2014-04-05 14:53:58 +02:00
|
|
|
|
image = new BitmapImage(new Uri(uri));
|
2013-05-13 23:49:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return image;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-04-05 14:53:58 +02:00
|
|
|
|
protected void UpdateImage()
|
2013-05-07 18:12:25 +02:00
|
|
|
|
{
|
2013-06-19 18:57:54 +02:00
|
|
|
|
if (updateInProgress)
|
|
|
|
|
|
{
|
2014-04-05 14:53:58 +02:00
|
|
|
|
updateTimer.Start(); // update image on next timer tick
|
2013-06-19 18:57:54 +02:00
|
|
|
|
}
|
2014-04-05 14:53:58 +02:00
|
|
|
|
else
|
2013-05-07 18:12:25 +02:00
|
|
|
|
{
|
2014-04-05 14:53:58 +02:00
|
|
|
|
updateTimer.Stop();
|
2013-05-15 15:58:07 +02:00
|
|
|
|
|
2014-04-05 14:53:58 +02:00
|
|
|
|
if (ParentMap != null && ActualWidth > 0 && ActualHeight > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
updateInProgress = true;
|
2013-12-20 17:05:10 +01:00
|
|
|
|
|
2014-04-05 14:53:58 +02:00
|
|
|
|
var relativeSize = Math.Max(RelativeImageSize, 1d);
|
|
|
|
|
|
var width = ActualWidth * relativeSize;
|
|
|
|
|
|
var height = ActualHeight * relativeSize;
|
|
|
|
|
|
var dx = (ActualWidth - width) / 2d;
|
|
|
|
|
|
var dy = (ActualHeight - height) / 2d;
|
|
|
|
|
|
|
|
|
|
|
|
var loc1 = ParentMap.ViewportPointToLocation(new Point(dx, dy));
|
|
|
|
|
|
var loc2 = ParentMap.ViewportPointToLocation(new Point(dx + width, dy));
|
|
|
|
|
|
var loc3 = ParentMap.ViewportPointToLocation(new Point(dx, dy + height));
|
|
|
|
|
|
var loc4 = ParentMap.ViewportPointToLocation(new Point(dx + width, dy + height));
|
2013-05-15 15:58:07 +02:00
|
|
|
|
|
|
|
|
|
|
var west = Math.Min(loc1.Longitude, Math.Min(loc2.Longitude, Math.Min(loc3.Longitude, loc4.Longitude)));
|
|
|
|
|
|
var east = Math.Max(loc1.Longitude, Math.Max(loc2.Longitude, Math.Max(loc3.Longitude, loc4.Longitude)));
|
|
|
|
|
|
var south = Math.Min(loc1.Latitude, Math.Min(loc2.Latitude, Math.Min(loc3.Latitude, loc4.Latitude)));
|
|
|
|
|
|
var north = Math.Max(loc1.Latitude, Math.Max(loc2.Latitude, Math.Max(loc3.Latitude, loc4.Latitude)));
|
2013-05-07 18:12:25 +02:00
|
|
|
|
|
2013-12-20 17:05:10 +01:00
|
|
|
|
var p1 = ParentMap.MapTransform.Transform(new Location(south, west));
|
|
|
|
|
|
var p2 = ParentMap.MapTransform.Transform(new Location(north, east));
|
|
|
|
|
|
|
|
|
|
|
|
width = Math.Round((p2.X - p1.X) * ParentMap.ViewportScale);
|
|
|
|
|
|
height = Math.Round((p2.Y - p1.Y) * ParentMap.ViewportScale);
|
|
|
|
|
|
|
2014-04-05 14:53:58 +02:00
|
|
|
|
var image = GetBitmap(west, east, south, north, (int)width, (int)height);
|
2013-12-20 17:05:10 +01:00
|
|
|
|
|
2014-04-05 14:53:58 +02:00
|
|
|
|
UpdateImage(west, east, south, north, image);
|
|
|
|
|
|
}
|
2013-05-15 15:58:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2013-05-07 18:12:25 +02:00
|
|
|
|
|
2014-04-05 14:53:58 +02:00
|
|
|
|
private void UpdateImage(double west, double east, double south, double north, BitmapSource image)
|
2013-05-15 15:58:07 +02:00
|
|
|
|
{
|
2014-04-05 14:53:58 +02:00
|
|
|
|
currentImageIndex = (currentImageIndex + 1) % 2;
|
2013-05-15 15:58:07 +02:00
|
|
|
|
var mapImage = (MapImage)Children[currentImageIndex];
|
|
|
|
|
|
|
|
|
|
|
|
mapImage.Source = null;
|
2013-05-25 08:53:50 +02:00
|
|
|
|
mapImage.North = double.NaN; // avoid frequent MapRectangle.UpdateData() calls
|
2013-05-15 15:58:07 +02:00
|
|
|
|
mapImage.West = west;
|
|
|
|
|
|
mapImage.East = east;
|
|
|
|
|
|
mapImage.South = south;
|
|
|
|
|
|
mapImage.North = north;
|
2014-04-05 14:53:58 +02:00
|
|
|
|
|
|
|
|
|
|
if (image != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
mapImage.Source = image;
|
|
|
|
|
|
AddDownloadEventHandlers(image);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
BlendImages();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void BlendImages()
|
|
|
|
|
|
{
|
|
|
|
|
|
#if NETFX_CORE
|
|
|
|
|
|
var duration = TimeSpan.Zero; // animation not working in WinRT (?)
|
|
|
|
|
|
#else
|
|
|
|
|
|
var duration = Tile.AnimationDuration;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
var mapImage = (MapImage)Children[currentImageIndex];
|
|
|
|
|
|
var fadeOut = new DoubleAnimation { To = 0d, Duration = duration };
|
|
|
|
|
|
|
|
|
|
|
|
if (mapImage.Source != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
mapImage.BeginAnimation(UIElement.OpacityProperty,
|
|
|
|
|
|
new DoubleAnimation { To = 1d, Duration = duration });
|
|
|
|
|
|
|
|
|
|
|
|
fadeOut.BeginTime = duration;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mapImage = (MapImage)Children[(currentImageIndex + 1) % 2];
|
|
|
|
|
|
mapImage.BeginAnimation(UIElement.OpacityProperty, fadeOut);
|
|
|
|
|
|
|
|
|
|
|
|
updateInProgress = false;
|
2013-05-07 18:12:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|