File scoped namespaces

This commit is contained in:
ClemensFischer 2026-04-13 17:14:49 +02:00
parent c14377f976
commit 65aba44af6
152 changed files with 11962 additions and 12115 deletions

View file

@ -2,23 +2,22 @@
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
namespace SampleApplication
namespace SampleApplication;
public partial class App : Application
{
public partial class App : Application
public override void Initialize()
{
public override void Initialize()
AvaloniaXamlLoader.Load(this);
}
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
AvaloniaXamlLoader.Load(this);
desktop.MainWindow = new MainWindow();
}
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow();
}
base.OnFrameworkInitializationCompleted();
}
base.OnFrameworkInitializationCompleted();
}
}

View file

@ -7,139 +7,138 @@ using System.Diagnostics;
using System.Globalization;
using System.Linq;
namespace SampleApplication
namespace SampleApplication;
public partial class MainWindow : Window
{
public partial class MainWindow : Window
public MainWindow()
{
public MainWindow()
ImageLoader.HttpClient.DefaultRequestHeaders.Add("User-Agent", "XAML Map Control Avalonia Sample Application");
var loggerFactory = LoggerFactory.Create(builder => builder.AddDebug().SetMinimumLevel(LogLevel.Information));
ImageLoader.LoggerFactory = loggerFactory;
var tileCache = new MapControl.Caching.ImageFileCache(TileImageLoader.DefaultCacheFolder, loggerFactory);
TileImageLoader.Cache = tileCache;
Closed += (s, e) => tileCache.Dispose();
InitializeComponent();
AddTestLayers();
}
partial void AddTestLayers();
private void MapItemsControlSelectionChanged(object sender, SelectionChangedEventArgs e)
{
Debug.WriteLine("SelectedItems: " + string.Join(", ", ((MapItemsControl)sender).SelectedItems.OfType<PointItem>().Select(item => item.Name)));
}
private void ResetHeadingButtonClick(object sender, Avalonia.Interactivity.RoutedEventArgs e)
{
map.TargetHeading = 0d;
}
private void MapDoubleTapped(object sender, TappedEventArgs e)
{
if (e.Source == map)
{
ImageLoader.HttpClient.DefaultRequestHeaders.Add("User-Agent", "XAML Map Control Avalonia Sample Application");
var loggerFactory = LoggerFactory.Create(builder => builder.AddDebug().SetMinimumLevel(LogLevel.Information));
ImageLoader.LoggerFactory = loggerFactory;
var tileCache = new MapControl.Caching.ImageFileCache(TileImageLoader.DefaultCacheFolder, loggerFactory);
TileImageLoader.Cache = tileCache;
Closed += (s, e) => tileCache.Dispose();
InitializeComponent();
AddTestLayers();
}
partial void AddTestLayers();
private void MapItemsControlSelectionChanged(object sender, SelectionChangedEventArgs e)
{
Debug.WriteLine("SelectedItems: " + string.Join(", ", ((MapItemsControl)sender).SelectedItems.OfType<PointItem>().Select(item => item.Name)));
}
private void ResetHeadingButtonClick(object sender, Avalonia.Interactivity.RoutedEventArgs e)
{
map.TargetHeading = 0d;
}
private void MapDoubleTapped(object sender, TappedEventArgs e)
{
if (e.Source == map)
{
map.TargetCenter = map.ViewToLocation(e.GetPosition(map));
}
}
private async void MapPointerPressed(object sender, PointerPressedEventArgs e)
{
if (e.Pointer.Type == PointerType.Mouse)
{
var point = e.GetCurrentPoint(map);
if (point.Properties.IsLeftButtonPressed &&
e.KeyModifiers.HasFlag(KeyModifiers.Control) &&
map.MapLayer is WmsImageLayer wmsLayer)
{
Debug.WriteLine(await wmsLayer.GetFeatureInfoAsync(e.GetPosition(map)));
}
else if (point.Properties.IsRightButtonPressed)
{
e.Pointer.Capture(map);
var location = map.ViewToLocation(point.Position);
map.Cursor = new Cursor(StandardCursorType.Cross);
measurementLine.IsVisible = true;
measurementLine.Locations = new LocationCollection(location);
}
}
}
private void MapPointerReleased(object sender, PointerReleasedEventArgs e)
{
if (e.Pointer.Captured == map)
{
e.Pointer.Capture(null);
map.Cursor = null;
measurementLine.IsVisible = false;
measurementLine.Locations = null;
}
}
private void MapPointerMoved(object sender, PointerEventArgs e)
{
var location = map.ViewToLocation(e.GetPosition(map));
mouseLocation.IsVisible = true;
mouseLocation.Text = GetLatLonText(location);
if (measurementLine.Locations != null)
{
var start = measurementLine.Locations.First();
measurementLine.Locations = LocationCollection.GeodesicLocations(start, location);
mouseLocation.Text += GetDistanceText(location.GetDistance(start));
}
}
private void MapPointerExited(object sender, PointerEventArgs e)
{
mouseLocation.IsVisible = false;
mouseLocation.Text = "";
}
private static string GetLatLonText(MapControl.Location location)
{
var latitude = (int)Math.Round(location.Latitude * 60000d);
var longitude = (int)Math.Round(MapControl.Location.NormalizeLongitude(location.Longitude) * 60000d);
var latHemisphere = 'N';
var lonHemisphere = 'E';
if (latitude < 0)
{
latitude = -latitude;
latHemisphere = 'S';
}
if (longitude < 0)
{
longitude = -longitude;
lonHemisphere = 'W';
}
return string.Format(CultureInfo.InvariantCulture,
"{0} {1:00} {2:00.000}\n{3} {4:000} {5:00.000}",
latHemisphere, latitude / 60000, (latitude % 60000) / 1000d,
lonHemisphere, longitude / 60000, (longitude % 60000) / 1000d);
}
private static string GetDistanceText(double distance)
{
var unit = "m";
if (distance >= 1000d)
{
distance /= 1000d;
unit = "km";
}
var distanceFormat = distance >= 100d ? "F0" : "F1";
return string.Format(CultureInfo.InvariantCulture, "\n {0:" + distanceFormat + "} {1}", distance, unit);
map.TargetCenter = map.ViewToLocation(e.GetPosition(map));
}
}
private async void MapPointerPressed(object sender, PointerPressedEventArgs e)
{
if (e.Pointer.Type == PointerType.Mouse)
{
var point = e.GetCurrentPoint(map);
if (point.Properties.IsLeftButtonPressed &&
e.KeyModifiers.HasFlag(KeyModifiers.Control) &&
map.MapLayer is WmsImageLayer wmsLayer)
{
Debug.WriteLine(await wmsLayer.GetFeatureInfoAsync(e.GetPosition(map)));
}
else if (point.Properties.IsRightButtonPressed)
{
e.Pointer.Capture(map);
var location = map.ViewToLocation(point.Position);
map.Cursor = new Cursor(StandardCursorType.Cross);
measurementLine.IsVisible = true;
measurementLine.Locations = new LocationCollection(location);
}
}
}
private void MapPointerReleased(object sender, PointerReleasedEventArgs e)
{
if (e.Pointer.Captured == map)
{
e.Pointer.Capture(null);
map.Cursor = null;
measurementLine.IsVisible = false;
measurementLine.Locations = null;
}
}
private void MapPointerMoved(object sender, PointerEventArgs e)
{
var location = map.ViewToLocation(e.GetPosition(map));
mouseLocation.IsVisible = true;
mouseLocation.Text = GetLatLonText(location);
if (measurementLine.Locations != null)
{
var start = measurementLine.Locations.First();
measurementLine.Locations = LocationCollection.GeodesicLocations(start, location);
mouseLocation.Text += GetDistanceText(location.GetDistance(start));
}
}
private void MapPointerExited(object sender, PointerEventArgs e)
{
mouseLocation.IsVisible = false;
mouseLocation.Text = "";
}
private static string GetLatLonText(MapControl.Location location)
{
var latitude = (int)Math.Round(location.Latitude * 60000d);
var longitude = (int)Math.Round(MapControl.Location.NormalizeLongitude(location.Longitude) * 60000d);
var latHemisphere = 'N';
var lonHemisphere = 'E';
if (latitude < 0)
{
latitude = -latitude;
latHemisphere = 'S';
}
if (longitude < 0)
{
longitude = -longitude;
lonHemisphere = 'W';
}
return string.Format(CultureInfo.InvariantCulture,
"{0} {1:00} {2:00.000}\n{3} {4:000} {5:00.000}",
latHemisphere, latitude / 60000, (latitude % 60000) / 1000d,
lonHemisphere, longitude / 60000, (longitude % 60000) / 1000d);
}
private static string GetDistanceText(double distance)
{
var unit = "m";
if (distance >= 1000d)
{
distance /= 1000d;
unit = "km";
}
var distanceFormat = distance >= 100d ? "F0" : "F1";
return string.Format(CultureInfo.InvariantCulture, "\n {0:" + distanceFormat + "} {1}", distance, unit);
}
}

View file

@ -1,24 +1,23 @@
using Avalonia;
using System;
namespace SampleApplication
{
class Program
{
[STAThread]
public static void Main(string[] args)
{
BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
}
namespace SampleApplication;
public static AppBuilder BuildAvaloniaApp()
{
return AppBuilder
.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace();
}
class Program
{
[STAThread]
public static void Main(string[] args)
{
BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
}
public static AppBuilder BuildAvaloniaApp()
{
return AppBuilder
.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace();
}
}

View file

@ -1,8 +1,7 @@
using System.Windows;
namespace ProjectionDemo
namespace ProjectionDemo;
public partial class App : Application
{
public partial class App : Application
{
}
}

View file

@ -7,139 +7,138 @@ using System.Linq;
using System.Windows;
using System.Windows.Input;
namespace ProjectionDemo
namespace ProjectionDemo;
public partial class MainWindow : Window
{
public partial class MainWindow : Window
private readonly ViewModel viewModel = new();
public MainWindow()
{
private readonly ViewModel viewModel = new();
InitializeComponent();
}
public MainWindow()
private void Window_Loaded(object sender, RoutedEventArgs e)
{
viewModel.Projections.Add(new WebMercatorProjection());
viewModel.Projections.Add(new Etrs89UtmProjection(32));
viewModel.Layers.Add(
"OpenStreetMap WMS",
new WmsImageLayer
{
ServiceUri = new Uri("http://ows.terrestris.de/osm/service"),
RequestLayers = "OSM-WMS"
});
viewModel.Layers.Add(
"TopPlusOpen WMS",
new WmsImageLayer
{
ServiceUri = new Uri("https://sgx.geodatenzentrum.de/wms_topplus_open"),
RequestLayers = "web"
});
viewModel.Layers.Add(
"Basemap.de WMS",
new WmsImageLayer
{
ServiceUri = new Uri("https://sgx.geodatenzentrum.de/wms_basemapde"),
RequestLayers = "de_basemapde_web_raster_farbe"
});
viewModel.Layers.Add(
"Orthophotos Wiesbaden",
new WmsImageLayer
{
ServiceUri = new Uri("https://geoportal.wiesbaden.de/cgi-bin/mapserv.fcgi?map=d:/openwimap/umn/map/orthophoto/orthophotos.map"),
RequestLayers = "orthophoto2023"
});
viewModel.CurrentProjection = viewModel.Projections[0];
viewModel.CurrentLayer = viewModel.Layers.First().Value;
DataContext = viewModel;
}
private void Map_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
var map = (MapBase)sender;
var pos = e.GetPosition(map);
viewModel.PushpinLocation = map.ViewToLocation(pos);
}
}
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public List<MapProjection> Projections { get; } = [];
public Dictionary<string, IMapLayer> Layers { get; } = [];
public MapProjection CurrentProjection
{
get;
set
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
viewModel.Projections.Add(new WebMercatorProjection());
viewModel.Projections.Add(new Etrs89UtmProjection(32));
viewModel.Layers.Add(
"OpenStreetMap WMS",
new WmsImageLayer
{
ServiceUri = new Uri("http://ows.terrestris.de/osm/service"),
RequestLayers = "OSM-WMS"
});
viewModel.Layers.Add(
"TopPlusOpen WMS",
new WmsImageLayer
{
ServiceUri = new Uri("https://sgx.geodatenzentrum.de/wms_topplus_open"),
RequestLayers = "web"
});
viewModel.Layers.Add(
"Basemap.de WMS",
new WmsImageLayer
{
ServiceUri = new Uri("https://sgx.geodatenzentrum.de/wms_basemapde"),
RequestLayers = "de_basemapde_web_raster_farbe"
});
viewModel.Layers.Add(
"Orthophotos Wiesbaden",
new WmsImageLayer
{
ServiceUri = new Uri("https://geoportal.wiesbaden.de/cgi-bin/mapserv.fcgi?map=d:/openwimap/umn/map/orthophoto/orthophotos.map"),
RequestLayers = "orthophoto2023"
});
viewModel.CurrentProjection = viewModel.Projections[0];
viewModel.CurrentLayer = viewModel.Layers.First().Value;
DataContext = viewModel;
}
private void Map_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
var map = (MapBase)sender;
var pos = e.GetPosition(map);
viewModel.PushpinLocation = map.ViewToLocation(pos);
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentProjection)));
}
}
public class ViewModel : INotifyPropertyChanged
public IMapLayer CurrentLayer
{
public event PropertyChangedEventHandler PropertyChanged;
public List<MapProjection> Projections { get; } = [];
public Dictionary<string, IMapLayer> Layers { get; } = [];
public MapProjection CurrentProjection
get;
set
{
get;
set
{
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentProjection)));
}
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentLayer)));
}
}
public IMapLayer CurrentLayer
public Location PushpinLocation
{
get;
set
{
get;
set
{
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentLayer)));
}
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PushpinLocation)));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PushpinText)));
}
}
public Location PushpinLocation
public string PushpinText
{
get
{
get;
set
if (PushpinLocation == null)
{
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PushpinLocation)));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PushpinText)));
return null;
}
}
public string PushpinText
{
get
var latitude = (int)Math.Round(PushpinLocation.Latitude * 36000);
var longitude = (int)Math.Round(Location.NormalizeLongitude(PushpinLocation.Longitude) * 36000);
var latHemisphere = 'N';
var lonHemisphere = 'E';
if (latitude < 0)
{
if (PushpinLocation == null)
{
return null;
}
var latitude = (int)Math.Round(PushpinLocation.Latitude * 36000);
var longitude = (int)Math.Round(Location.NormalizeLongitude(PushpinLocation.Longitude) * 36000);
var latHemisphere = 'N';
var lonHemisphere = 'E';
if (latitude < 0)
{
latitude = -latitude;
latHemisphere = 'S';
}
if (longitude < 0)
{
longitude = -longitude;
lonHemisphere = 'W';
}
return string.Format(CultureInfo.InvariantCulture,
"{0} {1:00} {2:00} {3:00.0}\n{4} {5:000} {6:00} {7:00.0}",
latHemisphere, latitude / 36000, (latitude / 600) % 60, (latitude % 600) / 10d,
lonHemisphere, longitude / 36000, (longitude / 600) % 60, (longitude % 600) / 10d);
latitude = -latitude;
latHemisphere = 'S';
}
if (longitude < 0)
{
longitude = -longitude;
lonHemisphere = 'W';
}
return string.Format(CultureInfo.InvariantCulture,
"{0} {1:00} {2:00} {3:00.0}\n{4} {5:000} {6:00} {7:00.0}",
latHemisphere, latitude / 36000, (latitude / 600) % 60, (latitude % 600) / 10d,
lonHemisphere, longitude / 36000, (longitude / 600) % 60, (longitude % 600) / 10d);
}
}
}

View file

@ -13,33 +13,32 @@ using Microsoft.UI.Xaml.Data;
using Avalonia.Data.Converters;
#endif
namespace SampleApplication
namespace SampleApplication;
public class MapHeadingToVisibilityConverter : IValueConverter
{
public class MapHeadingToVisibilityConverter : IValueConverter
public object Convert(object value, Type targetType, object parameter, string language)
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var visible = (double)value != 0d;
var visible = (double)value != 0d;
#if AVALONIA
return visible;
return visible;
#else
return visible ? Visibility.Visible : Visibility.Collapsed;
return visible ? Visibility.Visible : Visibility.Collapsed;
#endif
}
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Convert(value, targetType, parameter, culture.ToString());
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Convert(value, targetType, parameter, culture.ToString());
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

View file

@ -3,113 +3,112 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
namespace SampleApplication
namespace SampleApplication;
public class PointItem
{
public class PointItem
public string Name { get; set; }
public Location Location { get; set; }
}
public class PolylineItem
{
public LocationCollection Locations { get; set; }
}
public class MapViewModel : INotifyPropertyChanged
{
public List<PointItem> Points { get; } = new List<PointItem>();
public List<PointItem> Pushpins { get; } = new List<PointItem>();
public List<PolylineItem> Polylines { get; } = new List<PolylineItem>();
public event PropertyChangedEventHandler PropertyChanged;
private PointItem selectedPushpin;
public PointItem SelectedPushpin
{
public string Name { get; set; }
public Location Location { get; set; }
get => selectedPushpin;
set
{
selectedPushpin = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedPushpin)));
Debug.WriteLine("SelectedPushpin: " + (selectedPushpin?.Name ?? "None"));
}
}
public class PolylineItem
public MapViewModel()
{
public LocationCollection Locations { get; set; }
}
public class MapViewModel : INotifyPropertyChanged
{
public List<PointItem> Points { get; } = new List<PointItem>();
public List<PointItem> Pushpins { get; } = new List<PointItem>();
public List<PolylineItem> Polylines { get; } = new List<PolylineItem>();
public event PropertyChangedEventHandler PropertyChanged;
private PointItem selectedPushpin;
public PointItem SelectedPushpin
Points.Add(new PointItem
{
get => selectedPushpin;
set
{
selectedPushpin = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedPushpin)));
Debug.WriteLine("SelectedPushpin: " + (selectedPushpin?.Name ?? "None"));
}
}
Name = "Steinbake Leitdamm",
Location = new Location(53.51217, 8.16603)
});
public MapViewModel()
Points.Add(new PointItem
{
Points.Add(new PointItem
{
Name = "Steinbake Leitdamm",
Location = new Location(53.51217, 8.16603)
});
Name = "Buhne 2",
Location = new Location(53.50926, 8.15815)
});
Points.Add(new PointItem
{
Name = "Buhne 2",
Location = new Location(53.50926, 8.15815)
});
Points.Add(new PointItem
{
Name = "Buhne 4",
Location = new Location(53.50468, 8.15343)
});
Points.Add(new PointItem
{
Name = "Buhne 4",
Location = new Location(53.50468, 8.15343)
});
Points.Add(new PointItem
{
Name = "Buhne 6",
Location = new Location(53.50092, 8.15267)
});
Points.Add(new PointItem
{
Name = "Buhne 6",
Location = new Location(53.50092, 8.15267)
});
Points.Add(new PointItem
{
Name = "Buhne 8",
Location = new Location(53.49871, 8.15321)
});
Points.Add(new PointItem
{
Name = "Buhne 8",
Location = new Location(53.49871, 8.15321)
});
Points.Add(new PointItem
{
Name = "Buhne 10",
Location = new Location(53.49348, 8.15694)
});
Points.Add(new PointItem
{
Name = "Buhne 10",
Location = new Location(53.49348, 8.15694)
});
Pushpins.Add(new PointItem
{
Name = "WHV - Eckwarderhörne",
Location = new Location(53.5495, 8.1877),
});
Pushpins.Add(new PointItem
{
Name = "WHV - Eckwarderhörne",
Location = new Location(53.5495, 8.1877),
});
Pushpins.Add(new PointItem
{
Name = "JadeWeserPort",
Location = new Location(53.5914, 8.14)
});
Pushpins.Add(new PointItem
{
Name = "JadeWeserPort",
Location = new Location(53.5914, 8.14)
});
Pushpins.Add(new PointItem
{
Name = "Kurhaus Dangast",
Location = new Location(53.447, 8.1114)
});
Pushpins.Add(new PointItem
{
Name = "Kurhaus Dangast",
Location = new Location(53.447, 8.1114)
});
Pushpins.Add(new PointItem
{
Name = "Eckwarderhörne",
Location = new Location(53.5207, 8.2323)
});
Pushpins.Add(new PointItem
{
Name = "Eckwarderhörne",
Location = new Location(53.5207, 8.2323)
});
SelectedPushpin = Pushpins[0];
SelectedPushpin = Pushpins[0];
Polylines.Add(new PolylineItem
{
Locations = LocationCollection.Parse("53.5140,8.1451 53.5123,8.1506 53.5156,8.1623 53.5276,8.1757 53.5491,8.1852 53.5495,8.1877 53.5426,8.1993 53.5184,8.2219 53.5182,8.2386 53.5195,8.2387")
});
Polylines.Add(new PolylineItem
{
Locations = LocationCollection.Parse("53.5140,8.1451 53.5123,8.1506 53.5156,8.1623 53.5276,8.1757 53.5491,8.1852 53.5495,8.1877 53.5426,8.1993 53.5184,8.2219 53.5182,8.2386 53.5195,8.2387")
});
Polylines.Add(new PolylineItem
{
Locations = LocationCollection.Parse("53.5978,8.1212 53.6018,8.1494 53.5859,8.1554 53.5852,8.1531 53.5841,8.1539 53.5802,8.1392 53.5826,8.1309 53.5867,8.1317 53.5978,8.1212")
});
}
Polylines.Add(new PolylineItem
{
Locations = LocationCollection.Parse("53.5978,8.1212 53.6018,8.1494 53.5859,8.1554 53.5852,8.1531 53.5841,8.1539 53.5802,8.1392 53.5826,8.1309 53.5867,8.1317 53.5978,8.1212")
});
}
}

View file

@ -2,33 +2,32 @@
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace SampleApplication
namespace SampleApplication;
public sealed partial class App : Application
{
public sealed partial class App : Application
public App()
{
public App()
InitializeComponent();
}
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
if (Window.Current.Content is not Frame rootFrame)
{
InitializeComponent();
rootFrame = new Frame();
Window.Current.Content = rootFrame;
}
protected override void OnLaunched(LaunchActivatedEventArgs e)
if (e.PrelaunchActivated == false)
{
if (Window.Current.Content is not Frame rootFrame)
if (rootFrame.Content == null)
{
rootFrame = new Frame();
Window.Current.Content = rootFrame;
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
if (e.PrelaunchActivated == false)
{
if (rootFrame.Content == null)
{
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
Window.Current.Activate();
}
Window.Current.Activate();
}
}
}

View file

@ -10,146 +10,145 @@ using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
namespace SampleApplication
namespace SampleApplication;
public sealed partial class MainPage : Page
{
public sealed partial class MainPage : Page
public MainPage()
{
public MainPage()
ImageLoader.HttpClient.DefaultRequestHeaders.Add("User-Agent", "XAML Map Control UWP Sample Application");
var loggerFactory = LoggerFactory.Create(builder => builder.AddDebug().SetMinimumLevel(LogLevel.Information));
ImageLoader.LoggerFactory = loggerFactory;
var tileCache = new MapControl.Caching.ImageFileCache(TileImageLoader.DefaultCacheFolder, loggerFactory);
TileImageLoader.Cache = tileCache;
Unloaded += (s, e) => tileCache.Dispose();
InitializeComponent();
AddTestLayers();
}
partial void AddTestLayers();
private void MapItemsControlSelectionChanged(object sender, SelectionChangedEventArgs e)
{
Debug.WriteLine("SelectedItems: " + string.Join(", ", ((MapItemsControl)sender).SelectedItems.OfType<PointItem>().Select(item => item.Name)));
}
private void MapDoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
if (e.OriginalSource is Map map)
{
ImageLoader.HttpClient.DefaultRequestHeaders.Add("User-Agent", "XAML Map Control UWP Sample Application");
var loggerFactory = LoggerFactory.Create(builder => builder.AddDebug().SetMinimumLevel(LogLevel.Information));
ImageLoader.LoggerFactory = loggerFactory;
var tileCache = new MapControl.Caching.ImageFileCache(TileImageLoader.DefaultCacheFolder, loggerFactory);
TileImageLoader.Cache = tileCache;
Unloaded += (s, e) => tileCache.Dispose();
InitializeComponent();
AddTestLayers();
map.TargetCenter = map.ViewToLocation(e.GetPosition(map));
}
}
partial void AddTestLayers();
private void ResetHeadingButtonClick(object sender, RoutedEventArgs e)
{
map.TargetHeading = 0d;
}
private void MapItemsControlSelectionChanged(object sender, SelectionChangedEventArgs e)
private async void MapPointerPressed(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
{
Debug.WriteLine("SelectedItems: " + string.Join(", ", ((MapItemsControl)sender).SelectedItems.OfType<PointItem>().Select(item => item.Name)));
}
var point = e.GetCurrentPoint(map);
private void MapDoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
if (e.OriginalSource is Map map)
if (point.Properties.IsRightButtonPressed && map.CapturePointer(e.Pointer))
{
map.TargetCenter = map.ViewToLocation(e.GetPosition(map));
}
}
var location = map.ViewToLocation(point.Position);
private void ResetHeadingButtonClick(object sender, RoutedEventArgs e)
{
map.TargetHeading = 0d;
}
private async void MapPointerPressed(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
{
var point = e.GetCurrentPoint(map);
if (point.Properties.IsRightButtonPressed && map.CapturePointer(e.Pointer))
if (location != null)
{
var location = map.ViewToLocation(point.Position);
if (location != null)
{
measurementLine.Visibility = Visibility.Visible;
measurementLine.Locations = new LocationCollection(location);
}
}
else if (e.KeyModifiers.HasFlag(VirtualKeyModifiers.Control) && map.MapLayer is WmsImageLayer wmsLayer)
{
Debug.WriteLine(await wmsLayer.GetFeatureInfoAsync(point.Position));
measurementLine.Visibility = Visibility.Visible;
measurementLine.Locations = new LocationCollection(location);
}
}
else if (e.KeyModifiers.HasFlag(VirtualKeyModifiers.Control) && map.MapLayer is WmsImageLayer wmsLayer)
{
Debug.WriteLine(await wmsLayer.GetFeatureInfoAsync(point.Position));
}
}
}
private void MapPointerReleased(object sender, PointerRoutedEventArgs e)
private void MapPointerReleased(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
{
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
{
map.ReleasePointerCapture(e.Pointer);
measurementLine.Visibility = Visibility.Collapsed;
measurementLine.Locations = null;
}
map.ReleasePointerCapture(e.Pointer);
measurementLine.Visibility = Visibility.Collapsed;
measurementLine.Locations = null;
}
}
private void MapPointerMoved(object sender, PointerRoutedEventArgs e)
private void MapPointerMoved(object sender, PointerRoutedEventArgs e)
{
var location = map.ViewToLocation(e.GetCurrentPoint(map).Position);
if (location != null)
{
var location = map.ViewToLocation(e.GetCurrentPoint(map).Position);
mouseLocation.Visibility = Visibility.Visible;
mouseLocation.Text = GetLatLonText(location);
if (location != null)
var start = measurementLine.Locations?.FirstOrDefault();
if (start != null)
{
mouseLocation.Visibility = Visibility.Visible;
mouseLocation.Text = GetLatLonText(location);
var start = measurementLine.Locations?.FirstOrDefault();
if (start != null)
{
measurementLine.Locations = LocationCollection.GeodesicLocations(start, location);
mouseLocation.Text += GetDistanceText(location.GetDistance(start));
}
}
else
{
mouseLocation.Visibility = Visibility.Collapsed;
mouseLocation.Text = "";
measurementLine.Locations = LocationCollection.GeodesicLocations(start, location);
mouseLocation.Text += GetDistanceText(location.GetDistance(start));
}
}
private void MapPointerExited(object sender, PointerRoutedEventArgs e)
else
{
mouseLocation.Visibility = Visibility.Collapsed;
mouseLocation.Text = "";
}
}
private static string GetLatLonText(Location location)
private void MapPointerExited(object sender, PointerRoutedEventArgs e)
{
mouseLocation.Visibility = Visibility.Collapsed;
mouseLocation.Text = "";
}
private static string GetLatLonText(Location location)
{
var latitude = (int)Math.Round(location.Latitude * 60000d);
var longitude = (int)Math.Round(Location.NormalizeLongitude(location.Longitude) * 60000d);
var latHemisphere = 'N';
var lonHemisphere = 'E';
if (latitude < 0)
{
var latitude = (int)Math.Round(location.Latitude * 60000d);
var longitude = (int)Math.Round(Location.NormalizeLongitude(location.Longitude) * 60000d);
var latHemisphere = 'N';
var lonHemisphere = 'E';
if (latitude < 0)
{
latitude = -latitude;
latHemisphere = 'S';
}
if (longitude < 0)
{
longitude = -longitude;
lonHemisphere = 'W';
}
return string.Format(CultureInfo.InvariantCulture,
"{0} {1:00} {2:00.000}\n{3} {4:000} {5:00.000}",
latHemisphere, latitude / 60000, (latitude % 60000) / 1000d,
lonHemisphere, longitude / 60000, (longitude % 60000) / 1000d);
latitude = -latitude;
latHemisphere = 'S';
}
private static string GetDistanceText(double distance)
if (longitude < 0)
{
var unit = "m";
if (distance >= 1000d)
{
distance /= 1000d;
unit = "km";
}
var distanceFormat = distance >= 100d ? "F0" : "F1";
return string.Format(CultureInfo.InvariantCulture, "\n {0:" + distanceFormat + "} {1}", distance, unit);
longitude = -longitude;
lonHemisphere = 'W';
}
return string.Format(CultureInfo.InvariantCulture,
"{0} {1:00} {2:00.000}\n{3} {4:000} {5:00.000}",
latHemisphere, latitude / 60000, (latitude % 60000) / 1000d,
lonHemisphere, longitude / 60000, (longitude % 60000) / 1000d);
}
private static string GetDistanceText(double distance)
{
var unit = "m";
if (distance >= 1000d)
{
distance /= 1000d;
unit = "km";
}
var distanceFormat = distance >= 100d ? "F0" : "F1";
return string.Format(CultureInfo.InvariantCulture, "\n {0:" + distanceFormat + "} {1}", distance, unit);
}
}

View file

@ -1,20 +1,19 @@
using Microsoft.UI.Xaml;
namespace SampleApplication
namespace SampleApplication;
public partial class App : Application
{
public partial class App : Application
private Window window;
public App()
{
private Window window;
InitializeComponent();
}
public App()
{
InitializeComponent();
}
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
window = new MainWindow();
window.Activate();
}
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
window = new MainWindow();
window.Activate();
}
}

View file

@ -10,135 +10,134 @@ using System.Globalization;
using System.Linq;
using Windows.System;
namespace SampleApplication
namespace SampleApplication;
public sealed partial class MainWindow : Window
{
public sealed partial class MainWindow : Window
public MainWindow()
{
public MainWindow()
ImageLoader.HttpClient.DefaultRequestHeaders.Add("User-Agent", "XAML Map Control WinUI Sample Application");
var loggerFactory = LoggerFactory.Create(builder => builder.AddDebug().SetMinimumLevel(LogLevel.Information));
ImageLoader.LoggerFactory = loggerFactory;
var tileCache = new MapControl.Caching.ImageFileCache(TileImageLoader.DefaultCacheFolder, loggerFactory);
TileImageLoader.Cache = tileCache;
Closed += (s, e) => tileCache.Dispose();
InitializeComponent();
AddTestLayers();
}
partial void AddTestLayers();
private void MapItemsControlSelectionChanged(object sender, SelectionChangedEventArgs e)
{
Debug.WriteLine("SelectedItems: " + string.Join(", ", ((MapItemsControl)sender).SelectedItems.OfType<PointItem>().Select(item => item.Name)));
}
private void MapDoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
if (e.OriginalSource is Map map)
{
ImageLoader.HttpClient.DefaultRequestHeaders.Add("User-Agent", "XAML Map Control WinUI Sample Application");
var loggerFactory = LoggerFactory.Create(builder => builder.AddDebug().SetMinimumLevel(LogLevel.Information));
ImageLoader.LoggerFactory = loggerFactory;
var tileCache = new MapControl.Caching.ImageFileCache(TileImageLoader.DefaultCacheFolder, loggerFactory);
TileImageLoader.Cache = tileCache;
Closed += (s, e) => tileCache.Dispose();
InitializeComponent();
AddTestLayers();
}
partial void AddTestLayers();
private void MapItemsControlSelectionChanged(object sender, SelectionChangedEventArgs e)
{
Debug.WriteLine("SelectedItems: " + string.Join(", ", ((MapItemsControl)sender).SelectedItems.OfType<PointItem>().Select(item => item.Name)));
}
private void MapDoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
if (e.OriginalSource is Map map)
{
map.TargetCenter = map.ViewToLocation(e.GetPosition(map));
}
}
private void ResetHeadingButtonClick(object sender, RoutedEventArgs e)
{
map.TargetHeading = 0d;
}
private async void MapPointerPressed(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
{
var point = e.GetCurrentPoint(map);
if (point.Properties.IsRightButtonPressed && map.CapturePointer(e.Pointer))
{
var location = map.ViewToLocation(point.Position);
measurementLine.Visibility = Visibility.Visible;
measurementLine.Locations = new LocationCollection(location);
}
else if (e.KeyModifiers.HasFlag(VirtualKeyModifiers.Control) && map.MapLayer is WmsImageLayer wmsLayer)
{
Debug.WriteLine(await wmsLayer.GetFeatureInfoAsync(point.Position));
}
}
}
private void MapPointerReleased(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
{
map.ReleasePointerCapture(e.Pointer);
measurementLine.Visibility = Visibility.Collapsed;
measurementLine.Locations = null;
}
}
private void MapPointerMoved(object sender, PointerRoutedEventArgs e)
{
var point = e.GetCurrentPoint(map);
var location = map.ViewToLocation(point.Position);
mouseLocation.Visibility = Visibility.Visible;
mouseLocation.Text = GetLatLonText(location);
if (measurementLine.Locations != null)
{
var start = measurementLine.Locations.First();
measurementLine.Locations = LocationCollection.GeodesicLocations(start, location);
mouseLocation.Text += GetDistanceText(location.GetDistance(start));
}
}
private void MapPointerExited(object sender, PointerRoutedEventArgs e)
{
mouseLocation.Visibility = Visibility.Collapsed;
mouseLocation.Text = "";
}
private static string GetLatLonText(Location location)
{
var latitude = (int)Math.Round(location.Latitude * 60000d);
var longitude = (int)Math.Round(Location.NormalizeLongitude(location.Longitude) * 60000d);
var latHemisphere = 'N';
var lonHemisphere = 'E';
if (latitude < 0)
{
latitude = -latitude;
latHemisphere = 'S';
}
if (longitude < 0)
{
longitude = -longitude;
lonHemisphere = 'W';
}
return string.Format(CultureInfo.InvariantCulture,
"{0} {1:00} {2:00.000}\n{3} {4:000} {5:00.000}",
latHemisphere, latitude / 60000, (latitude % 60000) / 1000d,
lonHemisphere, longitude / 60000, (longitude % 60000) / 1000d);
}
private static string GetDistanceText(double distance)
{
var unit = "m";
if (distance >= 1000d)
{
distance /= 1000d;
unit = "km";
}
var distanceFormat = distance >= 100d ? "F0" : "F1";
return string.Format(CultureInfo.InvariantCulture, "\n {0:" + distanceFormat + "} {1}", distance, unit);
map.TargetCenter = map.ViewToLocation(e.GetPosition(map));
}
}
private void ResetHeadingButtonClick(object sender, RoutedEventArgs e)
{
map.TargetHeading = 0d;
}
private async void MapPointerPressed(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
{
var point = e.GetCurrentPoint(map);
if (point.Properties.IsRightButtonPressed && map.CapturePointer(e.Pointer))
{
var location = map.ViewToLocation(point.Position);
measurementLine.Visibility = Visibility.Visible;
measurementLine.Locations = new LocationCollection(location);
}
else if (e.KeyModifiers.HasFlag(VirtualKeyModifiers.Control) && map.MapLayer is WmsImageLayer wmsLayer)
{
Debug.WriteLine(await wmsLayer.GetFeatureInfoAsync(point.Position));
}
}
}
private void MapPointerReleased(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
{
map.ReleasePointerCapture(e.Pointer);
measurementLine.Visibility = Visibility.Collapsed;
measurementLine.Locations = null;
}
}
private void MapPointerMoved(object sender, PointerRoutedEventArgs e)
{
var point = e.GetCurrentPoint(map);
var location = map.ViewToLocation(point.Position);
mouseLocation.Visibility = Visibility.Visible;
mouseLocation.Text = GetLatLonText(location);
if (measurementLine.Locations != null)
{
var start = measurementLine.Locations.First();
measurementLine.Locations = LocationCollection.GeodesicLocations(start, location);
mouseLocation.Text += GetDistanceText(location.GetDistance(start));
}
}
private void MapPointerExited(object sender, PointerRoutedEventArgs e)
{
mouseLocation.Visibility = Visibility.Collapsed;
mouseLocation.Text = "";
}
private static string GetLatLonText(Location location)
{
var latitude = (int)Math.Round(location.Latitude * 60000d);
var longitude = (int)Math.Round(Location.NormalizeLongitude(location.Longitude) * 60000d);
var latHemisphere = 'N';
var lonHemisphere = 'E';
if (latitude < 0)
{
latitude = -latitude;
latHemisphere = 'S';
}
if (longitude < 0)
{
longitude = -longitude;
lonHemisphere = 'W';
}
return string.Format(CultureInfo.InvariantCulture,
"{0} {1:00} {2:00.000}\n{3} {4:000} {5:00.000}",
latHemisphere, latitude / 60000, (latitude % 60000) / 1000d,
lonHemisphere, longitude / 60000, (longitude % 60000) / 1000d);
}
private static string GetDistanceText(double distance)
{
var unit = "m";
if (distance >= 1000d)
{
distance /= 1000d;
unit = "km";
}
var distanceFormat = distance >= 100d ? "F0" : "F1";
return string.Format(CultureInfo.InvariantCulture, "\n {0:" + distanceFormat + "} {1}", distance, unit);
}
}

View file

@ -1,8 +1,7 @@
using System.Windows;
namespace SampleApplication
namespace SampleApplication;
public partial class App : Application
{
public partial class App : Application
{
}
}

View file

@ -8,158 +8,157 @@ using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace SampleApplication
namespace SampleApplication;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
class HttpHandler : DelegatingHandler
{
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
class HttpHandler : DelegatingHandler
{
public HttpHandler()
public HttpHandler()
#if NET
: base(new SocketsHttpHandler())
: base(new SocketsHttpHandler())
#else
: base(new HttpClientHandler())
: base(new HttpClientHandler())
#endif
{
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
//Debug.WriteLine(request.RequestUri);
return base.SendAsync(request, cancellationToken);
}
{
}
public partial class MainWindow : Window
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
public MainWindow()
{
var httpClient = new HttpClient(new HttpHandler()) { Timeout = TimeSpan.FromSeconds(10) };
httpClient.DefaultRequestHeaders.Add("User-Agent", "XAML Map Control WPF Sample Application");
ImageLoader.HttpClient = httpClient;
var loggerFactory = LoggerFactory.Create(builder => builder.AddDebug().SetMinimumLevel(LogLevel.Information));
ImageLoader.LoggerFactory = loggerFactory;
var tileCache = new MapControl.Caching.ImageFileCache(TileImageLoader.DefaultCacheFolder, loggerFactory);
TileImageLoader.Cache = tileCache;
Closed += (s, e) => tileCache.Dispose();
InitializeComponent();
AddTestLayers();
}
partial void AddTestLayers();
private void MapItemsControlSelectionChanged(object sender, SelectionChangedEventArgs e)
{
Debug.WriteLine("SelectedItems: " + string.Join(", ", ((MapItemsControl)sender).SelectedItems.OfType<PointItem>().Select(item => item.Name)));
}
private void ResetHeadingButtonClick(object sender, RoutedEventArgs e)
{
map.TargetHeading = 0d;
}
private async void MapMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2 && e.Source == map)
{
map.TargetCenter = map.ViewToLocation(e.GetPosition(map));
}
else if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control) &&
map.MapLayer is WmsImageLayer wmsLayer)
{
Debug.WriteLine(await wmsLayer.GetFeatureInfoAsync(e.GetPosition(map)));
}
}
private void MapMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
var location = map.ViewToLocation(e.GetPosition(map));
if (map.CaptureMouse())
{
map.Cursor = Cursors.Cross;
measurementLine.Visibility = Visibility.Visible;
measurementLine.Locations = new LocationCollection(location);
}
}
private void MapMouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
map.ReleaseMouseCapture();
map.Cursor = null;
measurementLine.Visibility = Visibility.Collapsed;
measurementLine.Locations = null;
}
private void MapMouseMove(object sender, MouseEventArgs e)
{
var location = map.ViewToLocation(e.GetPosition(map));
mouseLocation.Visibility = Visibility.Visible;
mouseLocation.Text = GetLatLonText(location);
if (measurementLine.Locations != null)
{
var start = measurementLine.Locations.First();
measurementLine.Locations = LocationCollection.GeodesicLocations(start, location);
mouseLocation.Text += GetDistanceText(location.GetDistance(start));
}
}
private void MapMouseLeave(object sender, MouseEventArgs e)
{
mouseLocation.Visibility = Visibility.Collapsed;
mouseLocation.Text = "";
}
private void MapManipulationInertiaStarting(object sender, ManipulationInertiaStartingEventArgs e)
{
e.TranslationBehavior.DesiredDeceleration = 0.001;
}
private static string GetLatLonText(Location location)
{
var latitude = (int)Math.Round(location.Latitude * 60000d);
var longitude = (int)Math.Round(Location.NormalizeLongitude(location.Longitude) * 60000d);
var latHemisphere = 'N';
var lonHemisphere = 'E';
if (latitude < 0)
{
latitude = -latitude;
latHemisphere = 'S';
}
if (longitude < 0)
{
longitude = -longitude;
lonHemisphere = 'W';
}
return string.Format(CultureInfo.InvariantCulture,
"{0} {1:00} {2:00.000}\n{3} {4:000} {5:00.000}",
latHemisphere, latitude / 60000, (latitude % 60000) / 1000d,
lonHemisphere, longitude / 60000, (longitude % 60000) / 1000d);
}
private static string GetDistanceText(double distance)
{
var unit = "m";
if (distance >= 1000d)
{
distance /= 1000d;
unit = "km";
}
var distanceFormat = distance >= 100d ? "F0" : "F1";
return string.Format(CultureInfo.InvariantCulture, "\n {0:" + distanceFormat + "} {1}", distance, unit);
}
//Debug.WriteLine(request.RequestUri);
return base.SendAsync(request, cancellationToken);
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
var httpClient = new HttpClient(new HttpHandler()) { Timeout = TimeSpan.FromSeconds(10) };
httpClient.DefaultRequestHeaders.Add("User-Agent", "XAML Map Control WPF Sample Application");
ImageLoader.HttpClient = httpClient;
var loggerFactory = LoggerFactory.Create(builder => builder.AddDebug().SetMinimumLevel(LogLevel.Information));
ImageLoader.LoggerFactory = loggerFactory;
var tileCache = new MapControl.Caching.ImageFileCache(TileImageLoader.DefaultCacheFolder, loggerFactory);
TileImageLoader.Cache = tileCache;
Closed += (s, e) => tileCache.Dispose();
InitializeComponent();
AddTestLayers();
}
partial void AddTestLayers();
private void MapItemsControlSelectionChanged(object sender, SelectionChangedEventArgs e)
{
Debug.WriteLine("SelectedItems: " + string.Join(", ", ((MapItemsControl)sender).SelectedItems.OfType<PointItem>().Select(item => item.Name)));
}
private void ResetHeadingButtonClick(object sender, RoutedEventArgs e)
{
map.TargetHeading = 0d;
}
private async void MapMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2 && e.Source == map)
{
map.TargetCenter = map.ViewToLocation(e.GetPosition(map));
}
else if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control) &&
map.MapLayer is WmsImageLayer wmsLayer)
{
Debug.WriteLine(await wmsLayer.GetFeatureInfoAsync(e.GetPosition(map)));
}
}
private void MapMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
var location = map.ViewToLocation(e.GetPosition(map));
if (map.CaptureMouse())
{
map.Cursor = Cursors.Cross;
measurementLine.Visibility = Visibility.Visible;
measurementLine.Locations = new LocationCollection(location);
}
}
private void MapMouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
map.ReleaseMouseCapture();
map.Cursor = null;
measurementLine.Visibility = Visibility.Collapsed;
measurementLine.Locations = null;
}
private void MapMouseMove(object sender, MouseEventArgs e)
{
var location = map.ViewToLocation(e.GetPosition(map));
mouseLocation.Visibility = Visibility.Visible;
mouseLocation.Text = GetLatLonText(location);
if (measurementLine.Locations != null)
{
var start = measurementLine.Locations.First();
measurementLine.Locations = LocationCollection.GeodesicLocations(start, location);
mouseLocation.Text += GetDistanceText(location.GetDistance(start));
}
}
private void MapMouseLeave(object sender, MouseEventArgs e)
{
mouseLocation.Visibility = Visibility.Collapsed;
mouseLocation.Text = "";
}
private void MapManipulationInertiaStarting(object sender, ManipulationInertiaStartingEventArgs e)
{
e.TranslationBehavior.DesiredDeceleration = 0.001;
}
private static string GetLatLonText(Location location)
{
var latitude = (int)Math.Round(location.Latitude * 60000d);
var longitude = (int)Math.Round(Location.NormalizeLongitude(location.Longitude) * 60000d);
var latHemisphere = 'N';
var lonHemisphere = 'E';
if (latitude < 0)
{
latitude = -latitude;
latHemisphere = 'S';
}
if (longitude < 0)
{
longitude = -longitude;
lonHemisphere = 'W';
}
return string.Format(CultureInfo.InvariantCulture,
"{0} {1:00} {2:00.000}\n{3} {4:000} {5:00.000}",
latHemisphere, latitude / 60000, (latitude % 60000) / 1000d,
lonHemisphere, longitude / 60000, (longitude % 60000) / 1000d);
}
private static string GetDistanceText(double distance)
{
var unit = "m";
if (distance >= 1000d)
{
distance /= 1000d;
unit = "km";
}
var distanceFormat = distance >= 100d ? "F0" : "F1";
return string.Format(CultureInfo.InvariantCulture, "\n {0:" + distanceFormat + "} {1}", distance, unit);
}
}