using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Xamarin.Forms; using Xamarin.Forms.Maps; using InABox.Core; using Comal.Classes; using XF.Material.Forms.UI.Dialogs; namespace comal.timesheets { public class GPSJobModel { public String JobNumber { get; set; } public String JobName { get; set; } public double Distance { get; set; } } public partial class GPSInfoPage : ContentPage { Location _here = null; CoreTable _jobs = null; public void GetLocation(double latitude, double longitude) { Task.Run(async () => { String address = ""; var coder = new Geocoder(); var location = await coder.GetAddressesForPositionAsync(new Xamarin.Forms.Maps.Position(latitude, longitude)); if (location.Any()) address = location.First(); Device.BeginInvokeOnMainThread(() => { Location.Text = address; }); }); } public GPSInfoPage(CoreTable jobs) { _here = new Location() { Latitude = App.GPS.Latitude, Longitude = App.GPS.Longitude }; _jobs = jobs; InitializeComponent(); NavigationPage.SetHasBackButton(this, false); ToolbarItems.Clear(); ToolbarItems.Add(new ToolbarItem("Back", "", () => { Navigation.PopAsync(); })); Title = "GPS Info"; GetLocation(App.GPS.Latitude, App.GPS.Longitude); TimeStamp.Text = String.Format("{0:dd/MM/yy hh:mm:ss tt}", App.GPS.TimeStamp); } protected override async void OnAppearing() { base.OnAppearing(); await LoadData(); } private async Task LoadData() { using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading Data")) { List jobmodel = new List(); if (_jobs != null) { foreach (CoreRow row in _jobs.Rows) { Location job = new Location() { Latitude = row.Get(x => x.SiteAddress.Location.Latitude), Longitude = row.Get(x => x.SiteAddress.Location.Longitude), }; if ((job.Latitude != 0.0F) && (job.Longitude != 0.0F)) { jobmodel.Add( new GPSJobModel() { JobNumber = row.Get(x => x.JobNumber), JobName = row.Get(x => x.Name), Distance = _here.DistanceTo(job, UnitOfLength.Kilometers), } ); } } } Device.BeginInvokeOnMainThread( () => { JobList.ItemsSource = null; JobList.ItemsSource = jobmodel; } ); } } } }