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

208 lines
7.3 KiB
C#
Raw Normal View History

2021-07-01 22:07:46 +02:00
using MapControl;
2022-01-13 20:12:49 +01:00
using MapControl.UiTools;
using Microsoft.UI;
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;
2022-01-13 20:12:49 +01:00
using Microsoft.UI.Xaml.Media;
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;
2021-07-01 22:07:46 +02:00
using System.IO;
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
{
static MainWindow()
{
2024-02-10 20:50:09 +01:00
//TileImageLoader.Cache = new MapControl.Caching.ImageFileCache(TileImageLoader.DefaultCacheFolder);
//TileImageLoader.Cache = new MapControl.Caching.FileDbCache(TileImageLoader.DefaultCacheFolder);
//TileImageLoader.Cache = new MapControl.Caching.SQLiteCache(TileImageLoader.DefaultCacheFolder);
2021-11-17 21:35:58 +01:00
2022-01-13 20:12:49 +01:00
var bingMapsApiKeyPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl", "BingMapsApiKey.txt");
2021-11-17 21:35:58 +01:00
2022-01-13 20:12:49 +01:00
if (File.Exists(bingMapsApiKeyPath))
2021-07-01 22:07:46 +02:00
{
2022-01-13 20:12:49 +01:00
BingMapsTileLayer.ApiKey = File.ReadAllText(bingMapsApiKeyPath)?.Trim();
2021-07-01 22:07:46 +02:00
}
}
public MainWindow()
{
InitializeComponent();
Title = "XAML Map Control - WinUI Sample Application";
2022-01-13 20:12:49 +01:00
if (!string.IsNullOrEmpty(BingMapsTileLayer.ApiKey))
{
mapLayersMenuButton.MapLayers.Add(new MapLayerItem
{
Text = "Bing Maps Road",
Layer = new BingMapsTileLayer
{
Mode = BingMapsTileLayer.MapMode.Road,
SourceName = "Bing Maps Road",
Description = "© [Microsoft](http://www.bing.com/maps/)"
}
});
mapLayersMenuButton.MapLayers.Add(new MapLayerItem
{
Text = "Bing Maps Aerial",
Layer = new BingMapsTileLayer
{
Mode = BingMapsTileLayer.MapMode.Aerial,
SourceName = "Bing Maps Aerial",
Description = "© [Microsoft](http://www.bing.com/maps/)",
MapForeground = new SolidColorBrush(Colors.White),
MapBackground = new SolidColorBrush(Colors.Black)
}
});
mapLayersMenuButton.MapLayers.Add(new MapLayerItem
{
Text = "Bing Maps Aerial with Labels",
Layer = new BingMapsTileLayer
{
Mode = BingMapsTileLayer.MapMode.AerialWithLabels,
SourceName = "Bing Maps Hybrid",
Description = "© [Microsoft](http://www.bing.com/maps/)",
MapForeground = new SolidColorBrush(Colors.White),
MapBackground = new SolidColorBrush(Colors.Black)
}
});
}
2022-11-28 17:34:06 +01:00
AddTestLayers();
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)));
}
private void MapItemsControlDoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
e.Handled = true; // prevent MapDoubleTapped
}
2024-05-24 15:28:28 +02:00
private void MapDoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
map.TargetCenter = map.ViewToLocation(e.GetPosition(map));
}
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
if (point.Properties.IsRightButtonPressed)
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);
}
}
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)
{
measurementLine.Visibility = Visibility.Collapsed;
measurementLine.Locations = null;
}
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;
mouseLocation.Text = string.Empty;
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;
mouseLocation.Text = string.Empty;
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);
}
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);
}
2021-07-01 22:07:46 +02:00
}
}