路线规划

本分类包含 5 个接口:驾车、步行、骑行、公交四种出行方式的标准版,以及一套覆盖面相同、权限门槛更低的轻量版。

段落顺序与其他分类一致:接口说明 → API 服务地址 → 请求参数 → 返回参数 → 返回示例 → 服务状态码。 一次算多组起终点的 OD 矩阵,见 批量算路——那是另一个接口、另一份额度。

接口 路径 权限 额度
驾车路线规划 /direction/v2/driving 标准版,需高级权限 四种方式共用一份
步行路线规划 /direction/v2/walking 标准版,需高级权限 同上
骑行路线规划 /direction/v2/riding 标准版,需高级权限 同上
公交路线规划 /direction/v2/transit 标准版,需高级权限 同上
路线规划(轻量版) /directionlite/v1/{driving,walking,riding,transit} 基础权限即可 四种方式各一份

一个 KEY 就能调全部路线接口,不区分服务端还是网页。网页调用与后端调用共用(见 接入前必读 · 1.4)。

标准版四种方式是连坐的:上游只按「路线规划」这一个服务计费,驾车、步行、骑行、公交共用同一份额度,随便哪一种用满了,其余三种也会一起被拦。控制台把它们显示成「路线规划」一行。轻量版是四个独立服务,各算各的。


0. 先搞清标准版与轻量版

这是本分类唯一的选型问题,但选错了会直接 403。

维度 标准版 /direction/v2/* 轻量版 /directionlite/v1/*
权限 需高级权限,未开通返回 403 category_disabled 基础权限即可
额度 各出行方式一份 各出行方式一份,与标准版不共享
详细步骤 支持 steps 完整分段 支持,但字段较少
途经点 最多 18 个 不支持
车牌规避限行 支持 不支持
未来出行规划 支持 不支持
可用位置 后端 + 网页 后端(网页里不走这条路)

选型规则:

一个反直觉的点:轻量版不是「标准版的降级替换」,而是另一套独立配额。同一份业务代码里混用两者不算作弊,是推荐的节流手段。


1. 驾车路线规划

1.1 接口说明

根据起终点坐标检索驾车路线方案,支持一次返回多条备选路线。

能力范围:

  1. 一次请求返回多条路线(备选方案)
  2. 支持 18 个以内的有序途经点
  3. 支持传入车牌号规避限行路段
  4. 支持传入车头方向,辅助判断起点所在正逆向车道
  5. 支持未来出行规划(指定未来 7 天内任意出发时刻,按预测路况规划)

适用:导航、里程与耗时估算、配送时效计算、可达性分析。 不适用:需要实时避堵的高频重算(用轻量版更省额度)、货车路线(走货车专用接口)。

1.2 API 服务地址

GET https://itruelink.top/direction/v2/driving?origin={lat,lng}&destination={lat,lng}&output=json&ak={KEY}

1.3 请求参数

参数 类型 必填 默认值 说明
origin string 是 — 起点,纬度,经度,小数不超过 6 位,如 31.289388,121.508230
destination string 是 — 终点,纬度,经度
origin_uid string 否 — 起点 POI 的 uid,与 origin 二选一,精度更高
destination_uid string 否 — 终点 POI 的 uid
waypoints string 否 — 有序途经点,纬度,经度 用 \| 分隔,最多 18 个
tactics int 否 0 路线策略,见下表
alternatives int 否 0 是否返回备选路线:0 只返回一条,1 返回多条
coord_type string 否 bd09ll 输入坐标系:bd09ll / bd09mc / gcj02 / wgs84
ret_coordtype string 否 bd09ll 返回坐标系,可设 gcj02
plate_number string 否 — 车牌号,用于规避限行。如 京A12345
cartype int 否 0 车辆类型:0 普通汽车、1 纯电动车(影响限行规则)
steps_info int 否 1 0 不返回详细分段,响应体积会小一个数量级
departure_time int 否 — 未来出行出发时刻(Unix 秒),高级付费能力
gps_direction int 否 — 车头方向 0–359,辅助判断正逆向车道
radius int 否 — 起终点吸附半径(米)
speed int 否 — 自定义平均车速(km/h)
output string 否 xml 必须显式传 json

tactics 取值:

值 含义
0 默认(综合最优)
2 距离最短
3 不走高速
4 高速优先
5 躲避拥堵
6 少收费
7–11 组合策略(如 7 不走高速+躲避拥堵)
12 距离优先
13 时间优先

2(距离最短)是纯几何距离最短,可能穿小巷、走村道,实际耗时反而更长。业务上要「快」就用 13,要「省」用 6,不要凭直觉选 2。

steps_info=0 是最省流量的开关。 一次驾车算路带完整 steps 的响应通常 30–80KB,关掉后只有 1–2KB。如果只取 distance / duration,务必关掉——在批量场景下这是最大的性能差别。

1.4 返回参数

顶层:

字段 类型 说明
status int 0 成功
message string 说明
type int 固定返回 2,无需关注
result object 结果

result:

字段 类型 说明
restriction string 限行提示,无限行为空
total int 方案总数
routes array 方案列表。吸附后的起终点在 routes[] 里,不在 result 上
holiday string 节假日提示,无则为空

routes[]:

字段 类型 说明
origin object 实际吸附的起点 {lng, lat}
destination object 实际吸附的终点 {lng, lat}
distance int 方案总距离(米,数字)
duration int 预计耗时(秒,数字)
traffic_light int 红绿灯数量
tag string 方案标签,如「时间少」「距离短」
taxi_fee int 出租车费用(元,估算)
toll int 过路费(元,估算)
toll_distance int 收费路段里程(米)
restriction_info object 限行信息,含 status 与 desc
steps array 分段详情,steps_info=0 时为空

restriction_info.status 取值:0 无限行、1 已规避限行(路线合法)、2 无法规避限行(路线非法)、3 疫情相关提示。

steps[]:

字段 类型 说明
leg_index int 所属途经点路段序号,从 0 开始
direction int 进入道路的角度,0–11,每 30 度一档,与正北顺时针夹角
distance int 本段距离(米)
road_name string 道路名称,未命名返回「无名路」
road_type int 道路类型,见下表
start_location / end_location object 本段起终点 {lng, lat}
path string 本段坐标串,lng,lat;lng,lat;...
adcodes string 途经城市编码,多城市逗号分隔
traffic_condition array 路况分段,[{status, geo_cnt, distance}]

road_type 取值:0 高速路、1 城市高速路、2 国道、3 省道、4 县道、5 乡镇村道、6 其他道路、7 九级路、8 航线(轮渡)、9 行人道路。

traffic_condition[].status:0 无路况、1 畅通、2 缓行、3 拥堵、4 非常拥堵。

distance 和 duration 是数字类型。这一点在做加减法时很关键——如果是字符串,"5200" + 100 在 JS 里会变成 "5200100",在 Python 里直接抛 TypeError。

1.5 返回示例

{
  "status": 0,
  "message": "成功",
  "type": 2,
  "result": {
    "restriction": "",
    "total": 1,
    "routes": [
      {
        "origin": { "lng": 121.508135, "lat": 31.289332 },
        "destination": { "lng": 121.484598, "lat": 31.234286 },
        "distance": 10882,
        "duration": 1456,
        "traffic_light": 8,
        "tag": "拥堵少",
        "taxi_fee": 36,
        "toll": 0,
        "toll_distance": 0,
        "restriction_info": { "status": 0, "desc": "" },
        "steps": [
          {
            "leg_index": 0,
            "direction": 10,
            "distance": 820,
            "road_name": "无名路",
            "road_type": 6,
            "start_location": { "lng": 121.508135, "lat": 31.289332 },
            "end_location": { "lng": 121.512, "lat": 31.283716 },
            "path": "121.508135,31.289332;121.512,31.283716",
            "adcodes": "310000",
            "traffic_condition": [
              { "status": 0, "geo_cnt": 29, "distance": 820.49 }
            ]
          }
        ]
      }
    ]
  }
}

1.6 服务状态码

返回码 含义 处理
0 成功 —
1 服务内部错误 重试
2 参数非法 检查坐标格式(纬度,经度)、参数拼写
4 配额校验失败 当日额度用尽
5 ak 非法 检查 KEY
7 无返回结果 起终点在不可达区域(如海岛、境外)。属正常业务分支
302 天配额超限 重试无用
401 并发超限 降并发后重试

7 要单独处理,不要计入错误率:

def drive(origin, destination):
    data = request_route(origin, destination)
    if data.get("status") == 7:
        return None                                   # 不可达,属正常业务分支
    if data.get("status") != 0:
        raise ApiError(data)
    route = data["result"]["routes"][0]
    return {"distance_m": route["distance"], "duration_s": route["duration"]}

2. 步行路线规划

2.1 接口说明

检索两点之间的步行路线。不支持途经点、限行规避、车牌等驾车专属能力。

适用:步行导航、步行可达性分析(如「地铁站步行 15 分钟内的小区」)。

2.2 API 服务地址

GET https://itruelink.top/direction/v2/walking?origin={lat,lng}&destination={lat,lng}&output=json&ak={KEY}

2.3 请求参数

参数 类型 必填 说明
origin string 是 起点,纬度,经度
destination string 是 终点,纬度,经度
coord_type / ret_coordtype string 否 坐标系声明,同驾车
steps_info int 否 0 不返回分段
output string 否 必须显式传 json

步行没有 tactics、waypoints、plate_number 这些参数——传了也是被忽略,不会报错。

2.4 返回参数

结构与驾车一致:status / message / result{routes[]},routes[] 里含 origin、destination、distance、duration、steps[]。

步行的 steps[].road_type 基本只有 9(行人道路)和 6(其他道路),不必按驾车的道路类型去解释。

2.5 返回示例

{
  "status": 0,
  "message": "成功",
  "result": {
    "origin": { "lng": 121.508230, "lat": 31.289388 },
    "destination": { "lng": 121.498, "lat": 31.2795 },
    "total": 1,
    "routes": [
      {
        "distance": 1860,
        "duration": 1320,
        "steps": [
          {
            "direction": 3,
            "distance": 420,
            "road_name": "四平路",
            "road_type": 9,
            "start_location": { "lng": 121.508230, "lat": 31.289388 },
            "end_location": { "lng": 121.5115, "lat": 31.283 },
            "path": "121.508230,31.289388;121.5115,31.283"
          }
        ]
      }
    ]
  }
}

2.6 服务状态码

同 1.6 节。步行的 7(无结果)更常见——起终点直线距离过远时不会给步行方案。


3. 骑行路线规划

3.1 接口说明

检索两点之间的骑行路线,支持普通自行车与电动自行车。

适用:骑行导航、外卖/即时配送的骑行时效估算。

3.2 API 服务地址

GET https://itruelink.top/direction/v2/riding?origin={lat,lng}&destination={lat,lng}&output=json&ak={KEY}

3.3 请求参数

参数 类型 必填 说明
origin string 是 起点,纬度,经度
destination string 是 终点,纬度,经度
coord_type / ret_coordtype string 否 坐标系声明
steps_info int 否 0 不返回分段
output string 否 必须显式传 json

3.4 返回参数

同驾车结构。骑行方案通常比驾车短、耗时更长(受骑行速度限制),这是正常的。

3.5 返回示例

{
  "status": 0,
  "message": "成功",
  "result": {
    "origin": { "lng": 121.508230, "lat": 31.289388 },
    "destination": { "lng": 121.49, "lat": 31.262 },
    "total": 1,
    "routes": [
      {
        "distance": 4120,
        "duration": 1030,
        "steps": []
      }
    ]
  }
}

3.6 服务状态码

同 1.6 节。


4. 公交路线规划

4.1 接口说明

检索公共交通方案,支持同城与跨城。交通工具覆盖公交、地铁、火车、飞机、大巴,并支持出行策略(少换乘、少步行、地铁优先等)。

适用:公共交通出行规划、通勤时间分析。 不适用:需要精确到分钟的实时到站预测(本接口给的是计划耗时,非实时)。

4.2 API 服务地址

GET https://itruelink.top/direction/v2/transit?origin={lat,lng}&destination={lat,lng}&output=json&ak={KEY}

4.3 请求参数

参数 类型 必填 默认值 说明
origin string 是 — 起点,纬度,经度
destination string 是 — 终点,纬度,经度
transit_policy int 否 0 出行策略,见下表
departure_time int 否 — 出发时刻(Unix 秒),不传按当前时间
page_size int 否 10 每页方案数,最大 10
page_num int 否 0 页码,从 0 开始
coord_type / ret_coordtype string 否 — 坐标系声明
output string 否 xml 必须显式传 json

transit_policy 取值: 0 少换乘、1 少步行、2 不坐地铁、3 时间短、4 地铁优先。

策略之间会互相覆盖,只能传一个。想要「地铁优先且时间短」是做不到的,需要自己排序。

4.4 返回参数

顶层结构同其他出行方式,但 routes[] 的构成不同——公交方案是多段的,多一层 legs:

字段 类型 说明
distance int 全程距离(米)
duration int 预计耗时(秒)
price int 票价(元)
arrive_time string 预计到达时间
steps array 换乘段列表

steps[] 每个元素代表一段「步行 → 乘车」的组合,其中:

字段 说明
instruction 该段的文字指引
distance / duration 该段距离与耗时
legs 该段内的具体行程,一条 legs 对应一次乘车或一次步行

legs[] 内常见字段:leg_type(1 公交/地铁,2 步行,3 火车/飞机等城际)、line_name(线路名)、start_name / end_name(上车站 / 下车站)、start_time / end_time(首末班)、stop_num(经过站数)、distance / duration。

不要把公交的 steps 当成驾车的 steps 用。驾车 steps[] 是平铺的连续路段,公交 steps[] 是「换乘段」容器,里面还有 legs[]。复制驾车解析代码到公交上,拿到的是空数组或 undefined。

4.5 返回示例

{
  "status": 0,
  "message": "成功",
  "result": {
    "origin": { "lng": 121.508230, "lat": 31.289388 },
    "destination": { "lng": 121.4846, "lat": 31.2343 },
    "routes": [
      {
        "distance": 8100,
        "duration": 1860,
        "price": 4,
        "steps": [
          {
            "instruction": "步行 320 米至地铁站",
            "distance": 320,
            "duration": 260,
            "legs": [
              {
                "leg_type": 2,
                "distance": 320,
                "duration": 260
              }
            ]
          },
          {
            "instruction": "乘坐地铁 10 号线(航中路方向),6 站",
            "distance": 12800,
            "duration": 1320,
            "legs": [
              {
                "leg_type": 1,
                "line_name": "地铁10号线",
                "start_name": "同济大学",
                "end_name": "南京东路",
                "stop_num": 6,
                "distance": 7500,
                "duration": 1060
              }
            ]
          }
        ]
      }
    ]
  }
}

4.6 服务状态码

同 1.6 节。公交的 7(无结果)在跨城场景下较常见——超出铁路覆盖范围时无方案。


5. 路线规划(轻量版)

5.1 接口说明

标准版路线规划的精简接口,基础权限即可调用,配额独立。四种出行方式各有独立路径:

出行方式 路径
驾车 /directionlite/v1/driving
步行 /directionlite/v1/walking
骑行 /directionlite/v1/riding
公交 /directionlite/v1/transit

能力取舍:不返回导航级细节、不支持途经点、车牌规避、未来出行。只解决「多远、多久」。

这是批量场景的默认选择。如果你一次任务要算一万条 OD,用标准版会很快打满额度,用轻量版通常够用且便宜。

5.2 API 服务地址

GET https://itruelink.top/directionlite/v1/driving?origin={lat,lng}&destination={lat,lng}&output=json&ak={KEY}

其余三种出行方式把 driving 换成 walking / riding / transit 即可。

5.3 请求参数

参数 类型 必填 说明
origin string 是 起点,纬度,经度
destination string 是 终点,纬度,经度
coord_type / ret_coordtype string 否 坐标系声明
tactics int 否 驾车策略,取值同标准版(部分值可能不生效)
output string 否 必须显式传 json

5.4 返回参数

结构基本同标准版:status / message / result{routes[]},routes[] 含 distance、duration。区别是 steps 通常为空或字段极少。

只取 distance 和 duration 时,轻量版与标准版的结果一致,这是它能替代标准版做批量计算的基础。

5.5 返回示例

{
  "status": 0,
  "message": "ok",
  "result": {
    "routes": [
      {
        "distance": 17585,
        "duration": 2052,
        "steps": []
      }
    ]
  }
}

5.6 服务状态码

返回码 含义 处理
0 成功 —
2 参数非法 检查坐标格式
4 配额校验失败 当日额度用尽(这是轻量版自己的额度,与标准版无关)
5 ak 非法 检查 KEY
7 无返回结果 不可达

排查额度问题时的关键点:轻量版和标准版是两份额度。看到「标准版还有余量但报 4」不矛盾,是轻量版自己的额度满了。



附:本分类的四个易错点

易错点 表现 正确做法
标准版/轻量版额度混算 「明明还有额度却报 4」 两套独立额度,分别核对
steps_info 没关 批处理极慢、响应几十 KB 只要距离耗时就传 steps_info=0
公交 steps 当驾车用 解析出空数组 公交多一层 legs[],结构不同
拿单条接口跑批量 额度扛不住、耗时成倍 多组起终点用批量算路
本页对应原始 Markdown 版本: /docs/route.md