Ejemplo n.º 1
0
        private async Task <SimplyWeatherLocation> FetchNewLocation(CancellationTokenSource cts)
        {
            try
            {
                var request = new GeolocationRequest(GeolocationAccuracy.Low, TimeSpan.FromSeconds(15));
                cts = new CancellationTokenSource();
                var location = await Geolocation.GetLocationAsync(request, cts.Token);

                if (location != null)
                {
                    Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}, Altitude: {location.Altitude}");
                }
            }
            catch (FeatureNotSupportedException)
            {
                return(SimplyWeatherLocation.Unavailable());
            }
            catch (FeatureNotEnabledException)
            {
                return(SimplyWeatherLocation.Disabled());
            }
            catch (PermissionException)
            {
                return(SimplyWeatherLocation.PermissionRequired());
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Error retrieving location: {ex.StackTrace}");
            }

            return(SimplyWeatherLocation.Unknown());
        }
Ejemplo n.º 2
0
        private async Task <SimplyWeatherLocation> GetLastKnownLocation()
        {
            try
            {
                Xamarin.Essentials.Location lastKnownLocation = await Geolocation.GetLastKnownLocationAsync();

                if (lastKnownLocation != null)
                {
                    return(SimplyWeatherLocation.Known(lastKnownLocation.Latitude, lastKnownLocation.Longitude));
                }
            }
            catch (FeatureNotSupportedException)
            {
                return(SimplyWeatherLocation.Unavailable());
            }
            catch (FeatureNotEnabledException)
            {
                return(SimplyWeatherLocation.Disabled());
            }
            catch (PermissionException)
            {
                return(SimplyWeatherLocation.PermissionRequired());
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Error retrieving last known location: {e.StackTrace}");
                return(SimplyWeatherLocation.Unknown());
            }

            throw new Exception("Unable to get Last location");
        }