using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using Google.Maps;
using Google.Maps.StaticMaps;
using InABox.Core;
using InABox.Wpf;
using Syncfusion.Linq;
using Location = Google.Maps.Location;
namespace PRSDesktop.Forms
{
///
/// Interaction logic for MapForm.xaml
///
public partial class MapForm : ThemableWindow
{
String GOOGLE_KEY = "AIzaSyChZhoJCzeaOm8q2QMm2KJOgc26aCeopjQ";
StaticMapRequest request = new StaticMapRequest();
StaticMapService service = new StaticMapService();
private double _zoom = 18.0;
public BitmapImage ToImage(byte[] array)
{
using (var ms = new System.IO.MemoryStream(array))
{
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad; // here
image.StreamSource = ms;
image.EndInit();
return image;
}
}
public MapForm(double latitude, double longitude, DateTime timestamp, string? geometry = null)
{
InitializeComponent();
GoogleSigned.AssignAllServices(new GoogleSigned(GOOGLE_KEY));
request = new StaticMapRequest();
String sLoc = String.Format("{0},{1}", latitude, longitude);
Location location = new Location(sLoc);
request.Center = location;
request.Markers.Add(location);
request.Scale = 2;
request.Size = new MapSize(640,640); // Convert.ToInt32(ActualWidth)-20, Convert.ToInt32(ActualHeight)-20);
request.Format = GMapsImageFormats.JPG;
if (!string.IsNullOrWhiteSpace(geometry))
{
var perims = Serialization.Deserialize>>(geometry) ?? new Dictionary>();
var paths = new List();
foreach (var perim in perims)
{
var path = new Path()
{
Points = new Collection(perim.Value.Select(x => new Location($"{x.Y},{x.X}")).ToList()),
Color = MapColor.FromArgb(255, 255, 0, 0),
FillColor = MapColor.FromArgb(64, 255, 0, 0),
Weight = 1
};
paths.Add(path);
}
request.Paths = paths;
}
service = new StaticMapService();
GetImage();
TimeStamp.Content = String.Format("Last Updated {0:dd MMM yyy hh:mm:ss tt}", timestamp);
}
private void GetImage()
{
request.Zoom = (int)Math.Round(_zoom, 0);
var imageSource = new BitmapImage();
imageSource.BeginInit();
imageSource.StreamSource = service.GetStream(request);
imageSource.CacheOption = BitmapCacheOption.OnLoad;
imageSource.EndInit();
staticmap.Source = imageSource;
}
private void staticmap_MouseUp(object sender, MouseButtonEventArgs e)
{
Close();
}
private void ZoomOut_OnClick(object sender, RoutedEventArgs e)
{
_zoom -= 1.0;
GetImage();
}
private void ZoomIn_OnClick(object sender, RoutedEventArgs e)
{
_zoom += 1.0;
GetImage();
}
}
}