XAML-Map-Control/SampleApps/WinUiApp/MainWindow.xaml.cs

153 lines
5.1 KiB
C#
Raw Normal View History

2021-07-01 22:07:46 +02:00
using MapControl;
2022-12-03 20:57:34 +01:00
using Microsoft.UI.Input;
2021-07-01 22:07:46 +02:00
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
2021-12-05 17:16:14 +01:00
using Microsoft.UI.Xaml.Input;
2021-07-01 22:07:46 +02:00
using System;
2022-12-03 20:57:34 +01:00
using System.Diagnostics;
2021-12-05 17:16:14 +01:00
using System.Globalization;
2023-01-06 16:01:03 +01:00
using System.Linq;
using Windows.System;
2021-07-01 22:07:46 +02:00
2021-10-28 20:26:51 +02:00
namespace SampleApplication
2021-07-01 22:07:46 +02:00
{
public sealed partial class MainWindow : Window
{
2025-02-22 16:06:45 +01:00
public MainWindow()
2021-07-01 22:07:46 +02:00
{
2025-03-22 17:11:58 +01:00
//TileImageLoader.Cache = new MapControl.Caching.ImageFileCache(TileImageLoader.DefaultCacheFolder);
2025-03-22 17:08:43 +01:00
//TileImageLoader.Cache = new MapControl.Caching.SQLiteCache(TileImageLoader.DefaultCacheFolder);
Closed += (s, e) => (TileImageLoader.Cache as IDisposable)?.Dispose();
2021-07-01 22:07:46 +02:00
InitializeComponent();
2022-11-28 17:34:06 +01:00
AddTestLayers();
2025-02-22 16:06:45 +01:00
2025-02-11 13:11:05 +01:00
Title = "XAML Map Control - WinUI Sample Application";
2021-07-01 22:07:46 +02:00
}
2022-11-28 17:34:06 +01:00
partial void AddTestLayers();
2022-01-13 20:12:49 +01:00
private void MapItemsControlSelectionChanged(object sender, SelectionChangedEventArgs e)
{
Debug.WriteLine("SelectedItems: " + string.Join(", ", ((MapItemsControl)sender).SelectedItems.OfType<PointItem>().Select(item => item.Name)));
}
2024-05-24 15:28:28 +02:00
private void MapDoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
2024-08-25 18:02:23 +02:00
if (e.OriginalSource is Map map)
{
map.TargetCenter = map.ViewToLocation(e.GetPosition(map));
}
2024-05-24 15:28:28 +02:00
}
private void ResetHeadingButtonClick(object sender, RoutedEventArgs e)
{
map.TargetHeading = 0d;
}
2022-12-03 20:57:34 +01:00
private async void MapPointerPressed(object sender, PointerRoutedEventArgs e)
{
2023-01-06 16:01:03 +01:00
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
2022-12-03 20:57:34 +01:00
{
2023-01-06 16:01:03 +01:00
var point = e.GetCurrentPoint(map);
2022-12-03 20:57:34 +01:00
2024-07-15 00:02:30 +02:00
if (point.Properties.IsRightButtonPressed && map.CapturePointer(e.Pointer))
2023-01-06 16:01:03 +01:00
{
var location = map.ViewToLocation(point.Position);
if (location != null)
{
measurementLine.Visibility = Visibility.Visible;
measurementLine.Locations = new LocationCollection(location);
}
}
2024-07-15 00:02:30 +02:00
else if (e.KeyModifiers.HasFlag(VirtualKeyModifiers.Control) && map.MapLayer is WmsImageLayer wmsLayer)
2022-12-03 20:57:34 +01:00
{
Debug.WriteLine(await wmsLayer.GetFeatureInfoAsync(point.Position));
}
}
}
2023-01-06 16:01:03 +01:00
private void MapPointerReleased(object sender, PointerRoutedEventArgs e)
{
2024-07-15 00:02:30 +02:00
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
{
map.ReleasePointerCapture(e.Pointer);
measurementLine.Visibility = Visibility.Collapsed;
measurementLine.Locations = null;
}
2023-01-06 16:01:03 +01:00
}
2021-12-05 17:16:14 +01:00
private void MapPointerMoved(object sender, PointerRoutedEventArgs e)
2021-07-01 22:07:46 +02:00
{
2023-01-06 16:01:03 +01:00
var point = e.GetCurrentPoint(map);
var location = map.ViewToLocation(point.Position);
2021-12-05 17:16:14 +01:00
2022-03-04 22:28:18 +01:00
if (location != null)
2021-12-05 17:16:14 +01:00
{
2023-01-06 16:01:03 +01:00
mouseLocation.Visibility = Visibility.Visible;
mouseLocation.Text = GetLatLonText(location);
2022-03-04 22:28:18 +01:00
2023-01-06 16:01:03 +01:00
var start = measurementLine.Locations?.FirstOrDefault();
2021-12-05 17:16:14 +01:00
2023-01-06 16:01:03 +01:00
if (start != null)
2022-03-04 22:28:18 +01:00
{
2023-01-06 16:01:03 +01:00
measurementLine.Locations = LocationCollection.OrthodromeLocations(start, location);
mouseLocation.Text += GetDistanceText(location.GetDistance(start));
2022-03-04 22:28:18 +01:00
}
}
else
2021-12-05 17:16:14 +01:00
{
2022-03-04 22:28:18 +01:00
mouseLocation.Visibility = Visibility.Collapsed;
2024-07-12 18:42:18 +02:00
mouseLocation.Text = "";
2021-12-05 17:16:14 +01:00
}
2021-07-01 22:07:46 +02:00
}
2021-07-07 19:30:05 +02:00
2021-12-05 17:16:14 +01:00
private void MapPointerExited(object sender, PointerRoutedEventArgs e)
2021-07-07 19:30:05 +02:00
{
2021-12-05 17:16:14 +01:00
mouseLocation.Visibility = Visibility.Collapsed;
2024-07-12 18:42:18 +02:00
mouseLocation.Text = "";
2021-07-07 19:30:05 +02:00
}
2023-01-06 16:01:03 +01:00
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);
}
2024-08-24 23:17:47 +02:00
private static string GetDistanceText(double distance)
2023-01-06 16:01:03 +01:00
{
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);
}
2021-07-01 22:07:46 +02:00
}
}