Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace UnweWeatherApp;
- public partial class MainPage : ContentPage
- {
- OpenWeatherService _openWeatherService;
- public MainPage()
- {
- InitializeComponent();
- _openWeatherService = new OpenWeatherService();
- GetWeatherWithGeoLocation();
- }
- string GenerateRequestUri(string endpoint)
- {
- string requestUri = endpoint;
- requestUri += $"?q={_cityEntry.Text}";
- requestUri += "&units=metric";
- requestUri += $"&APPID={Constants.OpenWeatherMapAPIKey}";
- return requestUri;
- }
- public async void OnGetWeatherButtonClicked(object sender, EventArgs e)
- {
- if (!string.IsNullOrWhiteSpace(_cityEntry.Text))
- {
- WeatherData weatherData = await _openWeatherService.GetWeatherData(GenerateRequestUri(Constants.OpenWeatherMapEndpoint));
- _weatherIcon.Source = "https://openweathermap.org/img/wn/" + weatherData.weather[0].icon + "@2x.png";
- BindingContext = weatherData;
- }
- }
- public async void GetWeatherWithGeoLocation()
- {
- var location = await Geolocation.GetLocationAsync();
- if (location != null)
- {
- var lat = location.Latitude;
- var lon = location.Longitude;
- WeatherData weatherData = await _openWeatherService.GetWeatherData(GenerateRequestUriGeo(Constants.OpenWeatherMapEndpoint, lat, lon));
- _weatherIcon.Source = "https://openweathermap.org/img/wn/" + weatherData.weather[0].icon + "@2x.png";
- BindingContext = weatherData;
- }
- }
- string GenerateRequestUriGeo(string endpoint, double lati, double longt)
- {
- string requestUri = endpoint;
- requestUri += $"?lat={lati}";
- requestUri += $"&lon={longt}";
- requestUri += "&units=imperial";
- requestUri += $"&APPID={Constants.OpenWeatherMapAPIKey}";
- return requestUri;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement