XAML-Map-Control/SampleApps/AvaloniaApp/MainWindow.axaml.cs

153 lines
4.9 KiB
C#
Raw Normal View History

2024-05-24 15:28:28 +02:00
using Avalonia.Controls;
using Avalonia.Input;
using MapControl;
2025-03-30 17:16:49 +02:00
using Microsoft.Extensions.Logging;
2024-07-15 00:02:30 +02:00
using System;
using System.Diagnostics;
2024-07-15 00:02:30 +02:00
using System.Globalization;
using System.Linq;
2024-05-24 15:28:28 +02:00
2024-05-27 11:10:10 +02:00
namespace SampleApplication
2024-05-24 15:28:28 +02:00
{
public partial class MainWindow : Window
{
2025-02-22 16:07:21 +01:00
public MainWindow()
2024-05-24 15:28:28 +02:00
{
2025-03-31 21:42:32 +02:00
var loggerFactory = LoggerFactory.Create(builder => builder.AddDebug());
ImageLoader.LoggerFactory = loggerFactory;
//var tileCache = new MapControl.Caching.ImageFileCache(TileImageLoader.DefaultCacheFolder, loggerFactory);
2025-03-31 18:28:09 +02:00
//TileImageLoader.Cache = tileCache;
2025-03-31 21:42:32 +02:00
//Closed += (s, e) => tileCache.Dispose();
2024-05-24 15:28:28 +02:00
InitializeComponent();
2025-03-24 19:06:15 +01:00
sampleOverlayMenuItem.MapLayerFactory = async () => await GroundOverlay.CreateAsync("etna.kml");
2024-05-29 17:19:03 +02:00
AddTestLayers();
2024-05-28 18:56:28 +02:00
}
2024-05-29 17:19:03 +02:00
partial void AddTestLayers();
2024-05-28 18:56:28 +02:00
2024-05-31 22:45:50 +02:00
private void MapItemsControlSelectionChanged(object sender, SelectionChangedEventArgs e)
{
Debug.WriteLine("SelectedItems: " + string.Join(", ", ((MapItemsControl)sender).SelectedItems.OfType<PointItem>().Select(item => item.Name)));
}
2024-07-15 00:02:30 +02:00
private void ResetHeadingButtonClick(object sender, Avalonia.Interactivity.RoutedEventArgs e)
{
map.TargetHeading = 0d;
}
private void MapDoubleTapped(object sender, TappedEventArgs e)
2024-05-28 18:56:28 +02:00
{
2024-08-25 18:02:23 +02:00
if (e.Source == map)
{
map.TargetCenter = map.ViewToLocation(e.GetPosition(map));
}
2024-05-28 18:56:28 +02:00
}
2024-07-15 00:02:30 +02:00
private void MapPointerPressed(object sender, PointerPressedEventArgs e)
2024-05-28 18:56:28 +02:00
{
2024-07-15 00:02:30 +02:00
if (e.Pointer.Type == PointerType.Mouse)
{
var point = e.GetCurrentPoint(map);
2024-08-25 18:02:23 +02:00
if (point.Properties.IsRightButtonPressed)
2024-07-15 00:02:30 +02:00
{
e.Pointer.Capture(map);
var location = map.ViewToLocation(point.Position);
if (location != null)
{
2024-08-24 23:12:35 +02:00
map.Cursor = new Cursor(StandardCursorType.Cross);
2024-07-15 00:02:30 +02:00
measurementLine.IsVisible = true;
measurementLine.Locations = new LocationCollection(location);
}
}
}
}
private void MapPointerReleased(object sender, PointerReleasedEventArgs e)
{
if (e.Pointer.Captured == map)
{
e.Pointer.Capture(null);
2024-08-25 18:02:23 +02:00
map.Cursor = null;
2024-07-15 00:02:30 +02:00
measurementLine.IsVisible = false;
measurementLine.Locations = null;
}
}
private void MapPointerMoved(object sender, PointerEventArgs e)
{
var location = map.ViewToLocation(e.GetPosition(map));
if (location != null)
{
mouseLocation.IsVisible = true;
mouseLocation.Text = GetLatLonText(location);
var start = measurementLine.Locations?.FirstOrDefault();
if (start != null)
{
measurementLine.Locations = LocationCollection.OrthodromeLocations(start, location);
mouseLocation.Text += GetDistanceText(location.GetDistance(start));
}
}
else
{
mouseLocation.IsVisible = false;
mouseLocation.Text = "";
}
}
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);
}
2024-08-24 23:12:35 +02:00
private static string GetDistanceText(double distance)
2024-07-15 00:02:30 +02: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);
2024-05-24 15:28:28 +02:00
}
}
}