在 ruoyi-admin/src/main/resources/application.yml 中配置:
# 百度地图配置
baidu:
map:
ak: YOUR_BAIDU_MAP_AK_HERE # 替换为您的百度地图AK
获取AK: https://lbsyun.baidu.com/ → 控制台 → 应用管理 → 创建应用
配置完成后重启 Spring Boot 应用即可。
接口: GET /system/gps/baidu/route/driving
参数:
- origin: 起点坐标 (纬度,经度)
- destination: 终点坐标 (纬度,经度)
示例: GET /system/gps/baidu/route/driving?origin=40.056878,116.30815&destination=31.222965,121.505821
前端调用:
```javascript
import { baiduRouteDriving } from '@/api/map'
baiduRouteDriving('40.056878,116.30815', '31.222965,121.505821')
.then(res => {
const result = JSON.parse(res.data)
const distanceKm = (result.result.routes[0].distance / 1000).toFixed(2)
console.log('距离:', distanceKm + ' 公里')
})
```
两步走:
步骤1: 地址转坐标
import { baiduGeocoding } from '@/api/map'
// 起点地址转坐标
baiduGeocoding('广州市天河区天河路', '广州市')
.then(res => {
const result = JSON.parse(res.data)
const lat = result.result.location.lat
const lng = result.result.location.lng
console.log('起点坐标:', lat + ',' + lng)
})
步骤2: 坐标计算距离 (同接口1)
或使用组合接口:
import { baiduDistanceByAddress, baiduRouteDriving } from '@/api/map'
async function calcDistance() {
// 第1步: 获取坐标
const geocode = await baiduDistanceByAddress(
'广州市天河区天河路', '广州市',
'广州市越秀区中山路', '广州市'
)
const fromGeo = JSON.parse(geocode.data.fromGeocoding)
const toGeo = JSON.parse(geocode.data.toGeocoding)
const origin = fromGeo.result.location.lat + ',' + fromGeo.result.location.lng
const dest = toGeo.result.location.lat + ',' + toGeo.result.location.lng
// 第2步: 计算距离
const route = await baiduRouteDriving(origin, dest)
const result = JSON.parse(route.data)
const distanceKm = (result.result.routes[0].distance / 1000).toFixed(2)
console.log('距离:', distanceKm + ' 公里')
return distanceKm
}
// 在任务表单组件中
data() {
return {
form: {
startAddress: '',
endAddress: '',
distance: ''
}
}
},
watch: {
// 监听起点和终点变化
'form.startAddress': 'autoCalculateDistance',
'form.endAddress': 'autoCalculateDistance'
},
methods: {
async autoCalculateDistance() {
if (!this.form.startAddress || !this.form.endAddress) {
return
}
try {
// 1. 起点地址转坐标
const fromRes = await baiduGeocoding(this.form.startAddress, '广州市')
const fromGeo = JSON.parse(fromRes.data)
const fromLat = fromGeo.result.location.lat
const fromLng = fromGeo.result.location.lng
// 2. 终点地址转坐标
const toRes = await baiduGeocoding(this.form.endAddress, '广州市')
const toGeo = JSON.parse(toRes.data)
const toLat = toGeo.result.location.lat
const toLng = toGeo.result.location.lng
// 3. 计算距离
const routeRes = await baiduRouteDriving(
fromLat + ',' + fromLng,
toLat + ',' + toLng
)
const route = JSON.parse(routeRes.data)
const distanceKm = (route.result.routes[0].distance / 1000).toFixed(2)
// 4. 自动填充距离字段
this.form.distance = distanceKm
this.$message.success('距离自动计算成功: ' + distanceKm + ' 公里')
} catch (error) {
console.error('距离计算失败:', error)
this.$message.warning('距离自动计算失败,请手动填写')
}
}
}
纬度,经度 (lat,lng)40.056878,116.30815distanceKm = distance / 1000返回状态码说明:
- status: 0 - 成功
- status: 1 - 服务器内部错误
- status: 2 - 参数非法
- status: 3 - 权限校验失败 (检查AK配置)
- status: 4 - 配额不足
见 prd/百度地图距离计算接口说明.md 获取详细文档。
ruoyi-common/src/main/java/com/ruoyi/common/config/BaiduMapConfig.javaruoyi-admin/src/main/java/com/ruoyi/web/controller/system/VehicleGpsController.javaapp/api/map.jsruoyi-admin/src/main/resources/application.ymlQ: 返回 status: 3 怎么办?
A: 检查 application.yml 中的 AK 是否正确配置。
Q: 计算的距离不准确?
A: 确认坐标系是 BD-09,如果是其他坐标系需要转换。
Q: 能支持步行、骑行距离吗?
A: 可以,修改接口路径为 /walking 或 /riding 即可。
开发完成时间: 2025-10-21
文档版本: v1.0