XAML-Map-Control/SampleApps/UniversalApp/MainPage.xaml.cs

129 lines
4.6 KiB
C#
Raw Normal View History

2021-12-05 17:16:14 +01:00
using MapControl;
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 19:00:56 +02:00
using System.IO;
2022-01-13 20:12:49 +01:00
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
2021-12-05 17:16:14 +01:00
using Windows.UI.Xaml.Input;
2022-01-13 20:12:49 +01:00
using Windows.UI.Xaml.Media;
2021-10-28 20:26:51 +02:00
namespace SampleApplication
{
public sealed partial class MainPage : Page
{
2020-10-24 19:00:56 +02:00
static MainPage()
{
2022-01-13 20:12:49 +01:00
ImageLoader.HttpClient.DefaultRequestHeaders.Add("User-Agent", "XAML Map Control Test Application");
2020-10-24 19:00:56 +02:00
2022-01-13 20:12:49 +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);
2020-10-24 19:00:56 +02:00
2022-01-13 20:12:49 +01:00
var bingMapsApiKeyPath = "BingMapsApiKey.txt";
if (File.Exists(bingMapsApiKeyPath))
2020-10-24 19:00:56 +02:00
{
2022-01-13 20:12:49 +01:00
BingMapsTileLayer.ApiKey = File.ReadAllText(bingMapsApiKeyPath)?.Trim();
2020-10-24 19:00:56 +02:00
}
}
public MainPage()
{
InitializeComponent();
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-02-22 22:12:15 +01:00
AddChartServerLayer();
}
2022-02-22 22:12:15 +01:00
partial void AddChartServerLayer();
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-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)
{
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;
}
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-12-05 17:16:14 +01:00
private void MapPointerExited(object sender, PointerRoutedEventArgs e)
{
2021-12-05 17:16:14 +01:00
mouseLocation.Visibility = Visibility.Collapsed;
mouseLocation.Text = string.Empty;
}
}
}