MapForm.xaml.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.Windows.Input;
  8. using System.Windows.Media.Imaging;
  9. using Google.Maps;
  10. using Google.Maps.StaticMaps;
  11. using InABox.Core;
  12. using InABox.Wpf;
  13. using Syncfusion.Linq;
  14. using Location = Google.Maps.Location;
  15. namespace PRSDesktop.Forms
  16. {
  17. /// <summary>
  18. /// Interaction logic for MapForm.xaml
  19. /// </summary>
  20. public partial class MapForm : ThemableWindow
  21. {
  22. String GOOGLE_KEY = "AIzaSyChZhoJCzeaOm8q2QMm2KJOgc26aCeopjQ";
  23. StaticMapRequest request = new StaticMapRequest();
  24. StaticMapService service = new StaticMapService();
  25. private double _zoom = 18.0;
  26. public BitmapImage ToImage(byte[] array)
  27. {
  28. using (var ms = new System.IO.MemoryStream(array))
  29. {
  30. var image = new BitmapImage();
  31. image.BeginInit();
  32. image.CacheOption = BitmapCacheOption.OnLoad; // here
  33. image.StreamSource = ms;
  34. image.EndInit();
  35. return image;
  36. }
  37. }
  38. public MapForm(double latitude, double longitude, DateTime timestamp, string? geometry = null)
  39. {
  40. InitializeComponent();
  41. GoogleSigned.AssignAllServices(new GoogleSigned(GOOGLE_KEY));
  42. request = new StaticMapRequest();
  43. String sLoc = String.Format("{0},{1}", latitude, longitude);
  44. Location location = new Location(sLoc);
  45. request.Center = location;
  46. request.Markers.Add(location);
  47. request.Scale = 2;
  48. request.Size = new MapSize(640,640); // Convert.ToInt32(ActualWidth)-20, Convert.ToInt32(ActualHeight)-20);
  49. request.Format = GMapsImageFormats.JPG;
  50. if (!string.IsNullOrWhiteSpace(geometry))
  51. {
  52. var perims = Serialization.Deserialize<Dictionary<string, List<PointF>>>(geometry) ?? new Dictionary<string, List<PointF>>();
  53. var paths = new List<Path>();
  54. foreach (var perim in perims)
  55. {
  56. var path = new Path()
  57. {
  58. Points = new Collection<Location>(perim.Value.Select(x => new Location($"{x.Y},{x.X}")).ToList()),
  59. Color = MapColor.FromArgb(255, 255, 0, 0),
  60. FillColor = MapColor.FromArgb(64, 255, 0, 0),
  61. Weight = 1
  62. };
  63. paths.Add(path);
  64. }
  65. request.Paths = paths;
  66. }
  67. service = new StaticMapService();
  68. GetImage();
  69. TimeStamp.Content = String.Format("Last Updated {0:dd MMM yyy hh:mm:ss tt}", timestamp);
  70. }
  71. private void GetImage()
  72. {
  73. request.Zoom = (int)Math.Round(_zoom, 0);
  74. var imageSource = new BitmapImage();
  75. imageSource.BeginInit();
  76. imageSource.StreamSource = service.GetStream(request);
  77. imageSource.CacheOption = BitmapCacheOption.OnLoad;
  78. imageSource.EndInit();
  79. staticmap.Source = imageSource;
  80. }
  81. private void staticmap_MouseUp(object sender, MouseButtonEventArgs e)
  82. {
  83. Close();
  84. }
  85. private void ZoomOut_OnClick(object sender, RoutedEventArgs e)
  86. {
  87. _zoom -= 1.0;
  88. GetImage();
  89. }
  90. private void ZoomIn_OnClick(object sender, RoutedEventArgs e)
  91. {
  92. _zoom += 1.0;
  93. GetImage();
  94. }
  95. }
  96. }