天気API
Current weather data - OpenWeatherMap
位置情報
| <StackLayout> | |
| <Frame Padding="24" | |
| BackgroundColor="#2196F3" | |
| CornerRadius="0"> | |
| <Label HorizontalTextAlignment="Center" | |
| FontSize="36" | |
| Text="Welcome to Xamarin.Forms!" | |
| TextColor="White" /> | |
| </Frame> | |
| <Label Padding="20,10" | |
| FontSize="Title" | |
| Text="お天気確認" /> | |
| <Entry x:Name="LocationText" | |
| Margin="20,5" | |
| Text="品川区" /> | |
| <Button Margin="20,5" | |
| Clicked="Button_Clicked" | |
| Text="天気取得" /> | |
| <Label x:Name="WeatherLabel" | |
| Margin="20,5" | |
| Text="" /> | |
| </StackLayout> |
| public partial class MainPage : ContentPage | |
| { | |
| private static readonly HttpClient _client = new HttpClient(); | |
| public MainPage() | |
| { | |
| InitializeComponent(); | |
| } | |
| private async void Button_Clicked(object sender, EventArgs e) | |
| { | |
| try | |
| { | |
| var request = new HttpRequestMessage | |
| { | |
| Method = HttpMethod.Post, | |
| RequestUri = new Uri($"https://api.openweathermap.org/data/2.5/weather?q={LocationText.Text},jp&units=metric&lang=ja&appid=faea356fd4ddaf18f9950b22a437c6c3") | |
| }; | |
| var response = await _client.SendAsync(request); | |
| response.EnsureSuccessStatusCode(); | |
| var json = await response.Content.ReadAsStringAsync(); | |
| var weather = JsonConvert.DeserializeObject<WeatherInfo>(json); | |
| WeatherLabel.Text = $"{weather.Name} の天気は {weather.Weather.FirstOrDefault().Main} {weather.Weather.FirstOrDefault().Description}。\n" + | |
| $"現在の気温は {weather.Main.Temp}℃で、湿度は {weather.Main.Humidity}%です。"; | |
| } | |
| catch (Exception ex) | |
| { | |
| WeatherLabel.Text = "見つかりませんでした。"; | |
| } | |
| } | |
| } |
| public class WeatherInfo | |
| { | |
| [JsonProperty("coord")] | |
| public Coord Coord { get; set; } | |
| [JsonProperty("weather")] | |
| public List<Weather> Weather { get; set; } | |
| [JsonProperty("base")] | |
| public string Base { get; set; } | |
| [JsonProperty("main")] | |
| public Main Main { get; set; } | |
| [JsonProperty("visibility")] | |
| public long Visibility { get; set; } | |
| [JsonProperty("wind")] | |
| public Wind Wind { get; set; } | |
| [JsonProperty("clouds")] | |
| public Clouds Clouds { get; set; } | |
| [JsonProperty("dt")] | |
| public long Dt { get; set; } | |
| [JsonProperty("sys")] | |
| public Sys Sys { get; set; } | |
| [JsonProperty("timezone")] | |
| public long Timezone { get; set; } | |
| [JsonProperty("id")] | |
| public long Id { get; set; } | |
| [JsonProperty("name")] | |
| public string Name { get; set; } | |
| [JsonProperty("cod")] | |
| public long Cod { get; set; } | |
| } | |
| public class Clouds | |
| { | |
| [JsonProperty("all")] | |
| public long All { get; set; } | |
| } | |
| public class Coord | |
| { | |
| [JsonProperty("lon")] | |
| public double Lon { get; set; } | |
| [JsonProperty("lat")] | |
| public double Lat { get; set; } | |
| } | |
| public class Main | |
| { | |
| [JsonProperty("temp")] | |
| public double Temp { get; set; } | |
| [JsonProperty("feels_like")] | |
| public double FeelsLike { get; set; } | |
| [JsonProperty("temp_min")] | |
| public double TempMin { get; set; } | |
| [JsonProperty("temp_max")] | |
| public double TempMax { get; set; } | |
| [JsonProperty("pressure")] | |
| public long Pressure { get; set; } | |
| [JsonProperty("humidity")] | |
| public long Humidity { get; set; } | |
| } | |
| public class Sys | |
| { | |
| [JsonProperty("type")] | |
| public long Type { get; set; } | |
| [JsonProperty("id")] | |
| public long Id { get; set; } | |
| [JsonProperty("country")] | |
| public string Country { get; set; } | |
| [JsonProperty("sunrise")] | |
| public long Sunrise { get; set; } | |
| [JsonProperty("sunset")] | |
| public long Sunset { get; set; } | |
| } | |
| public class Weather | |
| { | |
| [JsonProperty("id")] | |
| public long Id { get; set; } | |
| [JsonProperty("main")] | |
| public string Main { get; set; } | |
| [JsonProperty("description")] | |
| public string Description { get; set; } | |
| [JsonProperty("icon")] | |
| public string Icon { get; set; } | |
| } | |
| public class Wind | |
| { | |
| [JsonProperty("speed")] | |
| public double Speed { get; set; } | |
| [JsonProperty("deg")] | |
| public long Deg { get; set; } | |
| } |
| try | |
| { | |
| var location = await Geolocation.GetLastKnownLocationAsync(); | |
| if (location != null) | |
| { | |
| var request = new HttpRequestMessage | |
| { | |
| Method = HttpMethod.Post, | |
| RequestUri = new Uri($"https://api.openweathermap.org/data/2.5/weather?lat={location.Latitude}&lon={location.Longitude}&units=metric&lang=ja&appid=faea356fd4ddaf18f9950b22a437c6c3") | |
| }; | |
| var response = await _client.SendAsync(request); | |
| response.EnsureSuccessStatusCode(); | |
| var json = await response.Content.ReadAsStringAsync(); | |
| var weather = JsonConvert.DeserializeObject<WeatherInfo>(json); | |
| WeatherLabel.Text = $"{weather.Name} の天気は {weather.Weather.FirstOrDefault().Main} {weather.Weather.FirstOrDefault().Description}。\n" + | |
| $"現在の気温は {weather.Main.Temp}℃で、湿度は {weather.Main.Humidity}%です。"; | |
| } | |
| } | |
| catch (FeatureNotSupportedException fnsEx) | |
| { | |
| // Handle not supported on device exception | |
| WeatherLabel.Text = fnsEx.Message; | |
| } | |
| catch (FeatureNotEnabledException fneEx) | |
| { | |
| // Handle not enabled on device exception | |
| WeatherLabel.Text = fneEx.Message; | |
| } | |
| catch (PermissionException pEx) | |
| { | |
| // Handle permission exception | |
| WeatherLabel.Text = pEx.Message; | |
| } | |
| catch (Exception ex) | |
| { | |
| // Unable to get location | |
| WeatherLabel.Text = ex.Message; | |
| } |