1.1 到以下網址,點選Sign up
https://openweathermap.org/

1.2 輸入使用者名稱及E-mail ,按下Create Account

1.3 選一下你的用途

2.註冊你要的API KEY。
2.1這裡以當下的天氣做示範,也可以選擇取得數日的紀錄。

2.2 先用個免費的來用一下,但一分鐘不能呼叫超過60次。

2.3 登入以取得API KEY

2.4 記下API KEY

3.切換到API文件。
https://openweathermap.org/current
可以透過City Name或是City ID來查詢,也可以透過經緯度及ZIP Code


如果要查City ID可以到這裡面下載city.list.json.gz
http://bulk.openweathermap.org/sample/

內容為Json格式,紀錄City ID及名稱還有經緯度及國家代碼

4.發HTTP Request取得資料。
4.1透過City Name 來查一下高雄的天氣
https://api.openweathermap.org/data/2.5/weather?q=Kaohsiung&appid=your API KEY

感覺那裡不對勁,原來是溫度怪怪的,要加一個參數units=metric 才會轉換成我們習慣的溫度單位。
我們加上參數後,API會變成以下這樣子,溫度就會變成習慣的單位。
https://api.openweathermap.org/data/2.5/weather?q=Kaohsiung&units=metric&appid=your api key

4.2 用City ID查詢高雄天氣
https://api.openweathermap.org/data/2.5/weather?id=7280289&units=metric&appid=your api key
跟用City Name不同的地方只要把q=Kaohsiung 換成id= 7280289

4.3 取得天氣Icon
仔細看了一下回應內容中的weather ,除了描述天氣外,還有一個icon

官方有提供取得Icon的方式 ,以高雄天氣的Icon為02n,則URL如下:
http://openweathermap.org/img/w/02n.png
會取得一個Icon
介紹就到這裡,之後再貼實際應用的例子。
參考文件:
https://openweathermap.org/current
感謝!
回覆刪除感謝
回覆刪除