using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Maui.ApplicationModel; namespace Justice.Helpers { public class GeolocationHelper { /// /// Retrieves the user's current latitude, longitude, and a readable address. /// /// A tuple containing Latitude, Longitude, and Address. public static async Task<(double Latitude, double Longitude, string Address)> GetCurrentLocationAsync() { try { var location = await Geolocation.Default.GetLocationAsync(new GeolocationRequest { DesiredAccuracy = GeolocationAccuracy.High, Timeout = TimeSpan.FromSeconds(10) }); if (location != null) { var placemarks = await Geocoding.Default.GetPlacemarksAsync(location); var placemark = placemarks.FirstOrDefault(); string address = "Address not found"; if (placemark != null) { address = $"{placemark.SubLocality}, {placemark.Thoroughfare}, " + $"{placemark.Locality}, {placemark.AdminArea}, {placemark.CountryName}"; } return (location.Latitude, location.Longitude, address); } // Return a default value if location is null return (0, 0, "Unable to fetch location."); } catch (FeatureNotSupportedException) { // Return a default value if location services are not supported return (0, 0, "Location services are not supported on this device."); } catch (PermissionException) { // Return a default value if permissions are not granted return (0, 0, "Location permissions are not granted."); } catch (Exception ex) { // Return a default value for other errors return (0, 0, $"An error occurred: {ex.Message}"); } } } }