TransitRoutePlan
此类表示一条公交出行方案。没有构造函数,通过TransitRouteResult.getPlan()方法获得。
方法
| 方法 | 返回值 | 描述 |
|---|---|---|
getNumLines() | Number | 返回方案包含的公交线路段数(如果是跨城检索,还包括飞机、火车、大巴线路) |
getLine(i: Number) | Line | 返回方案包含的某条公交线路(如果是跨城检索,还包括飞机、火车、大巴线路) |
getNumRoutes() | Number | 返回方案包含的步行线路段数 |
getRoute(i: Number) | Route | 返回方案包含的某条步行线路 |
getDistance(format: Boolean) | String | Number | 返回方案总距离。当format参数为true时,返回方案距离字符串(包含单位),当format为false时,仅返回数值(单位为米)信息。默认参数为true |
getDuration(format: Boolean) | String | Number | 返回方案总时间。当format参数为true时,返回描述时间的字符串(包含单位),当format为false时,仅返回数值(单位为秒)信息。默认参数为true |
getDescription(includeHtml: Boolean) | String | 返回方案描述文本,默认包含HTML标签。当includeHtml为false时,方案描述不包含HTML标签 |
getTotalType(i: Number) | TransitPlanType | 返回指定路段的交通方式类型,分别对应Line和Route |
getTotal(i: Number) | Route | Line | 返回整个方案包含的某段线路,根据方案的数据情况,返回值可能是步行对象Route也有可能是线路对象Line |
getNumTotal() | Number | 总路段数量 |
示例
ts
// 创建公交导航实例
const transitRoute = new BMapGL.TransitRoute(map);
// 监听搜索结果
transitRoute.onSearchComplete = (results: BMapGL.TransitRouteResult) => {
// 获取第一条方案
const plan = results.getPlan(0);
// 获取方案基本信息
console.log('方案描述:', plan.getDescription());
console.log('总距离:', plan.getDistance());
console.log('总时间:', plan.getDuration());
// 获取公交线路信息
const numLines = plan.getNumLines();
console.log('公交线路数量:', numLines);
for (let i = 0; i < numLines; i++) {
const line = plan.getLine(i);
console.log(`公交线路 ${i + 1}:`, line);
}
// 获取步行线路信息
const numRoutes = plan.getNumRoutes();
console.log('步行线路数量:', numRoutes);
for (let i = 0; i < numRoutes; i++) {
const route = plan.getRoute(i);
console.log(`步行线路 ${i + 1}:`, route);
}
// 获取所有路段信息
const numTotal = plan.getNumTotal();
console.log('总路段数量:', numTotal);
for (let i = 0; i < numTotal; i++) {
const type = plan.getTotalType(i);
const total = plan.getTotal(i);
console.log(`路段 ${i + 1} 类型:`, type);
console.log(`路段 ${i + 1} 详情:`, total);
}
};