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

148 lines
4.8 KiB
C#
Raw Normal View History

2021-12-05 17:16:14 +01:00
using MapControl;
using MapControl.Caching;
2022-01-13 20:12:49 +01:00
using MapControl.UiTools;
2021-12-05 17:16:14 +01:00
using System;
using System.Globalization;
2020-10-24 11:12:47 +02:00
using System.IO;
2020-06-24 21:05:07 +02:00
using System.Threading.Tasks;
2012-05-04 12:52:20 +02:00
using System.Windows;
using System.Windows.Input;
2021-10-28 20:26:51 +02:00
namespace SampleApplication
2012-05-04 12:52:20 +02:00
{
public partial class MainWindow : Window
{
2020-10-24 11:12:47 +02:00
static MainWindow()
2012-05-04 12:52:20 +02:00
{
2022-01-13 20:12:49 +01:00
ImageLoader.HttpClient.DefaultRequestHeaders.Add("User-Agent", "XAML Map Control Test Application");
2020-10-24 11:12:47 +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);
//TileImageLoader.Cache = null;
2022-01-13 20:12:49 +01:00
var bingMapsApiKeyPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MapControl", "BingMapsApiKey.txt");
2021-07-01 15:43:52 +02:00
2022-01-13 20:12:49 +01:00
if (File.Exists(bingMapsApiKeyPath))
2020-10-24 11:12:47 +02:00
{
2022-01-13 20:12:49 +01:00
BingMapsTileLayer.ApiKey = File.ReadAllText(bingMapsApiKeyPath)?.Trim();
2020-10-24 11:12:47 +02:00
}
}
public MainWindow()
{
2012-05-04 12:52:20 +02:00
InitializeComponent();
2020-06-24 21:05:07 +02:00
2022-01-13 20:12:49 +01:00
if (!string.IsNullOrEmpty(BingMapsTileLayer.ApiKey))
{
mapLayersMenuButton.MapLayers.Add(new MapLayerItem
{
Text = "Bing Maps Road",
2022-01-14 00:22:41 +01:00
Layer = (UIElement)Resources["BingMapsRoad"]
2022-01-13 20:12:49 +01:00
});
mapLayersMenuButton.MapLayers.Add(new MapLayerItem
{
Text = "Bing Maps Aerial",
2022-01-14 00:22:41 +01:00
Layer = (UIElement)Resources["BingMapsAerial"]
2022-01-13 20:12:49 +01:00
});
mapLayersMenuButton.MapLayers.Add(new MapLayerItem
{
Text = "Bing Maps Aerial with Labels",
2022-01-14 00:22:41 +01:00
Layer = (UIElement)Resources["BingMapsHybrid"]
2022-01-13 20:12:49 +01:00
});
}
2022-11-28 17:34:06 +01:00
AddTestLayers();
2022-01-13 20:12:49 +01:00
2020-10-24 11:12:47 +02:00
if (TileImageLoader.Cache is ImageFileCache cache)
2020-06-24 21:05:07 +02:00
{
2020-10-24 11:12:47 +02:00
Loaded += async (s, e) =>
{
await Task.Delay(2000);
await cache.Clean();
};
}
2012-05-04 12:52:20 +02:00
}
2022-11-28 17:34:06 +01:00
partial void AddTestLayers();
2022-01-13 20:12:49 +01:00
private void ResetHeadingButtonClick(object sender, RoutedEventArgs e)
{
map.TargetHeading = 0d;
}
2022-12-03 20:57:34 +01:00
private void MapMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
2022-12-02 23:15:09 +01:00
{
2022-12-03 20:57:34 +01:00
if (e.ClickCount == 2)
2022-12-02 23:15:09 +01:00
{
2022-12-03 20:57:34 +01:00
map.TargetCenter = map.ViewToLocation(e.GetPosition(map));
2022-12-02 23:15:09 +01:00
}
}
2022-12-03 20:57:34 +01:00
private async void MapMouseRightButtonDown(object sender, MouseButtonEventArgs e)
2012-05-04 12:52:20 +02:00
{
2022-12-03 20:57:34 +01:00
if (map.MapLayer is WmsImageLayer wmsLayer)
{
System.Diagnostics.Debug.WriteLine(await wmsLayer.GetFeatureInfoAsync(e.GetPosition(map)));
}
}
2012-05-04 12:52:20 +02:00
private void MapMouseMove(object sender, MouseEventArgs e)
{
var location = map.ViewToLocation(e.GetPosition(map));
2022-03-04 22:28:18 +01:00
if (location != null)
{
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';
}
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;
2022-03-04 22:28:18 +01:00
}
else
{
mouseLocation.Visibility = Visibility.Collapsed;
2022-03-04 22:28:18 +01:00
mouseLocation.Text = string.Empty;
}
2012-05-04 12:52:20 +02:00
}
2014-10-29 18:22:50 +01:00
private void MapMouseLeave(object sender, MouseEventArgs e)
{
mouseLocation.Visibility = Visibility.Collapsed;
2014-10-29 18:22:50 +01:00
mouseLocation.Text = string.Empty;
}
private void MapManipulationInertiaStarting(object sender, ManipulationInertiaStartingEventArgs e)
{
e.TranslationBehavior.DesiredDeceleration = 0.001;
}
private void MapItemTouchDown(object sender, TouchEventArgs e)
{
var mapItem = (MapItem)sender;
mapItem.IsSelected = !mapItem.IsSelected;
e.Handled = true;
}
2012-05-04 12:52:20 +02:00
}
}