国内天气查询服务是一套REST风格的Web服务API,以HTTP形式提供了实时和未来天气查询服务。
国内天气查询服务分为基础服务和高级权限。
在基础服务中,用户可通过行政区划代码查询实时天气信息及未来5天天气预报。
在高级权限中,用户可通过经纬度查询实时天气信息、未来7天天气预报及未来24小时逐小时预报。同时,用户还可以通过高级权限获取空气质量指数、生活指数、气象预警等丰富信息。高级权限需付费开通,您可以联系我们开通15天试用并了解更多信息。
https://api.map.baidu.com/weather/v1/?district_id=222405&data_type=all&ak=你的ak //GET请求
https://api.map.baidu.com/weather/v1/?location=116.40387,39.91489&data_type=all&ak=你的ak //GET请求
注意:经纬度天气查询是付费服务,仅限高级权限用户使用,您可以联系我们开通15天试用并了解更多信息。
参数名 | 数据类型 | 是否必传 | 默认值 | 描述信息 |
---|---|---|---|---|
|
|
|
|
区县的行政区划编码,和location二选一 |
|
|
|
|
经纬度,经度在前纬度在后,逗号分隔。支持类型:bd09mc/bd09ll/wgs84/gcj02。开通高级权限后才能使用 |
|
|
|
|
开发者密钥,可在API控制台申请获得 |
|
|
|
|
请求数据类型。数据类型有:now/fc/index/alert/fc_hour/all,控制返回内容 |
|
|
|
|
返回格式,目前支持json/xml |
|
|
|
|
支持类型:wgs84/bd09ll/bd09mc/gcj02 |
注意:如果district_id和location同时传,默认以district_id为准;下列返回结果参数中,字段类型为高级字段的,仅在开通高级权限之后才会展示,否则不展示。
天气现象、风力、风向、生活指数、预警事件取值可在资源下载中查看。
参数名 | 参数类型 | 描述信息 | 返回条件 | 异常值 | 字段类型 |
---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
参数名 | 参数类型 | 描述信息 | 返回条件 | 异常值 | 字段类型 |
---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
status编码 | 错误原因 | 补充说明 |
---|---|---|
|
服务器内部错误 | 具体失败消息会在message提示 |
|
高级权限才允许使用location字段 | 用户需开通高级权限功能 |
|
缺少必要的请求参数 | 缺少参数的时候会抛出该异常 |
|
请求参数格式错误 | 请求参数类型错误时会抛出该异常, 如字符长度、数据类型、数据格式不规范等 |
|
身份验证失败 | ak 无效时会抛出该异常 |
|
timestamp超出有效时间范围 | timestamp 与服务端的当前时间相差超过1分钟 |
|
查询的区域编码无效 | 查询的区域编码与约定的编码不符 |
|
查询的经纬度值范围无效 | 查询的经纬度超出了正常的经纬度范围 |
|
不支持的数据类型 | 查询的数据类型不支持 |
|
不支持的语言类型 | 查询的语言类型不支持 |
|
经纬度所在地区无数据覆盖 | 输入的经纬度区域无数据覆盖 |
|
服务被禁用,请确认是否开通高级权限 | 查询的数据类型与用户类型不匹配,请确认是否开通高级权限 |
|
服务繁忙 | 其他类型错误 |
|
AK参数不存在 | 请求消息没有携带AK参数 |
|
MCODE参数不存在,mobile类型mcode参数必需 | 对于Mobile类型的应用请求需要携带mcode参数,该错误码代表服务器没有解析到mcode |
|
APP不存在,AK有误请检查再重试 | 根据请求的AK,找不到对应的APP |
|
APP被用户自己禁用,请在控制台解禁 | |
|
APP被管理员删除 | 恶意APP被管理员删除 |
|
APP类型错误 | 当前API控制台支持Server(类型1), Mobile(类型2, 新版控制台区分为Mobile_Android(类型21)及Mobile_IPhone(类型22))及Browser(类型3),除此之外其他类型认为是APP类型错误 |
|
APP IP校验失败 | 在申请SERVER类型应用的时候选择IP校验,需要填写IP白名单,如果当前请求的IP地址不在IP白名单或者不是0.0.0.0/0就认为IP校验失败 |
|
APP SN校验失败 | SERVER类型APP有两种校验方式IP校验和SN校验,当用户请求的SN和服务端计算出来的SN不相等的时候提示SN校验失败 |
|
APP Referer校验失败 | 浏览器类型的APP会校验referer字段是否存且切在referer白名单里面,否则返回该错误码 |
|
APP Mcode码校验失败 | 服务器能解析到mcode,但和数据库中不一致,请携带正确的mcode |
|
APP 服务被禁用 | 用户在API控制台中创建或设置某APP的时候禁用了某项服务,若需开通权限,可进入API控制台为AK勾选对应服务 |
|
用户不存在 | 根据请求的user_id, 数据库中找不到该用户的信息,请携带正确的user_id |
|
用户被自己删除 | 该用户处于未激活状态 |
|
用户被管理员删除 | 恶意用户被加入黑名单 |
|
服务不存在 | 服务器解析不到用户请求的服务名称 |
|
服务被禁用 | 该服务已下线,请在控制台确认是否有该服务的权限 |
|
永久配额超限,限制访问 | 配额超限,可在控制台购买提升配额 |
|
天配额超限,限制访问 | 配额超限,可在控制台购买提升配额 |
|
当前并发量已经超过约定并发配额,限制访问 | 并发控制超限,可在控制台购买提升并发 |
|
当前并发量已经超过约定并发配额,并且服务总并发量也已经超过设定的总并发配额,限制访问 | 并发控制超限,可在控制台购买提升并发 |
请求类型为all的响应示例(部分字段需开通高级权限,具体请参考上方文档说明)
{ "status":0, "result":{ "location":{ "country":"中国", "province":"北京市", "city":"北京市", "name":"东城", "id":"110101" }, "now":{ "temp":4, "feels_like":1, "rh":73, "wind_class":"2级", "wind_dir":"东风", "text":"多云", "prec_1h":0, "clouds":999999, "vis":3471, "aqi":140, "pm25":107, "pm10":0, "no2":23, "so2":22, "o3":70, "co":1.7, "uptime":"20200220143500" }, "indexes":[ { "name":"晨练指数", "brief":"较适宜", "detail":"天气阴沉,请避免在林中晨练。" }, { "name":"洗车指数", "brief":"适宜", "detail":"天气较好,适合擦洗汽车。" }, { "name":"感冒指数", "brief":"易发", "detail":"天凉,昼夜温差大,易感冒" }, { "name":"紫外线指数", "brief":"最弱", "detail":"辐射弱,涂擦SPF8-12防晒护肤品。" }, { "name":"穿衣指数", "brief":"较冷", "detail":"建议着厚外套加毛衣等服装。" }, { "name":"运动指数", "brief":"较适宜", "detail":"气温较低,在户外运动请注意增减衣物。" } ], "alerts":[ { "type": "道路冰雪", "level": "蓝色预警", "title": "市气象局发布道路冰雪蓝色预警[IV级/一般]", "desc": "市气象局发布道路冰雪蓝色预警信号:受降雪天气影响, 预计未来 24 小时我市将出现对交通有影响的道路结冰或积雪, 请有关部门及广大群众做好防范工作。" } ], "forecasts":[ { "date":"2020-02-20", "week":"星期四", "high":7, "low":-2, "wc_day":"<3级", "wc_night":"<3级", "wd_day":"东南风", "wd_night":"北风", "text_day":"多云", "text_night":"阴", "aqi":93 }, { "date":"2020-02-21", "week":"星期五", "high":11, "low":1, "wc_day":"4~5级", "wc_night":"<3级", "wd_day":"西北风", "wd_night":"西北风", "text_day":"多云", "text_night":"晴", "aqi":44 }, { "date":"2020-02-22", "week":"星期六", "high":10, "low":-2, "wc_day":"<3级", "wc_night":"<3级", "wd_day":"西风", "wd_night":"南风", "text_day":"晴", "text_night":"晴", "aqi":39 }, { "date":"2020-02-23", "week":"星期日", "high":11, "low":0, "wc_day":"<3级", "wc_night":"<3级", "wd_day":"北风", "wd_night":"北风", "text_day":"晴", "text_night":"晴", "aqi":65 }, { "date":"2020-02-24", "week":"星期一", "high":9, "low":-1, "wc_day":"<3级", "wc_night":"<3级", "wd_day":"东风", "wd_night":"东北风", "text_day":"多云", "text_night":"多云", "aqi":38 }, { "date":"2020-02-25", "week":"星期二", "high":9, "low":-3, "wc_day":"<3级", "wc_night":"<3级", "wd_day":"东南风", "wd_night":"西南风", "text_day":"晴", "text_night":"晴", "aqi":27 }, { "date":"2020-02-26", "week":"星期三", "high":9, "low":-3, "wc_day":"<3级", "wc_night":"<3级", "wd_day":"西南风", "wd_night":"西南风", "text_day":"晴", "text_night":"晴", "aqi":26 } ], "forecast_hours":[ { "text":"晴", "temp_fc":14, "wind_class":"3~4级", "wind_dir":"西南风", "rh":15, "prec_1h":0, "clouds":10, "data_time":"2020-04-01 16:00:00" }, { "text":"晴", "temp_fc":14, "wind_class":"3~4级", "wind_dir":"西南风", "rh":13, "prec_1h":0, "clouds":10, "data_time":"2020-04-01 17:00:00" }, { "text":"晴", "temp_fc":13, "wind_class":"<3级", "wind_dir":"西南风", "rh":14, "prec_1h":0, "clouds":10, "data_time":"2020-04-01 18:00:00" }, { "text":"晴", "temp_fc":11, "wind_class":"<3级", "wind_dir":"西南风", "rh":15, "prec_1h":0, "clouds":10, "data_time":"2020-04-01 19:00:00" }, { "text":"晴", "temp_fc":10, "wind_class":"<3级", "wind_dir":"西南风", "rh":16, "prec_1h":0, "clouds":10, "data_time":"2020-04-01 20:00:00" }, { "text":"晴", "temp_fc":9, "wind_class":"<3级", "wind_dir":"西风", "rh":18, "prec_1h":0, "clouds":6, "data_time":"2020-04-01 21:00:00" }, { "text":"晴", "temp_fc":9, "wind_class":"<3级", "wind_dir":"西风", "rh":20, "prec_1h":0, "clouds":3, "data_time":"2020-04-01 22:00:00" }, { "text":"晴", "temp_fc":8, "wind_class":"<3级", "wind_dir":"西风", "rh":21, "prec_1h":0, "clouds":0, "data_time":"2020-04-01 23:00:00" }, { "text":"晴", "temp_fc":7, "wind_class":"<3级", "wind_dir":"西北风", "rh":26, "prec_1h":0, "clouds":0, "data_time":"2020-04-02 00:00:00" }, { "text":"晴", "temp_fc":6, "wind_class":"<3级", "wind_dir":"西北风", "rh":31, "prec_1h":0, "clouds":0, "data_time":"2020-04-02 01:00:00" }, { "text":"晴", "temp_fc":6, "wind_class":"<3级", "wind_dir":"西北风", "rh":36, "prec_1h":0, "clouds":0, "data_time":"2020-04-02 02:00:00" }, { "text":"晴", "temp_fc":5, "wind_class":"<3级", "wind_dir":"西北风", "rh":39, "prec_1h":0, "clouds":0, "data_time":"2020-04-02 03:00:00" }, { "text":"晴", "temp_fc":4, "wind_class":"<3级", "wind_dir":"西北风", "rh":42, "prec_1h":0, "clouds":0, "data_time":"2020-04-02 04:00:00" }, { "text":"晴", "temp_fc":4, "wind_class":"<3级", "wind_dir":"西北风", "rh":45, "prec_1h":0, "clouds":0, "data_time":"2020-04-02 05:00:00" }, { "text":"晴", "temp_fc":5, "wind_class":"<3级", "wind_dir":"西北风", "rh":40, "prec_1h":0, "clouds":0, "data_time":"2020-04-02 06:00:00" }, { "text":"晴", "temp_fc":7, "wind_class":"<3级", "wind_dir":"西北风", "rh":34, "prec_1h":0, "clouds":0, "data_time":"2020-04-02 07:00:00" }, { "text":"晴", "temp_fc":8, "wind_class":"<3级", "wind_dir":"西北风", "rh":29, "prec_1h":0, "clouds":0, "data_time":"2020-04-02 08:00:00" }, { "text":"晴", "temp_fc":11, "wind_class":"<3级", "wind_dir":"西北风", "rh":29, "prec_1h":0, "clouds":0, "data_time":"2020-04-02 09:00:00" }, { "text":"晴", "temp_fc":13, "wind_class":"<3级", "wind_dir":"西北风", "rh":29, "prec_1h":0, "clouds":0, "data_time":"2020-04-02 10:00:00" }, { "text":"晴", "temp_fc":16, "wind_class":"<3级", "wind_dir":"西北风", "rh":29, "prec_1h":0, "clouds":0, "data_time":"2020-04-02 11:00:00" }, { "text":"晴", "temp_fc":17, "wind_class":"3~4级", "wind_dir":"西北风", "rh":24, "prec_1h":0, "clouds":0, "data_time":"2020-04-02 12:00:00" }, { "text":"晴", "temp_fc":18, "wind_class":"3~4级", "wind_dir":"西北风", "rh":19, "prec_1h":0, "clouds":0, "data_time":"2020-04-02 13:00:00" }, { "text":"晴", "temp_fc":19, "wind_class":"3~4级", "wind_dir":"西北风", "rh":14, "prec_1h":0, "clouds":0, "data_time":"2020-04-02 14:00:00" }, { "text":"晴", "temp_fc":19, "wind_class":"<3级", "wind_dir":"西风", "rh":17, "prec_1h":0, "clouds":0, "data_time":"2020-04-02 15:00:00" } ] }, "message":"success" }
API请求中需要用到中文或一些特殊字符的参数,如query、region等,为了避免提交到后台乱码,需要对这几个参数值进行编码处理,转换成UTF-8字符的二字符十六进制值,凡是不在下表中的字符都需要进行编码。
字符集合 | 字符 |
URL非保留字 |
a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 - _ . ~ |
URL保留字 |
! * ' ( ) ; : @ & = + $ , / ? % # [ ] |
如果中文参数中使用URL保留字字符的字面意思,例如:region关键字取值为"?北京",检索关键字包含一个问号,此问号也必须进行编码。
附注:
javascript中一般采用encodeURIComponent函数对特殊字符进行编码。
Java中可以使用函数URLEncoder.encode对特殊字符进行编码。
C#中可以使用函数HttpUtility.UrlEncode对特殊字符进行编码。
php中可以使用函数urlencode对特殊字符进行编码。
WGS84:为一种大地坐标系,也是目前广泛使用的GPS全球卫星定位系统使用的坐标系。
GCJ02:是由中国国家测绘局制订的地理信息系统的坐标系统。由WGS84坐标系经加密后的坐标系。
BD09:为百度坐标系,在GCJ02坐标系基础上再次加密。其中bd09ll表示百度经纬度坐标,bd09mc表示百度墨卡托米制坐标
注意:不同类型坐标如需转换,请使用百度地图开放平台提供的标准坐标转换接口。请勿使用其他渠道提供的坐标转换方法,以免影响坐标转换精度。
服务相关配额及并发请求限制请访问开发者权益页面查看。
天气查询服务上线后,在控制台中未默认开启服务。请进入控制台-我的应用,创建新AK或设置原有AK,在启用服务选项中勾选国内天气查询,即可开启国内天气查询服务。
2020年2月21日
V1.0
2020年4月14日
V1.1