
Open Map WeatherのAPIを使えば東京だけでなくほかの場所の天気予報も取得できます。場所の指定は、緯度経度、都市名、city ID でできますが、city IDを知りたいのであればこちらからダウンロードして調べることができます。
これをみると国内だけでずいぶんたくさん指定することができることがわかります。今回は県庁所在地だけに限定しました。Dictionaryクラスをつかって都市名からcityIDを取得できるようにしています。
アプリケーションが開始されたらDictionaryクラスのインスタンスを生成して要素を追加していきます。そのあとコンボボックスにアイテムを追加していきます。
あとはGetJson()メソッドが実行されるときにコンボボックスのどのアイテムが選択されているかを調べてリクエストするだけです。それ以外の変更箇所はありません。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83  | 
						public partial class Form1 : Form {     public Form1()     {         InitializeComponent();         InitListView();         InitKeyValuePairs();         InitComboBox();     }     Dictionary<string, string> KeyValuePairs = new Dictionary<string, string>();     void InitKeyValuePairs()     {         KeyValuePairs.Add("札幌", "2128295");         KeyValuePairs.Add("青森", "2130658");         KeyValuePairs.Add("盛岡", "2111834");         KeyValuePairs.Add("秋田", "2113124");         KeyValuePairs.Add("仙台", "2111149");         KeyValuePairs.Add("山形", "2110556");         KeyValuePairs.Add("福島", "2112923");         KeyValuePairs.Add("水戸", "2111901");         KeyValuePairs.Add("宇都宮", "1849053");         KeyValuePairs.Add("前橋", "1857843");         KeyValuePairs.Add("さいたま", "6940394");         KeyValuePairs.Add("千葉", "2113015");         KeyValuePairs.Add("東京", "1850147");         KeyValuePairs.Add("横浜", "2127436");         KeyValuePairs.Add("山梨", "1848649");         KeyValuePairs.Add("長野", "1856215");         KeyValuePairs.Add("新潟", "1855431");         KeyValuePairs.Add("富山", "1849876");         KeyValuePairs.Add("金沢", "1860243");         KeyValuePairs.Add("福井", "1863983");         KeyValuePairs.Add("静岡", "1851717");         KeyValuePairs.Add("名古屋", "1856057");         KeyValuePairs.Add("岐阜", "1863640");         KeyValuePairs.Add("津", "1849796");         KeyValuePairs.Add("滋賀", "1852553");         KeyValuePairs.Add("京都", "1857910");         KeyValuePairs.Add("大阪", "1853909");         KeyValuePairs.Add("神戸", "1859171");         KeyValuePairs.Add("奈良", "1855612");         KeyValuePairs.Add("和歌山", "1926004");         KeyValuePairs.Add("鳥取", "1849892");         KeyValuePairs.Add("松江", "1857550");         KeyValuePairs.Add("岡山", "1854383");         KeyValuePairs.Add("広島", "1862415");         KeyValuePairs.Add("山口", "1848689");         KeyValuePairs.Add("高松", "1851099");         KeyValuePairs.Add("松山", "1926099");         KeyValuePairs.Add("徳島", "1850158");         KeyValuePairs.Add("高知", "1859146");         KeyValuePairs.Add("福岡", "1863967");         KeyValuePairs.Add("佐賀", "1853303");         KeyValuePairs.Add("長崎", "1856177");         KeyValuePairs.Add("熊本", "1858419");         KeyValuePairs.Add("大分", "1854484");         KeyValuePairs.Add("宮崎", "1856710");         KeyValuePairs.Add("鹿児島", "1860827");         KeyValuePairs.Add("那覇", "1856035");     }     void InitComboBox()     {         foreach (var a in KeyValuePairs)             comboBox1.Items.Add(a.Key);         comboBox1.SelectedIndex = 12; // とりあえず「東京」を選択     }     public async Task<string> GetJson()     {         int index = comboBox1.SelectedIndex;         Text = comboBox1.Items[index].ToString() + " の天気予報";         string value = KeyValuePairs[comboBox1.Items[index].ToString()];         string apiKey = "apiキーは各自で取得してください";         string url = String.Format("https://api.openweathermap.org/data/2.5/forecast?id={0}&units=metric&cnt=12&APPID={1}", value, apiKey);         return await httpClient.GetStringAsync(url);     } }  | 
					
