DeliveryEditMapView.xaml.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Syncfusion.SfMaps.XForms;
  8. using Xamarin.Essentials;
  9. using Xamarin.Forms;
  10. using Xamarin.Forms.Xaml;
  11. namespace PRS.Mobile
  12. {
  13. [XamlCompilation(XamlCompilationOptions.Compile)]
  14. public partial class DeliveryEditMapView
  15. {
  16. private string? _currentaddress;
  17. private Location? _currentlocation;
  18. public DeliveryEditMapView()
  19. {
  20. InitializeComponent();
  21. }
  22. public override void Refresh()
  23. {
  24. NoMap.IsVisible = true;
  25. Map.IsVisible = false;
  26. if (ViewModel == null)
  27. return;
  28. var address = $"{ViewModel.Item.Street} {ViewModel.Item.City} {ViewModel.Item.PostCode}".Trim()
  29. .Replace(" ", " ");
  30. if (string.Equals(address, _currentaddress))
  31. {
  32. UpdateMap(_currentlocation);
  33. return;
  34. }
  35. Task.Run(async () =>
  36. {
  37. var locations = await Geocoding.GetLocationsAsync(address);
  38. _currentaddress = address;
  39. _currentlocation = locations.FirstOrDefault();
  40. Dispatcher.BeginInvokeOnMainThread(() => UpdateMap(_currentlocation));
  41. });
  42. }
  43. private void UpdateMap(Location? location)
  44. {
  45. if (location == null)
  46. return;
  47. NoMap.IsVisible = false;
  48. Map.IsVisible = true;
  49. if (Device.RuntimePlatform.Equals(Device.iOS))
  50. {
  51. Layer.MarkerSettings.MarkerIcon = MapMarkerIcon.Circle;
  52. Layer.MarkerSettings.IconColor = Color.DarkBlue;
  53. Layer.MarkerSettings.IconSize = 15;
  54. }
  55. else
  56. {
  57. Layer.MarkerSettings.MarkerIcon = MapMarkerIcon.Image;
  58. Layer.MarkerSettings.ImageSource = "mapmarker";
  59. Layer.MarkerSettings.IconSize = 35;
  60. }
  61. Layer.MarkerSettings.FontAttributes = FontAttributes.Bold;
  62. Layer.MarkerSettings.LabelSize = 20;
  63. Layer.MarkerSettings.LabelColor = Color.DarkBlue;
  64. MapMarker marker = new MapMarker();
  65. marker.Label = "";
  66. marker.Latitude = location.Latitude.ToString(CultureInfo.InvariantCulture);
  67. marker.Longitude = location.Longitude.ToString(CultureInfo.InvariantCulture);
  68. Layer.Markers.Add(marker);
  69. Layer.GeoCoordinates = new Point(location.Latitude, location.Longitude); //ViewModel.Coordinates;
  70. Map.ZoomLevel = 14;
  71. }
  72. }
  73. }