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()); }
public async Task <SimplyWeatherLocation> UpdateLocation(CancellationTokenSource cts) { SimplyWeatherLocation location = SimplyWeatherLocation.Unavailable(); try { location = await GetLastKnownLocation(); } catch (Exception) { Debug.WriteLine("Error retrieving last known location"); } if (location?.State != LocationState.LocationReady) { Debug.WriteLine("Last known location is not available, getting new location"); location = await FetchNewLocation(cts); } if (location.State == LocationState.LocationReady) { _appPrefs.SetLocationInfo(location); } return(location); }
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"); }
public async Task <CitiesInCircle> GetLocationName(SimplyWeatherLocation location) { var query = HttpUtility.ParseQueryString(string.Empty); query["lat"] = location.Latitude.ToString(); query["lon"] = location.Longitude.ToString(); query["appid"] = AppConfig.OPEN_WEATHER_API_KEY; query["cnt"] = "1"; HttpResponseMessage response = await _httpClient.GetAsync("find?" + query.ToString()); if (response?.IsSuccessStatusCode == true) { try { string rawContent = await response.Content.ReadAsStringAsync(); return(JsonConvert.DeserializeObject <CitiesInCircle>(rawContent)); } catch (Exception e) { Console.Error.WriteLine($"Error decoding json: {e.Message}"); } } return(null); }
public async Task <Models.Forecast> GetTodaysWeather(SimplyWeatherLocation location) { WeatherForecast weatherForecast = await _weatherApi.GetForecast(location); if (weatherForecast != null) { List <HourlyConditions> hourlyConditionsForDay = GetHourlyConditionsForDay(weatherForecast.Hourly); Models.Forecast forecast = new Models.Forecast { CurrentTemperature = weatherForecast.Current?.Temp != null ? (int)weatherForecast.Current.Temp : 0, HighTemp = GetHighTempForDay(hourlyConditionsForDay), LowTemp = GetLowempForDay(hourlyConditionsForDay), CurrentWindSpeed = weatherForecast.Current?.WindSpeed != null ? (int)weatherForecast.Current.WindSpeed : 0, FeelsLikeTemp = weatherForecast.Current?.FeelsLikeTemp != null ? (int)weatherForecast.Current.FeelsLikeTemp : 0, }; if (weatherForecast.Current?.Conditions?.Count > 0) { forecast.CurrentConditionsImageUrl = GetUrlForImageIcon(weatherForecast.Current.Conditions[0].Icon); forecast.CurrentConditions = weatherForecast.Current.Conditions[0].Description; } forecast.HourlyConditionsForDay = GetHourlyConditions(hourlyConditionsForDay); return(forecast); } return(null); }
public async Task <WeatherForecast> GetForecast(SimplyWeatherLocation location) { var query = HttpUtility.ParseQueryString(string.Empty); query["lat"] = location.Latitude.ToString(); query["lon"] = location.Longitude.ToString(); query["appid"] = AppConfig.OPEN_WEATHER_API_KEY; query["exclude"] = "minutely,alerts"; query["units"] = "imperial"; HttpResponseMessage response = await _httpClient.GetAsync("onecall?" + query.ToString()); if (response?.IsSuccessStatusCode == true) { try { string rawContent = await response.Content.ReadAsStringAsync(); return(JsonConvert.DeserializeObject <WeatherForecast>(rawContent)); } catch (Exception e) { Console.Error.WriteLine($"Error decoding json: {e.Message}"); } } return(null); }
private Point GetCenterCoordinate(SimplyWeatherLocation location, int zoom) { double n = Math.Pow(2, zoom); double xTile = n * ((location.Longitude + 180.0) / 360.0); //ytile = int((1.0 - math.asinh(math.tan(lat_rad)) / math.pi) / 2.0 * n) double latInRads = DegreesToRadians(location.Latitude); double yTile = ((1.0 - asinh(Math.Tan(latInRads)) / Math.PI) / 2.0 * n); return(new Point(xTile, yTile)); }
public async Task <string> GetForecastLocationName(SimplyWeatherLocation location) { CitiesInCircle citiesInCircle = await _weatherApi.GetLocationName(location); if (citiesInCircle != null && citiesInCircle.Cities?.Count > 0) { return(citiesInCircle.Cities.FirstOrDefault().CityName); } return("Unknown"); }
public async Task <List <DayForecast> > GetExtendedForecast(SimplyWeatherLocation location) { WeatherForecast weatherForecast = await _weatherApi.GetForecast(location); if (weatherForecast != null) { List <DayForecast> daysForecast = GetDayForecast(weatherForecast.Daily); return(daysForecast); } return(null); }
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()); }
public List <RadarTile> GetRadarTiles(SimplyWeatherLocation location, int zoom) { List <RadarTile> radarTiles = new List <RadarTile>(); Point centerTileCoordinates = GetCenterCoordinate(location, zoom); //add tiles for a 9x9 grid starting in the upper left corner for (int rowOffset = -1; rowOffset < 2; rowOffset++) { for (int columnOffset = -1; columnOffset < 2; columnOffset++) { Point tileCoordinate = new Point(centerTileCoordinates.X + columnOffset, centerTileCoordinates.Y + rowOffset); radarTiles.Add(new RadarTile { coordinate = tileCoordinate, MapUrl = GetMapUrl(tileCoordinate, zoom), RadarUrl = GetRadarUrl(tileCoordinate, zoom) }); } } return(radarTiles); }
public void SetLocationInfo(SimplyWeatherLocation location) { Preferences.Set(_latitudeKey, location.Latitude); Preferences.Set(_longitudeKey, location.Longitude); }