예제 #1
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");
        }
예제 #2
0
        public SimplyWeatherLocation GetLocation()
        {
            const double Unknown   = -9999;
            double       longitude = Preferences.Get(_longitudeKey, Unknown);
            double       latitude  = Preferences.Get(_latitudeKey, Unknown);

            if (latitude != Unknown && longitude != Unknown)
            {
                return(SimplyWeatherLocation.Known(latitude, longitude));
            }

            return(SimplyWeatherLocation.Unknown());
        }