'How to write logic based off of selection in Picker
I have an application that based off of your zipcode gathers weather information from the OpenWeather API. I am trying to change it to have a drop down that allows you to select the city that you want information for instead of inputting the zipcode, but I am not sure how the logic works out.
XAML
<StackLayout>
<!-- Screen Controls and Text -->
<Label Text="Welcome to Isaac's Weather App!:"
HorizontalOptions="Center"
HorizontalTextAlignment="Center"
VerticalOptions="CenterAndExpand"
FontSize="30"/>
<Picker x:Name="picker"
Title="Select City"
SelectedItem=""/>
<Button x:Name="ShowTemp"
VerticalOptions="CenterAndExpand"
Padding="30"
Text="Show Current Temp"
Margin="10"
Clicked="ShowTemp_Clicked"/>
</StackLayout>
API Code
public MainPage()
{
InitializeComponent();
var citylist = new List<string>();
citylist.Add("LA");
citylist.Add("New York");
citylist.Add("Orlando");
picker.ItemsSource = citylist;
}
private void ShowTemp_Clicked(object sender, EventArgs e)
{
using (WebClient wc = new WebClient()) //New webclient
{
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; //which web client
try
{
string jsontext = wc.DownloadString($"https://api.openweathermap.org/data/2.5/weather?zip={EntryZipCode.Text}&appid={APIKEY}%units=imperial"); //Get json text
JObject jo = JObject.Parse(jsontext); //Parse json
JObject main = JObject.Parse(jo["main"].ToString()); //parse main section
WeatherGV.CurTemp = main["temp"].ToString(); //get temperature
WeatherGV.CityName = jo["name"].ToString(); //get city name
WeatherGV.LowTemp = jo["temp_min"].ToString(); //get low temp
WeatherGV.HighTemp = jo["temp_max"].ToString(); //get high temp
WeatherGV.WindSpeed = jo["speed"].ToString(); //get wind speed
WeatherGV.WindDirection = jo["deg"].ToString(); //get wind direction
WeatherGV.Pressure = jo["pressure"].ToString(); //get air pressure
WeatherGV.Humidity = jo["humidity"].ToString(); //get humidity
WeatherGV.Sunrise = jo["sunrise"].ToString(); //get sunrise
WeatherGV.Sunset = jo["sunset"].ToString(); //get sunset
Navigation.PushAsync(new WeatherPage());
}
I know I need to convert the city into my EntryZipCode variable that I had from before, but I don't know how to get the city into the zipcode.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
