动态数据
本分类包含 2 个接口,都是有时效性的数据:天气、实时路况。这类数据不要做长期归档,也不要假定缓存有效。
段落顺序同其他分类:接口说明 → API 服务地址 → 请求参数 → 返回参数 → 返回示例 → 服务状态码。
| 接口 | 路径 | 更新频率 | 缓存建议 |
|---|---|---|---|
| 天气查询 | /weather/v1/ |
实况约每小时,预报每日 | 实况 30 分钟,预报 6 小时 |
| 实时路况查询 | /traffic/v1/road |
分钟级(1 分钟) | 不缓存,或不超过 1 分钟 |
1. 天气查询
1.1 接口说明
查询指定区县的实时天气与未来 7 天预报,也可返回生活指数、预警信息、逐小时预报。
按查询方式分四种能力:
| 能力 | 用什么参数 |
|---|---|
| 国内按行政区划查 | district_id(区县 adcode) |
| 国内按经纬度查 | location |
| 按名称查 | province + city + district |
| 海外查询 | 同上,district_id 换成海外区划编码 |
1.2 API 服务地址
按区划编码:
GET https://itruelink.top/weather/v1/?district_id={区县adcode}&data_type=all&ak={KEY}
按经纬度:
GET https://itruelink.top/weather/v1/?location={lng,lat}&data_type=all&ak={KEY}
1.3 请求参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
district_id |
string | 三选一 | — | 区县行政区划编码(注意是区县级,不是城市级) |
location |
string | 三选一 | — | 经纬度,经度,纬度(见下方警告) |
district |
string | 三选一 | — | 区县名称,如 历下区。可与 province / city 组合提高命中率 |
data_type |
string | 是 | — | 返回内容类型,见下表 |
province |
string | 否 | — | 省份名,辅助名称查询 |
city |
string | 否 | — | 城市名,辅助名称查询 |
coordtype |
string | 否 | wgs84 |
location 的坐标系:wgs84 / bd09ll / bd09mc / gcj02 |
output |
string | 否 | json |
默认就是 json,但建议显式传 |
data_type 取值:
| 值 | 返回内容 |
|---|---|
now |
仅实况 |
fc |
仅未来 7 天预报 |
index |
仅生活指数 |
alert |
仅预警信息 |
fc_hour |
仅未来 24 小时逐小时预报 |
all |
以上全部 |
两处与其他接口不同的地方,必须注意:
①
location是「经度,纬度」,与路线规划、逆地理编码、地点检索的「纬度,经度」相反。②
coordtype默认是wgs84,不是 BD09。如果你传入的是接口返回的 BD09 坐标却没声明coordtype=bd09ll,位置会偏几百米——在城市尺度上可能偏到隔壁区,天气结果就错了。这两条是本手册里最容易出错的地方之一,因为它和其他接口的约定不一致。
另外:district_id 与 location 同时传时,以 district_id 为准,location 被忽略。
1.4 返回参数
| 字段 | 类型 | 说明 |
|---|---|---|
status |
int | 0 成功 |
message |
string | 说明,成功时是 "success" |
result |
object | 全部业务数据都在 result 里,顶层没有 address |
result 内字段:
| 字段 | 类型 | 说明 |
|---|---|---|
location |
object | 地理位置信息:country / province / city / name(区县名)/ id(区县编码) |
now |
object | 实况数据 |
forecasts |
array | 未来 7 天预报 |
indexes |
array | 生活指数 |
alerts |
array | 预警信息,无预警时是空数组 [] |
forecast_hours |
array | 逐小时预报,需 data_type=fc_hour |
⚠️ 顶层是
result,不是address。 写data["address"]会直接KeyError。 地理位置对象叫result.location,字段与常见预期一致(country/province/city/name/id)。
now 内字段(实测返回):
| 字段 | 类型 | 说明 |
|---|---|---|
text |
string | 天气现象,如 多云 |
temp |
int | 温度(℃) |
feels_like |
int | 体感温度(℃) |
rh |
int | 相对湿度(%) |
wind_class |
string | 风力等级,如 1级、4~5级 |
wind_dir |
string | 风向,如 西南风 |
wind_angle |
int | 风向角度 |
prec_1h |
float | 过去 1 小时降水量(mm)。没有 precise 字段 |
clouds |
int | 云量(%) |
vis |
int | 能见度(米) |
aqi / pm25 / pm10 / no2 / so2 / o3 / co |
int/float | 空气质量分项 |
uvi |
int | 紫外线指数 |
pressure / slp |
int | 气压 / 海平面气压 |
dpt |
int | 露点温度 |
uptime |
string | 数据更新时间,格式 20261118190500(紧凑格式,不是 2026-11-18 19:05:00) |
forecasts[] 内字段:date(2026-11-18)、week(星期三)、text_day / text_night、high / low(最高 / 最低温)、wc_day / wd_day / wc_night / wd_night(白天 / 夜间的风力与风向)、wind_angle_day / wind_angle_night、uvi、pressure、slp、dpt。
indexes[] 内字段:name / brief / detail——没有 level 和 desc。例如:
{ "name": "晨练指数", "brief": "较适宜", "detail": "感觉有点热,选择通风凉爽的地点晨练。" }
forecast_hours[] 内字段:text、temp_fc(逐小时温度叫 temp_fc,不叫 temp)、wind_class、wind_dir、rh、prec_1h、clouds、wind_angle、pop(降水概率)、uvi、pressure、slp、dpt、data_time。
temp的异常值是999999。某些站点数据缺失时会返回这个值,直接展示会给用户看到「999999℃」。必须做范围校验(合理区间约 -60 到 60)。
indexes[]的项数与内容随地区变化:同一时刻不同区县可能返回 6 项或 8 项,名称也不完全一致。写解析代码前先打印一次原始响应,按name取值,不要按下标硬编码。
1.5 返回示例
GET /weather/v1/?district_id=310110&data_type=all 的真实返回(indexes / forecasts / forecast_hours 已裁剪为各 1 条,实际分别返回 6 项 / 7 天 / 24 小时):
{
"status": 0,
"message": "success",
"result": {
"location": { "country": "中国", "province": "上海市", "city": "上海市", "name": "杨浦区", "id": "310110" },
"now": {
"text": "多云", "temp": 13, "feels_like": 12, "rh": 53,
"wind_class": "1级", "wind_dir": "西南风", "wind_angle": 242,
"prec_1h": 0, "clouds": 60, "vis": 10200,
"aqi": 29, "pm25": 11, "pm10": 29, "no2": 36, "so2": 10, "o3": 72, "co": 0.2,
"uvi": 0, "pressure": 1025, "slp": 1026, "dpt": 3,
"uptime": "20261118190500"
},
"indexes": [
{ "name": "晨练指数", "brief": "较适宜", "detail": "感觉有点热,选择通风凉爽的地点晨练。" }
],
"alerts": [],
"forecasts": [
{
"text_day": "多云", "text_night": "晴", "high": 14, "low": 6,
"wc_day": "<3级", "wd_day": "东风", "wc_night": "<3级", "wd_night": "西北风",
"wind_angle_day": 76, "wind_angle_night": 303,
"uvi": 7, "pressure": 1026, "slp": 1027, "dpt": 2,
"date": "2026-11-18", "week": "星期三"
}
],
"forecast_hours": [
{
"text": "阴", "temp_fc": 11, "wind_class": "4~5级", "wind_dir": "西北风",
"rh": 57, "prec_1h": 0, "clouds": 80, "wind_angle": 332,
"pop": 0, "uvi": 0, "pressure": 1026, "slp": 1027, "dpt": 3,
"data_time": "2026-11-18 19:00:00"
}
]
}
}
1.6 服务状态码
| 返回码 | 含义 | 处理 |
|---|---|---|
| 0 | 成功 | — |
| 2 | 参数非法 | data_type 未传或取值错、三个定位参数都未传 |
| 4 | 配额校验失败 | 当日额度用尽 |
| 5 | ak 非法 |
检查 KEY |
district_id 传了城市级而不是区县级时,可能返回 status: 0 但 address 为空——要判空。
1.7 一个实用的封装
TEMP_MIN, TEMP_MAX = -60, 60
def get_weather(lat=None, lng=None, district_id=None, data_type="all"):
"""
优先用 district_id(更准);没有则用经纬度。
注意经纬度传参顺序是 lng,lat —— 与多数接口相反
"""
params = {"data_type": data_type, "output": "json"}
if district_id:
params["district_id"] = district_id
elif lat is not None and lng is not None:
params["location"] = f"{lng:.6f},{lat:.6f}" # 经度在前!
params["coordtype"] = "bd09ll" # 显式声明,别依赖默认 wgs84
else:
raise ValueError("district_id 与经纬度必须提供其一")
data = request("/weather/v1/", **params)
if data.get("status") != 0:
raise ApiError(data)
res = data.get("result") or {} # 顶层是 result,不是 now/address
now = res.get("now") or {}
temp = now.get("temp")
return {
"region": (res.get("location") or {}).get("name"),
"text": now.get("text"),
# 过滤 999999 这类缺失值
"temp": temp if isinstance(temp, int) and TEMP_MIN <= temp <= TEMP_MAX else None,
"wind": f"{now.get('wind_dir', '')}{now.get('wind_class', '')}".strip(),
"uptime": now.get("uptime"),
}
1.8 高德协议直通(不改代码即可切换)
本服务把高德 /v3/* 全系列接口做了协议级兼容——不只是天气。原本用高德的客户不需要改任何代码,把域名和 key 换掉即可获得高德原样的路径、参数名和返回结构:
GET https://itruelink.top/v3/weather/weatherInfo?city={adcode}&key={KEY}&extensions=base
已直通的高德路径:地理编码 / 逆地理编码、POI 关键字 / 周边 / 详情 / 输入提示、驾车 / 步行 / 骑行 / 公交路径规划、坐标转换、IP 定位、行政区划、天气。完整清单、各接口的语义差异(哪些字段上游拿不到)以及坐标系换算说明见 从高德迁移 · 协议直通。
网页端(JS API)也一并支持:<script src="https://itruelink.top/amap/api/maps?v=2.0&key={KEY}"> 加载的是本服务提供的兼容运行时,页面里的 new AMap.Map / AMap.Marker / AMap.Geocoder / AMap.Driving 一行都不用改。支持范围与能力边界见 从高德迁移 · 网页端。
下面是天气这一路的细节,其余接口的字段对齐规则与之同理。
| 参数 | 说明 |
|---|---|
key |
本服务控制台创建的 KEY(沿用高德的参数名 key,不用改成 ak) |
city |
6 位 adcode,如 310110 |
extensions |
base 返回实况(lives[]),all 返回预报(forecast[].casts[]);缺省为 base |
output |
JSON(默认)或 XML |
实况响应与高德逐字段一致:
{
"status": "1", "count": "1", "info": "OK", "infocode": "10000",
"lives": [{
"province": "上海市", "city": "上海市", "adcode": "310110",
"weather": "多云", "temperature": "16", "winddirection": "东北风",
"windpower": "1", "humidity": "66", "reporttime": "2026-11-16 23:05:00"
}]
}
需要注意的三点:
- 返回全部是字符串(温度是
"16"),与高德一致 - 风力不带「级」字(
≤3/3-4/1),与高德一致 - 预报固定 3 天、字段与高德一致;
extensions=all不返回lives
错误同样按高德协议返回(HTTP 200 + status: "0" + infocode),客户原有的 if (data.status !== '1') 分支能正常触发。完整错误码对照见 从高德迁移 · 协议直通。
这两套路径指向同一个接口、共用同一份额度:
/v3/weather/weatherInfo与/weather/v1/调的都是天气查询,计费口径都是同一个weather接口,额度只有一份。新旧代码可以同时在线,不存在停机窗口。其余接口同理,/v3/*与原生路径共享同一份额度。注意:天气的浏览器端口径(
weather:js)默认未开通,网页里直接调会被拦成403 [gateway:category_disabled]。服务端调用不受影响。需要网页端调天气请联系服务商开通。
2. 实时路况查询
2.1 接口说明
查询指定道路的实时拥堵情况:整体评价、拥堵路段、拥堵距离、平均车速、拥堵趋势(较 10 分钟前加重 / 持平 / 缓解)。
除道路查询外,还有按矩形区域、多边形区域、周边半径查询的变体,路径前缀同为 /traffic。本节以最常用的道路查询为例。
适用:通行时间预估、拥堵播报、配送延误预警。
2.2 API 服务地址
GET https://itruelink.top/traffic/v1/road?road_name={道路名}&city={城市}&output=json&ak={KEY}
2.3 请求参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
road_name |
string | 是 | — | 道路名称,如 内环高架、四平路。不支持多方向立交桥与多方向道路(超过 2 个方向的道路) |
city |
string | 是 | — | 城市名(上海市)或城市级 adcode(310000)。只支持城市级,传区县级会查不到 |
coord_type_output |
string | 否 | bd09ll |
返回坐标类型:gcj02 / bd09ll |
output |
string | 否 | xml |
必须显式传 json |
road_name的匹配是按名称精确匹配。同一个城市有多条同名道路(如多个城市都有「人民路」,但city已限定)不会混淆;但同一城市内的「东二环」和「东二环路」可能被当成两条。先拿不到结果时,先试几种常见写法。
2.4 返回参数
| 字段 | 类型 | 说明 |
|---|---|---|
status |
int | 0 成功 |
message |
string | 说明 |
description |
string | 语义化路况描述,可直接播报给用户 |
evaluation |
object | 整体评价 |
evaluation.status |
int | 拥堵等级,见下表 |
evaluation.status_desc |
string | 等级的文字描述 |
road_traffic |
array | 道路明细(同一名称可能对应多段道路) |
road_traffic[]:
| 字段 | 类型 | 说明 |
|---|---|---|
road_name |
string | 道路名称 |
road_type |
int | 道路类型标识 |
congestion_sections |
array | 拥堵路段列表。没有拥堵时不返回此字段 |
congestion_sections[]:
| 字段 | 类型 | 说明 |
|---|---|---|
section_desc |
string | 路段的语义化描述,如「南向北,北新桥地铁站附近严重拥堵」 |
status |
int | 该路段的拥堵等级 |
speed |
double | 平均通行速度(km/h) |
congestion_distance |
int | 拥堵距离(米) |
congestion_trend |
string | 较 10 分钟前的变化:持平 / 缓解 / 加重 |
拥堵等级 status 取值(整体与路段一致):
| 值 | 含义 |
|---|---|
0 |
未知路况 |
1 |
畅通 |
2 |
缓行 |
3 |
拥堵 |
4 |
严重拥堵 |
congestion_sections可能不返回。道路畅通时这个字段直接消失,不是空数组。代码里必须用.get("congestion_sections") or [],直接下标会KeyError。
2.5 返回示例
{
"status": 0,
"message": "成功",
"description": "北五环:自东向西较为畅通;自西向东畅通;东向西,佳全教会附近拥堵。",
"evaluation": {
"status": 1,
"status_desc": "自东向西较为畅通;自西向东畅通"
},
"road_traffic": [
{
"road_name": "北五环",
"road_type": 2,
"congestion_sections": [
{
"section_desc": "东向西,佳全教会附近",
"status": 3,
"speed": 13.45,
"congestion_distance": 780,
"congestion_trend": "加重"
}
]
}
]
}
2.6 服务状态码
| 返回码 | 含义 | 处理 |
|---|---|---|
| 0 | 成功 | — |
| 2 | 参数非法 | road_name 或 city 缺失 |
| 4 | 配额校验失败 | 当日额度用尽 |
| 5 | ak 非法 |
检查 KEY |
道路名匹配不上时通常返回 status: 0 但 road_traffic 为空,不是报错。这需要和「真的畅通」区分开——两者的 road_traffic 都可能是空,判据是 description 是否有内容。
2.7 用它的正确姿势
def road_congestion(road_name, city):
data = request("/traffic/v1/road", road_name=road_name, city=city, output="json")
if data.get("status") != 0:
raise ApiError(data)
traffic = data.get("road_traffic") or []
if not traffic:
# 区分「查不到这条路」与「确实畅通」
return {"found": False, "desc": data.get("description", "")}
level = (data.get("evaluation") or {}).get("status", 0)
sections = []
for road in traffic:
for sec in road.get("congestion_sections") or []:
sections.append({
"desc": sec.get("section_desc"),
"level": sec.get("status"),
"speed_kmh": sec.get("speed"),
"jam_m": sec.get("congestion_distance"),
"trend": sec.get("congestion_trend"),
})
return {
"found": True,
"level": level,
"level_desc": (data.get("evaluation") or {}).get("status_desc"),
"sections": sections,
}
判断「能不能走」不要只看整体 level:一条长环路可能整体是「畅通」,但其中某一段「严重拥堵」。要看 congestion_sections 里是否有 status >= 3 的段,且 congestion_distance 是否落在你的行程范围内。