GPSInfoPage.xaml.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Xamarin.Forms;
  6. using Xamarin.Forms.Maps;
  7. using InABox.Core;
  8. using Comal.Classes;
  9. using XF.Material.Forms.UI.Dialogs;
  10. namespace comal.timesheets
  11. {
  12. public class GPSJobModel
  13. {
  14. public String JobNumber { get; set; }
  15. public String JobName { get; set; }
  16. public double Distance { get; set; }
  17. }
  18. public partial class GPSInfoPage : ContentPage
  19. {
  20. Location _here = null;
  21. CoreTable _jobs = null;
  22. public void GetLocation(double latitude, double longitude)
  23. {
  24. Task.Run(async () =>
  25. {
  26. String address = "";
  27. var coder = new Geocoder();
  28. var location = await coder.GetAddressesForPositionAsync(new Xamarin.Forms.Maps.Position(latitude, longitude));
  29. if (location.Any())
  30. address = location.First();
  31. Device.BeginInvokeOnMainThread(() => { Location.Text = address; });
  32. });
  33. }
  34. public GPSInfoPage(CoreTable jobs)
  35. {
  36. _here = new Location() { Latitude = App.GPS.Latitude, Longitude = App.GPS.Longitude };
  37. _jobs = jobs;
  38. InitializeComponent();
  39. NavigationPage.SetHasBackButton(this, false);
  40. ToolbarItems.Clear();
  41. ToolbarItems.Add(new ToolbarItem("Back", "", () =>
  42. {
  43. Navigation.PopAsync();
  44. }));
  45. Title = "GPS Info";
  46. GetLocation(App.GPS.Latitude, App.GPS.Longitude);
  47. TimeStamp.Text = String.Format("{0:dd/MM/yy hh:mm:ss tt}", App.GPS.TimeStamp);
  48. }
  49. protected override async void OnAppearing()
  50. {
  51. base.OnAppearing();
  52. await LoadData();
  53. }
  54. private async Task LoadData()
  55. {
  56. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading Data"))
  57. {
  58. List<GPSJobModel> jobmodel = new List<GPSJobModel>();
  59. if (_jobs != null)
  60. {
  61. foreach (CoreRow row in _jobs.Rows)
  62. {
  63. Location job = new Location()
  64. {
  65. Latitude = row.Get<Job, double>(x => x.SiteAddress.Location.Latitude),
  66. Longitude = row.Get<Job, double>(x => x.SiteAddress.Location.Longitude),
  67. };
  68. if ((job.Latitude != 0.0F) && (job.Longitude != 0.0F))
  69. {
  70. jobmodel.Add(
  71. new GPSJobModel()
  72. {
  73. JobNumber = row.Get<Job, String>(x => x.JobNumber),
  74. JobName = row.Get<Job, String>(x => x.Name),
  75. Distance = _here.DistanceTo(job, UnitOfLength.Kilometers),
  76. }
  77. );
  78. }
  79. }
  80. }
  81. Device.BeginInvokeOnMainThread(
  82. () =>
  83. {
  84. JobList.ItemsSource = null;
  85. JobList.ItemsSource = jobmodel;
  86. }
  87. );
  88. }
  89. }
  90. }
  91. }