Avalonia sample application

This commit is contained in:
ClemensFischer 2024-07-15 00:02:30 +02:00
parent c2a5394d8e
commit d2c3382f46
5 changed files with 141 additions and 14 deletions

View file

@ -14,7 +14,15 @@
ZoomLevel="11" MinZoomLevel="3"
Center="53.5,8.2"
ManipulationModes="All"
DoubleTapped="MapDoubleTapped">
DoubleTapped="MapDoubleTapped"
PointerPressed="MapPointerPressed"
PointerReleased="MapPointerReleased"
PointerMoved="MapPointerMoved"
PointerExited="MapPointerExited">
<map:MapPolyline x:Name="measurementLine" IsVisible="False"
Stroke="{Binding Foreground, ElementName=map}"
StrokeThickness="2" StrokeDashArray="1,1"/>
<map:MapItemsControl ItemsSource="{Binding Polylines}">
<map:MapItemsControl.ItemTemplate>
@ -92,6 +100,15 @@
<map:Pushpin AutoCollapse="True" Location="53.5,8.2" Content="N 53°30' E 8°12'"/>
</map:Map>
<TextBlock x:Name="mouseLocation"
HorizontalAlignment="Center" VerticalAlignment="Top" Margin="4" Padding="4,2"
FontFamily="Consolas" IsHitTestVisible="False" IsVisible="False"
Foreground="{Binding Foreground, ElementName=map}">
<TextBlock.Background>
<SolidColorBrush Color="{Binding Background.Color, ElementName=map}" Opacity="0.5"/>
</TextBlock.Background>
</TextBlock>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom" Background="#7FFFFFFF"
DataContext="{Binding MapLayer, ElementName=map}">
<ProgressBar Width="100" Height="8" Margin="4,2" VerticalAlignment="Center"

View file

@ -1,7 +1,9 @@
using Avalonia.Controls;
using Avalonia.Input;
using MapControl;
using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
namespace SampleApplication
@ -29,6 +31,11 @@ namespace SampleApplication
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 MapItemsControlDoubleTapped(object sender, TappedEventArgs e)
{
e.Handled = true; // prevent MapDoubleTapped
@ -39,9 +46,104 @@ namespace SampleApplication
map.TargetCenter = map.ViewToLocation(e.GetPosition(map));
}
private void ResetHeadingButtonClick(object sender, Avalonia.Interactivity.RoutedEventArgs e)
private void MapPointerPressed(object sender, PointerPressedEventArgs e)
{
map.TargetHeading = 0d;
if (e.Pointer.Type == PointerType.Mouse)
{
var point = e.GetCurrentPoint(map);
if (point.Properties.IsRightButtonPressed)
{
e.Pointer.Capture(map);
var location = map.ViewToLocation(point.Position);
if (location != null)
{
measurementLine.IsVisible = true;
measurementLine.Locations = new LocationCollection(location);
}
}
}
}
private void MapPointerReleased(object sender, PointerReleasedEventArgs e)
{
if (e.Pointer.Captured == map)
{
e.Pointer.Capture(null);
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);
}
private 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

@ -45,7 +45,7 @@ namespace SampleApplication
{
var point = e.GetCurrentPoint(map);
if (point.Properties.IsRightButtonPressed)
if (point.Properties.IsRightButtonPressed && map.CapturePointer(e.Pointer))
{
var location = map.ViewToLocation(point.Position);
@ -55,8 +55,7 @@ namespace SampleApplication
measurementLine.Locations = new LocationCollection(location);
}
}
else if (e.KeyModifiers.HasFlag(VirtualKeyModifiers.Control) &&
map.MapLayer is WmsImageLayer wmsLayer)
else if (e.KeyModifiers.HasFlag(VirtualKeyModifiers.Control) && map.MapLayer is WmsImageLayer wmsLayer)
{
Debug.WriteLine(await wmsLayer.GetFeatureInfoAsync(point.Position));
}
@ -65,8 +64,12 @@ namespace SampleApplication
private void MapPointerReleased(object sender, PointerRoutedEventArgs e)
{
measurementLine.Visibility = Visibility.Collapsed;
measurementLine.Locations = null;
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
{
map.ReleasePointerCapture(e.Pointer);
measurementLine.Visibility = Visibility.Collapsed;
measurementLine.Locations = null;
}
}
private void MapPointerMoved(object sender, PointerRoutedEventArgs e)

View file

@ -57,7 +57,7 @@ namespace SampleApplication
{
var point = e.GetCurrentPoint(map);
if (point.Properties.IsRightButtonPressed)
if (point.Properties.IsRightButtonPressed && map.CapturePointer(e.Pointer))
{
var location = map.ViewToLocation(point.Position);
@ -67,8 +67,7 @@ namespace SampleApplication
measurementLine.Locations = new LocationCollection(location);
}
}
else if (e.KeyModifiers.HasFlag(VirtualKeyModifiers.Control) &&
map.MapLayer is WmsImageLayer wmsLayer)
else if (e.KeyModifiers.HasFlag(VirtualKeyModifiers.Control) && map.MapLayer is WmsImageLayer wmsLayer)
{
Debug.WriteLine(await wmsLayer.GetFeatureInfoAsync(point.Position));
}
@ -77,8 +76,12 @@ namespace SampleApplication
private void MapPointerReleased(object sender, PointerRoutedEventArgs e)
{
measurementLine.Visibility = Visibility.Collapsed;
measurementLine.Locations = null;
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
{
map.ReleasePointerCapture(e.Pointer);
measurementLine.Visibility = Visibility.Collapsed;
measurementLine.Locations = null;
}
}
private void MapPointerMoved(object sender, PointerRoutedEventArgs e)

View file

@ -5,6 +5,7 @@ using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
namespace SampleApplication
@ -61,7 +62,7 @@ namespace SampleApplication
{
var location = map.ViewToLocation(e.GetPosition(map));
if (location != null)
if (location != null && map.CaptureMouse())
{
measurementLine.Visibility = Visibility.Visible;
measurementLine.Locations = new LocationCollection(location);
@ -70,6 +71,7 @@ namespace SampleApplication
private void MapMouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
map.ReleaseMouseCapture();
measurementLine.Visibility = Visibility.Collapsed;
measurementLine.Locations = null;
}