百度地图开放平台
更新时间: 2026/09/10 16:28
骑行路线规划
简介

riding 用于规划两点之间的骑行路线,返回总距离、预计耗时、折线坐标与分步指引。
对应 Web 服务接口 direction/v2/riding。返回的 routes 是 SDK 规范化后的方案数组,wxPolylineData 是主方案(routes[0])的折线坐标。

请求参数
参数类型必填默认值说明

origin

String

起点,"纬度,经度"或地点名称

destination

String

终点,"纬度,经度"或地点名称

tactics

Number

0

规划策略,取值见下表

ret_coordtype

String

gcj02

返回坐标类型,支持 gcj02 / bd09ll / wgs84

coord_type

String

gcj02

输入坐标类型,默认与小程序坐标系一致

output

String

json

返回格式

city

String

起终点所在城市,起终点为地点名称时建议提供

plate

String

车牌号,电动车场景使用

width

Number

返回路线的宽度,像素

success

Function

规划成功回调

fail

Function

规划失败回调

tactics 常用取值:

取值含义

0

常规(默认)

1

避开高速

其余参数与骑行路线规划请求参数一致,会原样透传。

返回值

success 回调入参:

字段类型说明

routes

Array

规范化的方案数组,元素结构见下表;百度返回多套方案时会有多个元素

wxPolylineData

Array

主方案折线坐标,元素为 { latitude, longitude }(gcj02)

originalData

Object

接口返回的完整原始数据

routes 数组元素字段:

字段类型说明

distance

Number

方案总距离,单位米

duration

Number

方案总耗时,单位秒

prefer

String

方案偏好描述,接口返回时存在

polyline

Array

本方案折线坐标,元素为 { latitude, longitude }(gcj02)

steps

Array

分步指引原始结构,含 path / road_name / instruction 等,字段随接口浮动

失败时走 fail 回调,入参统一为 { errMsg, message, statusCode, rawMessage },字段含义见"服务介绍 → 统一的回调约定"。

示例代码

规划两点之间的路线,把主方案折线绘制到地图上并展示距离与耗时:

// pages/index/index.js
// 引用百度地图微信小程序 JSAPI 模块
const { BMapWX } = require('../../libs/bmap-wx.min.js');
const bmap = new BMapWX({ ak: '您的ak' });
Page({
data: {
latitude: 39.912,
longitude: 116.400,
polyline: [],
summary: '',
error: '',
},
onLoad() {
bmap.riding({
origin: '39.908823,116.397470', // "纬度,经度",也可传地点名称
destination: '39.915119,116.403963',
success: (res) => {
if (!res.routes.length) {
this.setData({ error: '未规划出路线方案,请检查起终点' });
return;
}
const best = res.routes[0];
this.setData({
// <map polyline> 需要 [{ points, ... }] 结构,不能直接赋 wxPolylineData
polyline: [{ points: res.wxPolylineData, color: '#4db900', width: 6, arrowLine: true }],
summary: (best.distance / 1000).toFixed(1) + ' 公里 · 约 '
+ Math.max(1, Math.round(best.duration / 60)) + ' 分钟',
latitude: res.wxPolylineData[0].latitude,
longitude: res.wxPolylineData[0].longitude,
error: '',
});
},
// 缺少起终点时 SDK 本地拦截,此时只有 errMsg
fail: err => this.setData({ error: '路线规划失败:' + (err.message || err.errMsg) }),
});
},
});
<!-- pages/index/index.wxml -->
<map
style="width: 100%; height: 300px;"
longitude="{{longitude}}"
latitude="{{latitude}}"
scale="13"
polyline="{{polyline}}"
/>
<view wx:if="{{summary}}">{{summary}}</view>
<view wx:if="{{error}}">{{error}}</view>
Demo 效果与体验

下图为官方 Demo"路线规划"的运行界面,扫描下方小程序码即可直接体验。Demo 是产品化示例,界面比上面的示例代码精致,但调用方式与返回数据完全一致。

进阶:展示多套备选方案

百度通常会返回多套方案,wxPolylineData 只是其中第一套。要让用户在方案间切换,把 routes 全部存下来按索引取用:

// pages/index/index.js
Page({
data: {
routes: [], // 方案摘要,用于列表渲染
activeIndex: 0,
polyline: [],
},
onLoad() {
bmap.driving({ // 换成 walking / transit / riding 即切换交通方式
origin: '39.908823,116.397470',
destination: '39.915119,116.403963',
success: (res) => {
this._routes = res.routes; // 全部方案存到实例上,不进 data
this.setData({
routes: res.routes.map(r => ({
distanceText: (r.distance / 1000).toFixed(1) + ' 公里',
durationText: '约 ' + Math.max(1, Math.round(r.duration / 60)) + ' 分钟',
prefer: r.prefer || '',
})),
});
this.showRoute(0);
},
});
},
showRoute(index) {
const points = this._routes[index].polyline;
this.setData({
activeIndex: index,
polyline: [{ points, color: '#4db900', width: 6, arrowLine: true }],
latitude: points[0].latitude,
longitude: points[0].longitude,
});
},
onRouteTap(e) {
this.showRoute(e.currentTarget.dataset.index);
},
});
注意事项
  • 骑行不支持途经点。

  • 起终点均为必填,且建议用坐标形式("纬度,经度")。缺少任一项时 SDK 本地拦截,fail 入参只有 errMsg;地点名称的解析依赖城市信息,可先用 suggestion 或 geocoding 把用户输入转成坐标再规划。

  • 绑定折线时 <map> 的 polyline 需要 [{ points, color, width }] 结构,直接赋 wxPolylineData 不会显示;steps 的字段由官方接口决定,SDK 原样透传,取值前需判空。

上一篇

公交路线规划

下一篇

静态图
本篇文章对您是否有帮助?