XAML-Map-Control/SampleApps/WinUiApp/MainWindow.xaml.cs
2022-12-03 20:57:34 +01:00

162 lines
5.6 KiB
C#

using MapControl;
using MapControl.Caching;
using MapControl.UiTools;
using Microsoft.UI;
using Microsoft.UI.Input;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
namespace SampleApplication
{
public sealed partial class MainWindow : Window
{
static MainWindow()
{
ImageLoader.HttpClient.DefaultRequestHeaders.Add("User-Agent", "XAML Map Control Test Application");
TileImageLoader.Cache = new ImageFileCache(TileImageLoader.DefaultCacheFolder);
//TileImageLoader.Cache = new FileDbCache(TileImageLoader.DefaultCacheFolder);
//TileImageLoader.Cache = new SQLiteCache(TileImageLoader.DefaultCacheFolder);
var bingMapsApiKeyPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl", "BingMapsApiKey.txt");
if (File.Exists(bingMapsApiKeyPath))
{
BingMapsTileLayer.ApiKey = File.ReadAllText(bingMapsApiKeyPath)?.Trim();
}
}
public MainWindow()
{
InitializeComponent();
Title = "XAML Map Control - WinUI Sample Application";
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)
}
});
}
AddTestLayers();
if (TileImageLoader.Cache is ImageFileCache)
{
Activated += WindowActivated;
}
}
partial void AddTestLayers();
private async void WindowActivated(object sender, WindowActivatedEventArgs e)
{
Activated -= WindowActivated;
await Task.Delay(2000);
await ((ImageFileCache)TileImageLoader.Cache).Clean();
}
private void ResetHeadingButtonClick(object sender, RoutedEventArgs e)
{
map.TargetHeading = 0d;
}
private async void MapPointerPressed(object sender, PointerRoutedEventArgs e)
{
if (map.MapLayer is WmsImageLayer wmsLayer &&
e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
{
PointerPoint point = e.GetCurrentPoint(map);
if (point.Properties.IsRightButtonPressed)
{
Debug.WriteLine(await wmsLayer.GetFeatureInfoAsync(point.Position));
}
}
}
private void MapPointerMoved(object sender, PointerRoutedEventArgs e)
{
var location = map.ViewToLocation(e.GetCurrentPoint(map).Position);
if (location != null)
{
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';
}
mouseLocation.Text = 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);
mouseLocation.Visibility = Visibility.Visible;
}
else
{
mouseLocation.Visibility = Visibility.Collapsed;
mouseLocation.Text = string.Empty;
}
}
private void MapPointerExited(object sender, PointerRoutedEventArgs e)
{
mouseLocation.Visibility = Visibility.Collapsed;
mouseLocation.Text = string.Empty;
}
}
}