跳到主要内容基于 Java 的百度地图路线规划服务开发指南 | 极客日志Javajava
基于 Java 的百度地图路线规划服务开发指南
综述由AI生成基于 Java 语言集成百度地图开放平台的驾车路线规划服务。文章详细解析了 API 的请求与响应参数结构,演示了使用 UniHttp 进行接口调用的方法,并提供了将 JSON 响应转换为 JavaBean 的代码示例。同时总结了坐标顺序及格式处理等常见问题的解决方案,帮助开发者构建稳定的路径规划功能模块。
Eee_1235 浏览 一、驾车路线服务简介
本节将对百度地图的驾车路线服务进行简单的介绍。这不仅是我们开发的重要文档,也是我们后续进行问题查找的重要资料。这里将详细的介绍百度地图提供哪些规划服务、驾车路线服务包括哪些以及驾车路线服务的请求参数和响应参数信息。
1、百度路线规划服务
路线规划服务(又名 Direction API)是一套 REST 风格的 Web 服务 API,以 HTTP/HTTPS 形式提供了路线规划服务。

从上图可以看到,百度地图的路线规划服务视图是非常丰富的,不仅包含驾车的方式,还包含骑行、步行和公交。大家可以根据自己的业务需要来调用相应的服务即可。
2、驾车路线服务
驾车服务作为路线服务中非常重要的一环。如今随着城市交通建设的大力发展,许多城市的道路建设几乎是日新月异,如果没有路线导航服务,出行一定会受到极大的限制。
百度驾车路线服务:
根据起终点坐标检索符合条件的驾车路线规划方案,支持以下功能:
- 支持一次请求返回多条路线(备用路线)
- 支持 18 个以内的途径点
- 支持传入车牌规避限行路段
- 支持传入起点车头方向,辅助判断起点所在正逆向车道,辅助更准确算路
- 支持未来出行规划,指定未来 7 天任意出发时刻,将依据智能预测路况和道路限行规划合理路线
驾车路线服务的访问地址如下:
https://api.map.baidu.com/direction/v2/driving?origin=40.01116,116.339303&destination=39.936404,116.452562&ak=您的 AK
3、请求参数
| 字段名称 | 含义 | 字段类型 | 必填 | 备注 |
|---|
| ak | 用户的访问权限 | string | 必填 | |
| origin | 起点经纬度,小数点后不超过 6 位 | string | 必填 | 起点经纬度,格式为:纬度,经度;小数点后不超过 6 位 |
| destination | 终点经纬度,小数点后不超过 6 位 | string | 必填 | 终点经纬度,格式为:纬度,经度;小数点后不超过 6 位 |
| origin_uid | POI 的 uid | string | 选填 | |
| destination_uid | POI 的 uid | string | 选填 | |
| waypoints | 途径点坐标串,支持 18 个以内的有序途径点 | string | 选填 | |
| coord_type | 坐标类型,可选参数,默认为 bd09ll | string | 选填 | 默认 bd09ll |
|
| tactics | 策略选择 | int | 选填 | 默认 0:默认策略 |
| alternatives | 是否返回备选路线 | int | 选填 | 默认 0 |
| plate_number | 车牌号 | string | 选填 | 不填则不做规避 |
| departure_time | 设置出发时间(支持未来 7 天),UNIX 时间戳 | string | 选填 | 默认为当前时间 |
| ext_departure_time | 更多出发时间,UNIX 时间戳 | string | 选填 | 不填则不返回 ext_duration 字段 |
| expect_arrival_time | 预期的到达时间,UNIX 时间戳 | string | 选填 | 不填则不返回 suggest_departure_time 字段 |
| gps_direction | 起点的车头方向 | int | 选填 | |
| output | 表示输出类型,可设置为 xml 或 json | string | 选填 | 默认为 json |
| timestamp | 时间戳,与 SN 配合使用 | int64 | SN 存在时必填 | |
| intelligent_plan | 智能调整途经点顺序 | int | 选填 | 默认 0 |
| steps_info | 是否下发 step 详情 | int | 选填 | |
| origin_bind_stategy | 起点绑路策略 | int | 选填 | 默认值 0 |
| dest_bind_stategy | 终点绑路策略 | int | 选填 | 默认值 0 |
| origin_road_type | 起点道路类型 | int | 选填 | 默认值 0 |
4、响应参数
| 字段名称 | 字段含义 | 字段类型 | 备注 |
|---|
| status | 状态码 | int | 0:成功 |
| message | 状态码对应的信息 | string | |
| type | 默认返回 2 | int | |
| result | 返回的结果 | | |
| restriction | 限行结果提示信息 | string | |
| total | 返回方案的总数 | int | |
| routes | 返回的方案集 | | |
| restriction_info | 限行状态 | | |
| origin | 起点位置 | | |
| destination | 终点位置 | | |
| tag | 方案标签 | string | |
| traffic_light | 红绿灯数量 | int | |
| route_id | 路线 ID | string | |
| distance | 方案距离,单位:米 | int | |
| duration | 未来驾车路线耗时,单位:秒 | int | |
| ext_duration | 驾车路线历史耗时(扩展),单位:秒 | int | |
| suggest_departure_time | 建议出发时间,单位:秒 | int64 | |
| taxi_fee | 出租车费用,单位:元 | int | |
| toll | 此路线道路收费,单位:元 | int | |
| toll_distance | 收费路段里程,单位:米 | int | |
| steps | 路线分段 | array | |
| end_walkinfo | 终点步导路线 | array | |
| start_walkinfo | 起点步导路线 | array | |
二、UniHttp 接口访问集成
这里我们使用 UniHttp 来简化 Http 服务调用。这里仅详细说明如何进行访问接口的创建和本地接口的创建和集成。本节是实际的进行接口调用,并且返回相应的结果实战。
1、访问接口创建
package com.yelang.project.thridinterface;
import com.burukeyou.uniapi.http.annotation.param.QueryPar;
import com.burukeyou.uniapi.http.annotation.request.GetHttpInterface;
import com.burukeyou.uniapi.http.core.response.HttpResponse;
import com.yelang.project.thridinterface.apiprocessor.BaiduHttpApi;
@BaiduHttpApi(snMode = false)
public interface BaiduDrivingV2Service {
@GetHttpInterface(path = "/direction/v2/driving")
public HttpResponse<String> getSearch(@QueryPar("ak") String ak, @QueryPar("origin") String origin, @QueryPar("destination") String destination, @QueryPar("coord_type") String coordType, @QueryPar("ret_coordtype") String retCoordtype, @QueryPar("tactics") int tactics, @QueryPar("alternatives") int alternatives, @QueryPar("cartype") int cartype, @QueryPar("speed") float speed, @QueryPar("steps_info") int stepsInfo );
}
以上参数是根据业务的需求进行定义的,除必填的参数以外,很多选填的参数是可以不定义,直接使用默认值即可。
2、本地调用测试
下面我们以从广东省广州市为起点,驾车开往贵州省铜仁市为例,车速选择匀速 100 公里,车型为普通油车、需要返回备选路线、并且返回下发详情的实例进行接口请求。这里我们使用 Junit 的方式进行集成,核心代码如下:
package com.yelang.project.unihttp;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.burukeyou.uniapi.http.core.response.HttpResponse;
import com.yelang.common.utils.StringUtils;
import com.yelang.project.thridinterface.BaiduDrivingV2Service;
@SpringBootTest
@RunWith(SpringRunner.class)
public class BaiduDrivingV2ServiceCase {
private static final String BAIDU_CLIENT_AK = "yourak";
@Autowired
private BaiduDrivingV2Service drivingService;
@Test
public void testDriving() {
String origin = "23.115102,113.269043";
String destination = "27.728514,109.186249";
String coordType = "wgs84";
String retCoordtype = "bd09ll";
int tactics = 0;
int alternatives = 1;
int cartype = 0;
float speed = 27.78F;
int stepsInfo = 1;
HttpResponse<String> result = drivingService.getSearch(BAIDU_CLIENT_AK, origin, destination, coordType, retCoordtype, tactics, alternatives, cartype, speed, stepsInfo);
System.out.println(result.getBodyResult());
}
}
3、响应对象初探
在本地 IDE 中运行以上程序后,可以看到以下输出:
在文本编辑器中对返回数据进行 json 格式化后,可以看到如下结构:
这个结构很重要,这个结构是我们下一步进行 Java 对象封装的重要参考资料。
三、响应对象的 Java 封装
通过上一节,我们已经成功的获取到了百度地图开放平台返回的路径规划结果,数据格式是 json,在实际业务中,我们需要使用 Java 来进行数据的持久化或者数据挖掘,因此需要将 json 反序列化成 JavaBean。这里分别介绍响应类图、以及相应的实现,最后基于 GSON 来进行对象转换说明。
1、驾车路线服务响应类图
根据前面的响应参数介绍以及在上一小节的真实响应对象的研究,我们大致对返回对象有了一个初印象。整理成如下类图:
根据业务的需要,为了完整的业务数据还原,这里设计 11 个类来对百度地图的路线规划服务接口返回数据进行接收。
2、响应对象实现
a、公共属性类
公共对象是所有百度公共接口的父类,用来定义响应数据的公共属性,定义如下:
package com.yelang.project.meteorology.domain;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@ToString
public class BdCommonDTO implements Serializable {
private static final long serialVersionUID = 4985268844473756688L;
private int status;
private String message;
}
b、驾车路线数据传输类
驾车路线数据传输类封装了规划的路线信息,核心代码如下:
package com.yelang.project.meteorology.domain.baidudto;
import java.io.Serializable;
import com.yelang.project.meteorology.domain.BdCommonDTO;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@ToString(callSuper=true)
@EqualsAndHashCode(callSuper=true)
public class BdDrivingDTO extends BdCommonDTO implements Serializable{
private static final long serialVersionUID = 5055488530865458258L;
private Integer type;
private BdDrivingResultDTO result;
}
c、导航信息类
导航信息类包含了分段信息、方案总数、具体的方案路线信息,核心代码如下:
package com.yelang.project.meteorology.domain.baidudto;
import java.io.Serializable;
import java.util.List;
import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@ToString
public class BdDrivingResultDTO implements Serializable{
private static final long serialVersionUID = 6407644142297216118L;
private String restriction;
private int total;
@SerializedName("navi_restype")
private String naviRestype;
@SerializedName("session_id")
private String sessionId;
private List<BdDrivingRouteDTO> routes;
private String holiday;
@SerializedName("ret_coordtype")
private String retCoordtype;
@SerializedName("api_restype")
private String apiRestype;
}
d、具体路线分段信息类
具体路线分段信息类,封装了路线详情、距离、打车费用预估、预计时间和具体的转折路线列表,核心代码如下:
package com.yelang.project.meteorology.domain.baidudto;
import java.io.Serializable;
import java.util.List;
import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@ToString
public class BdDrivingRouteDTO implements Serializable{
private static final long serialVersionUID = -2475019111446934340L;
private BdLocationDTO origin;
private BdLocationDTO destination;
private String tag;
private String mrsl;
@SerializedName("route_md5")
private String routeMd5;
@SerializedName("data_version")
private String dataVersion;
@SerializedName("traffic_light")
private int trafficLight;
private float distance;
private int duration;
@SerializedName("taxi_fee")
private float taxiFee;
@SerializedName("has_guidance")
private boolean hasGuidance;
private List<BdDrivingStepDTO> steps;
@SerializedName("restriction_info")
private BdDrivingRestrictionInfoDTO restrictionInfo;
@SerializedName("yellow_tip")
private List<BdDrivingYelloTipDTO> yellowTipList;
private int toll;
@SerializedName("toll_distance")
private float tollDistance;
private float score;
@SerializedName("route_id")
private String routeId;
}
篇幅有限,如果百度驾车路径规划完整响应类的代码,可以参考上述的结果先自己研究,如有问题,可以就具体的类进行交流。
3、使用 GSON 转换实例
最后我们将百度开放平台返回的 JSON 数据转换成 Java 对象,转换框架这里以 Gson 为例,其它的 json 框架也是可以的。转换代码如下:
Gson gson = new Gson();
if(StringUtils.isNotEmpty(result.getBodyResult())) {
BdDrivingDTO drivingDTO = gson.fromJson(result.getBodyResult(), BdDrivingDTO.class);
System.out.println(drivingDTO);
System.out.println("---------------------------");
System.out.println(gson.toJson(drivingDTO));
}
运行程序后,可以看到正常的数据输出。后续我们可以基于此和其他应用的结合。
四、常见问题
在百度驾车路线规划服务集成过程中,可能会遇到一些问题。比如我在集成过程中就遇到了关于坐标精度和坐标和格式的问题。导致这个问题的原因主要还是对官网文档不是很熟悉,实践出真知,纸上得来终觉浅,只有经过实战才知道可能会遇到什么问题。
1、坐标顺序问题
众所周知,在地理位置中,经纬度是描述一个点的具体格式。因此我们特别习惯在描述一个点时,使用(经度,纬度)的方式,如:113.269043,23.115102。但是这种方式在实际查询过程中会报以下错误:
解决的办法也比较简单,在百度的位置描述中,将纬度描述在前,即(纬度,经度)即可。
2、坐标格式问题
坐标类型永远是 GIS 应用中绕不开的话题,大家可能都以为通用的 WGS84,在接口的参数定义中,我们可以看到,查询参数的坐标是支持 WGS84 的,但是在返回的坐标中,却没有了这个参数支持。如果在返回参数中设置了 WGS84,大概率是会报错的,如下图所示:
相关免费在线工具
- Keycode 信息
查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online
- Escape 与 Native 编解码
JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online
- JavaScript / HTML 格式化
使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online
- JavaScript 压缩与混淆
Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online
- Base64 字符串编码/解码
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
- Base64 文件转换器
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online