网页端获取天气信息:可以获取天气数据的开放平台有和风、心知、高德、百度等平台,本示例,从和风天气获取天气信息。如果之前没有使用过和风天气开发服务平台,需要注册账号。
和风天气开发服务平台:https://dev.qweather.com/
登录和风天气开发服务平台后,进入“开发服务控制台”,点击左侧“项目管理”,在右侧点击“创建项目”,然后填写“项目名称”,选择“免费订阅”,设置KEY为“Web API”,并给KEY起个名称,最后点击创建
创建好项目后,就可以通过网址获取天气信息了。免费用户获取天气信息的网址链接格式如下所示
https://devapi.qweather.com/v7/weather/now?{查询参数}
其中{查询参数},必须包含的是location和KEY,location是要查询的地理位置,key是你自己的key,点击上图中“KEY”下面的“查看”,可以复制到。完整的查询网址如下所示:
https://devapi.qweather.com/v7/weather/now?location=xxx&key=xxx
其中的xxx需要替换成你自己要查询的参数。location号码,可以通过下面的链接获得
https://geoapi.qweather.com/v2/city/lookup?location=深圳&key=xxx
上面链接中,“深圳”是你要查询的地理位置,你需要替换成你自己的,key也需要替换成你自己的,然后复制粘贴到浏览器后回车,就可以在网页中查找对应地址的id。如下所示,在网页中,还会出现城市对应的行政区,例如,“深圳”市中有“福田区
{"code": "200","location": [{"name": "深圳","id": "101280601","lat": "22.54700","lon": "114.08595","adm2": "深圳","adm1": "广东省","country": "中国","tz": "Asia/Shanghai","utcOffset": "+08:00","isDst": "0","type": "city","rank": "13","fxLink": "https://www.qweather.com/weather/shenzhen-101280601.html"},{"name": "福田","id": "101280603","lat": "22.54101","lon": "114.05096","adm2": "深圳","adm1": "广东省","country": "中国","tz": "Asia/Shanghai","utcOffset": "+08:00","isDst": "0","type": "city","rank": "25","fxLink": "https://www.qweather.com/weather/futian-101280603.html"},],"refer": {"sources": ["QWeather"],"license": ["QWeather Developers License"]}
}
现在我们可以查询到“福田区”的id号是101280603,现在我们把id号复制粘贴到查询天气的网址中,然后复制你的Key到网址中,假设我的key是d5a4d4as4d4f3as4df4sa,那么最终查询天气的网址就是
https://devapi.qweather.com/v7/weather/now?location=101280603&key=d5a4d4as4d4f3as4df4sa
把修改好的网址复制粘贴到浏览器回车,就可以查询到当地的天气信息了,如下所示
{"code": "200","updateTime": "2024-02-02T18:04+08:00","fxLink": "https://www.qweather.com/weather/futian-101280603.html","now": {"obsTime": "2024-02-02T17:49+08:00","temp": "23","feelsLike": "24","icon": "101","text": "多云","wind360": "33","windDir": "东北风","windScale": "2","windSpeed": "7","humidity": "77","precip": "0.0","pressure": "1008","vis": "30","cloud": "91","dew": "18"},"refer": {"sources": ["QWeather"],"license": ["CC BY-SA 4.0"]}
}
以上就是使用网址查询当地天气信息的方法,接下来在开发板上通过网络请求来查询天气信息