mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-01-23 00:50:16 +01:00
GeoImage and GroundOverlay implementation
This commit is contained in:
parent
1b0e73dc35
commit
020a871714
|
|
@ -74,11 +74,11 @@ namespace MapControl
|
|||
image.SetValue(SourcePathProperty, value);
|
||||
}
|
||||
|
||||
public static Image LoadGeoImage(string sourcePath)
|
||||
public static async Task<Image> CreateAsync(string sourcePath)
|
||||
{
|
||||
var image = new Image();
|
||||
|
||||
SetSourcePath(image, sourcePath);
|
||||
await LoadGeoImageAsync(image, sourcePath);
|
||||
|
||||
return image;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ namespace MapControl
|
|||
|
||||
public static readonly DependencyProperty SourcePathProperty =
|
||||
DependencyPropertyHelper.Register<GroundOverlay, string>(nameof(SourcePath), null,
|
||||
async (overlay, oldValue, newValue) => await overlay.SourcePathPropertyChanged(newValue));
|
||||
async (groundOverlay, oldValue, newValue) => await groundOverlay.LoadAsync(newValue));
|
||||
|
||||
public string SourcePath
|
||||
{
|
||||
|
|
@ -54,7 +54,16 @@ namespace MapControl
|
|||
set => SetValue(SourcePathProperty, value);
|
||||
}
|
||||
|
||||
private async Task SourcePathPropertyChanged(string sourcePath)
|
||||
public static async Task<GroundOverlay> CreateAsync(string sourcePath)
|
||||
{
|
||||
var groundOverlay = new GroundOverlay();
|
||||
|
||||
await groundOverlay.LoadAsync(sourcePath);
|
||||
|
||||
return groundOverlay;
|
||||
}
|
||||
|
||||
public async Task LoadAsync(string sourcePath)
|
||||
{
|
||||
IEnumerable<ImageOverlay> imageOverlays = null;
|
||||
|
||||
|
|
@ -66,11 +75,11 @@ namespace MapControl
|
|||
|
||||
if (ext == ".kmz")
|
||||
{
|
||||
imageOverlays = await ReadGroundOverlaysFromArchiveAsync(sourcePath);
|
||||
imageOverlays = await LoadGroundOverlaysFromArchiveAsync(sourcePath);
|
||||
}
|
||||
else if (ext == ".kml")
|
||||
{
|
||||
imageOverlays = await ReadGroundOverlaysFromFileAsync(sourcePath);
|
||||
imageOverlays = await LoadGroundOverlaysFromFileAsync(sourcePath);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
@ -98,7 +107,7 @@ namespace MapControl
|
|||
}
|
||||
}
|
||||
|
||||
private static async Task<IEnumerable<ImageOverlay>> ReadGroundOverlaysFromArchiveAsync(string archiveFilePath)
|
||||
private static async Task<IEnumerable<ImageOverlay>> LoadGroundOverlaysFromArchiveAsync(string archiveFilePath)
|
||||
{
|
||||
using (var archive = ZipFile.OpenRead(archiveFilePath))
|
||||
{
|
||||
|
|
@ -141,7 +150,7 @@ namespace MapControl
|
|||
}
|
||||
}
|
||||
|
||||
private static async Task<IEnumerable<ImageOverlay>> ReadGroundOverlaysFromFileAsync(string docFilePath)
|
||||
private static async Task<IEnumerable<ImageOverlay>> LoadGroundOverlaysFromFileAsync(string docFilePath)
|
||||
{
|
||||
docFilePath = FilePath.GetFullPath(docFilePath);
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ using System.Collections.Specialized;
|
|||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
#if WPF
|
||||
using System.Windows;
|
||||
#elif UWP
|
||||
|
|
@ -25,7 +26,7 @@ namespace MapControl
|
|||
{
|
||||
public static readonly DependencyProperty SourcePathsProperty =
|
||||
DependencyPropertyHelper.Register<MapOverlaysPanel, IEnumerable<string>>(nameof(SourcePaths), null,
|
||||
(control, oldValue, newValue) => control.SourcePathsPropertyChanged(oldValue, newValue));
|
||||
async (control, oldValue, newValue) => await control.SourcePathsPropertyChanged(oldValue, newValue));
|
||||
|
||||
public IEnumerable<string> SourcePaths
|
||||
{
|
||||
|
|
@ -33,7 +34,7 @@ namespace MapControl
|
|||
set => SetValue(SourcePathsProperty, value);
|
||||
}
|
||||
|
||||
private void SourcePathsPropertyChanged(IEnumerable<string> oldSourcePaths, IEnumerable<string> newSourcePaths)
|
||||
private async Task SourcePathsPropertyChanged(IEnumerable<string> oldSourcePaths, IEnumerable<string> newSourcePaths)
|
||||
{
|
||||
Children.Clear();
|
||||
|
||||
|
|
@ -49,16 +50,16 @@ namespace MapControl
|
|||
newCollection.CollectionChanged += SourcePathsCollectionChanged;
|
||||
}
|
||||
|
||||
AddOverlays(0, newSourcePaths);
|
||||
await AddOverlays(0, newSourcePaths);
|
||||
}
|
||||
}
|
||||
|
||||
private void SourcePathsCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
|
||||
private async void SourcePathsCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
|
||||
{
|
||||
switch (args.Action)
|
||||
{
|
||||
case NotifyCollectionChangedAction.Add:
|
||||
AddOverlays(args.NewStartingIndex, args.NewItems.Cast<string>());
|
||||
await AddOverlays(args.NewStartingIndex, args.NewItems.Cast<string>());
|
||||
break;
|
||||
|
||||
case NotifyCollectionChangedAction.Remove:
|
||||
|
|
@ -67,33 +68,33 @@ namespace MapControl
|
|||
|
||||
case NotifyCollectionChangedAction.Move:
|
||||
RemoveOverlays(args.OldStartingIndex, args.OldItems.Count);
|
||||
AddOverlays(args.NewStartingIndex, args.NewItems.Cast<string>());
|
||||
await AddOverlays(args.NewStartingIndex, args.NewItems.Cast<string>());
|
||||
break;
|
||||
|
||||
case NotifyCollectionChangedAction.Replace:
|
||||
ReplaceOverlays(args.NewStartingIndex, args.NewItems.Cast<string>());
|
||||
await ReplaceOverlays(args.NewStartingIndex, args.NewItems.Cast<string>());
|
||||
break;
|
||||
|
||||
case NotifyCollectionChangedAction.Reset:
|
||||
Children.Clear();
|
||||
AddOverlays(0, SourcePaths);
|
||||
await AddOverlays(0, SourcePaths);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void AddOverlays(int index, IEnumerable<string> sourcePaths)
|
||||
private async Task AddOverlays(int index, IEnumerable<string> sourcePaths)
|
||||
{
|
||||
foreach (var sourcePath in sourcePaths)
|
||||
{
|
||||
Children.Insert(index++, CreateOverlay(sourcePath));
|
||||
Children.Insert(index++, await CreateOverlay(sourcePath));
|
||||
}
|
||||
}
|
||||
|
||||
private void ReplaceOverlays(int index, IEnumerable<string> sourcePaths)
|
||||
private async Task ReplaceOverlays(int index, IEnumerable<string> sourcePaths)
|
||||
{
|
||||
foreach (var sourcePath in sourcePaths)
|
||||
{
|
||||
Children[index++] = CreateOverlay(sourcePath);
|
||||
Children[index++] = await CreateOverlay(sourcePath);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -105,7 +106,7 @@ namespace MapControl
|
|||
}
|
||||
}
|
||||
|
||||
protected virtual FrameworkElement CreateOverlay(string sourcePath)
|
||||
protected virtual async Task<FrameworkElement> CreateOverlay(string sourcePath)
|
||||
{
|
||||
FrameworkElement overlay;
|
||||
var ext = Path.GetExtension(sourcePath).ToLower();
|
||||
|
|
@ -114,11 +115,11 @@ namespace MapControl
|
|||
{
|
||||
if (ext == ".kmz" || ext == ".kml")
|
||||
{
|
||||
overlay = new GroundOverlay { SourcePath = sourcePath };
|
||||
overlay = await GroundOverlay.CreateAsync(sourcePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
overlay = GeoImage.LoadGeoImage(sourcePath);
|
||||
overlay = await GeoImage.CreateAsync(sourcePath);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ using System;
|
|||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
#if WPF
|
||||
using System.Windows;
|
||||
using System.Windows.Markup;
|
||||
|
|
@ -38,9 +39,9 @@ namespace MapControl.UiTools
|
|||
|
||||
public string Text { get; set; }
|
||||
|
||||
public Func<FrameworkElement> LayerFactory { get; set; }
|
||||
public Func<Task<FrameworkElement>> LayerFactory { get; set; }
|
||||
|
||||
public FrameworkElement GetLayer() => Layer ?? (Layer = LayerFactory?.Invoke());
|
||||
public async Task<FrameworkElement> GetLayer() => Layer ?? (Layer = await LayerFactory?.Invoke());
|
||||
}
|
||||
|
||||
#if WPF
|
||||
|
|
@ -55,13 +56,13 @@ namespace MapControl.UiTools
|
|||
public MapLayersMenuButton()
|
||||
: base("\uE81E")
|
||||
{
|
||||
((INotifyCollectionChanged)MapLayers).CollectionChanged += (s, e) => InitializeMenu();
|
||||
((INotifyCollectionChanged)MapOverlays).CollectionChanged += (s, e) => InitializeMenu();
|
||||
((INotifyCollectionChanged)MapLayers).CollectionChanged += async (s, e) => await InitializeMenu();
|
||||
((INotifyCollectionChanged)MapOverlays).CollectionChanged += async (s, e) => await InitializeMenu();
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty MapProperty =
|
||||
DependencyPropertyHelper.Register<MapLayersMenuButton, MapBase>(nameof(Map), null,
|
||||
(button, oldValue, newValue) => button.InitializeMenu());
|
||||
async (button, oldValue, newValue) => await button.InitializeMenu());
|
||||
|
||||
public MapBase Map
|
||||
{
|
||||
|
|
@ -76,7 +77,7 @@ namespace MapControl.UiTools
|
|||
|
||||
public Collection<MapLayerItem> MapOverlays { get; } = new ObservableCollection<MapLayerItem>();
|
||||
|
||||
private void InitializeMenu()
|
||||
private async Task InitializeMenu()
|
||||
{
|
||||
if (Map != null)
|
||||
{
|
||||
|
|
@ -104,25 +105,25 @@ namespace MapControl.UiTools
|
|||
|
||||
if (initialLayer != null)
|
||||
{
|
||||
SetMapLayer(initialLayer);
|
||||
SetMapLayer(await initialLayer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void MapLayerClicked(object sender, RoutedEventArgs e)
|
||||
private async void MapLayerClicked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var item = (FrameworkElement)sender;
|
||||
var mapLayerItem = (MapLayerItem)item.Tag;
|
||||
|
||||
SetMapLayer(mapLayerItem.GetLayer());
|
||||
SetMapLayer(await mapLayerItem.GetLayer());
|
||||
}
|
||||
|
||||
private void MapOverlayClicked(object sender, RoutedEventArgs e)
|
||||
private async void MapOverlayClicked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var item = (FrameworkElement)sender;
|
||||
var mapLayerItem = (MapLayerItem)item.Tag;
|
||||
|
||||
ToggleMapOverlay(mapLayerItem.GetLayer());
|
||||
ToggleMapOverlay(await mapLayerItem.GetLayer());
|
||||
}
|
||||
|
||||
private void SetMapLayer(FrameworkElement layer)
|
||||
|
|
|
|||
Loading…
Reference in a new issue