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

146 lines
5.1 KiB
C#
Raw Normal View History

2021-07-01 22:07:46 +02:00
using MapControl;
using MapControl.Caching;
2022-01-13 20:12:49 +01:00
using MapControl.UiTools;
using Microsoft.UI;
2021-07-01 22:07:46 +02:00
using Microsoft.UI.Xaml;
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;
2021-12-05 17:16:14 +01:00
using System.Globalization;
2021-07-01 22:07:46 +02:00
using System.IO;
using System.Threading.Tasks;
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()
{
2022-01-13 20:12:49 +01:00
ImageLoader.HttpClient.DefaultRequestHeaders.Add("User-Agent", "XAML Map Control Test Application");
2021-07-01 22:07:46 +02:00
2022-01-13 20:12:49 +01:00
TileImageLoader.Cache = new ImageFileCache(TileImageLoader.DefaultCacheFolder);
//TileImageLoader.Cache = new FileDbCache(TileImageLoader.DefaultCacheFolder);
//TileImageLoader.Cache = new 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();
2022-01-13 20:12:49 +01:00
2021-07-07 19:30:05 +02:00
if (TileImageLoader.Cache is ImageFileCache)
2021-07-01 22:07:46 +02:00
{
2021-07-07 19:30:05 +02:00
Activated += WindowActivated;
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
2021-07-07 19:30:05 +02:00
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;
}
2021-12-05 17:16:14 +01:00
private void MapPointerMoved(object sender, PointerRoutedEventArgs e)
2021-07-01 22:07:46 +02:00
{
2021-12-05 17:16:14 +01:00
var location = map.ViewToLocation(e.GetCurrentPoint(map).Position);
2022-03-04 22:28:18 +01:00
if (location != null)
2021-12-05 17:16:14 +01:00
{
2022-03-04 22:28:18 +01:00
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';
}
2021-12-05 17:16:14 +01:00
2022-03-04 22:28:18 +01:00
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
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
}
2021-07-01 22:07:46 +02:00
}
}