成功为急救转运系统后台实现了百度地图的距离计算功能,包括:
1. ✅ 计算两个坐标之间的驾车距离
2. ✅ 地址转坐标(地理编码)
3. ✅ 计算两个地址之间的距离(组合接口)
文件: ruoyi-common/src/main/java/com/ruoyi/common/config/BaiduMapConfig.java
@Configuration
@ConfigurationProperties(prefix = "baidu.map")
public class BaiduMapConfig {
private String ak; // 百度地图API Key
// getter/setter
}
文件: ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/VehicleGpsController.java
新增3个接口:
| 接口路径 | 方法 | 功能 | 参数 |
|---|---|---|---|
/system/gps/baidu/geocoding |
GET | 地址转坐标 | address, city |
/system/gps/baidu/route/driving |
GET | 计算坐标距离 | origin, destination |
/system/gps/baidu/distance/byAddress |
GET | 地址距离计算 | fromAddress, fromCity, toAddress, toCity |
关键代码:
@Autowired
private BaiduMapConfig baiduMapConfig;
@Anonymous()
@GetMapping("/baidu/route/driving")
public AjaxResult baiduRouteDriving(String origin, String destination) {
String url = "https://api.map.baidu.com/directionlite/v1/driving";
String params = "origin=" + origin +
"&destination=" + destination +
"&ak=" + baiduMapConfig.getAk();
String response = HttpUtils.sendGet(url, params);
return AjaxResult.success("计算成功", response);
}
文件: ruoyi-admin/src/main/resources/application.yml
# 百度地图配置
baidu:
map:
ak: YOUR_BAIDU_MAP_AK_HERE
文件: app/api/map.js
新增3个API调用方法:
// 1. 地理编码(地址转坐标)
export function baiduGeocoding(address, city)
// 2. 路线规划(坐标计算距离)
export function baiduRouteDriving(origin, destination)
// 3. 组合接口(地址距离计算)
export function baiduDistanceByAddress(fromAddress, fromCity, toAddress, toCity)
特点:
- ✅ 完整的参数验证
- ✅ 友好的错误提示
- ✅ Promise 封装,支持 async/await
| API | 用途 | 请求参数 | 返回数据 |
|---|---|---|---|
| Geocoding API | 地址转坐标 | address(地址), city(城市) | lng(经度), lat(纬度) |
| Direction API | 路线规划 | origin(起点坐标), destination(终点坐标) | distance(距离/米), duration(时长/秒) |
| 方式 | 输入 | 步骤 | 适用场景 |
|---|---|---|---|
| 坐标计算 | 经纬度坐标 | 1步(直接计算) | 已知精确坐标 |
| 地址计算 | 文字地址 | 2步(地址转坐标→计算距离) | 用户输入地址 |
百度地图接口: https://api.map.baidu.com/directionlite/v1/driving
主要参数:
- origin: 起点坐标 (格式: 纬度,经度)
- destination: 终点坐标 (格式: 纬度,经度)
- ak: 百度地图API密钥
返回结果:json { "status": 0, "result": { "routes": [{ "distance": 1213000, // 距离(米) "duration": 43920 // 时长(秒) }] } }
百度地图接口: https://api.map.baidu.com/geocoding/v3/
主要参数:
- address: 待解析的地址
- city: 地址所在城市 (可选,但建议填写)
- ak: 百度地图API密钥
返回结果:json { "status": 0, "result": { "location": { "lng": 113.331053, // 经度 "lat": 23.137883 // 纬度 } } }
实现方式: 组合调用
地址1 → Geocoding API → 坐标1 ┐
├→ Direction API → 距离
地址2 → Geocoding API → 坐标2 ┘
import { baiduRouteDriving } from '@/api/map'
// 北京到上海的距离
baiduRouteDriving('40.056878,116.30815', '31.222965,121.505821')
.then(response => {
const result = JSON.parse(response.data)
if (result.status === 0) {
const distanceKm = (result.result.routes[0].distance / 1000).toFixed(2)
console.log('驾车距离:', distanceKm + ' 公里')
}
})
import { baiduGeocoding, baiduRouteDriving } from '@/api/map'
async function calculateAddressDistance(fromAddr, toAddr) {
// Step 1: 起点地址转坐标
const fromRes = await baiduGeocoding(fromAddr, '广州市')
const fromGeo = JSON.parse(fromRes.data)
const fromCoord = fromGeo.result.location.lat + ',' + fromGeo.result.location.lng
// Step 2: 终点地址转坐标
const toRes = await baiduGeocoding(toAddr, '广州市')
const toGeo = JSON.parse(toRes.data)
const toCoord = toGeo.result.location.lat + ',' + toGeo.result.location.lng
// Step 3: 计算距离
const routeRes = await baiduRouteDriving(fromCoord, toCoord)
const route = JSON.parse(routeRes.data)
const distanceKm = (route.result.routes[0].distance / 1000).toFixed(2)
return distanceKm
}
// 使用
calculateAddressDistance('广州市天河区天河路', '广州市越秀区中山路')
.then(distance => console.log('距离:', distance + ' 公里'))
// 在任务表单组件中
export default {
data() {
return {
taskForm: {
startAddress: '',
endAddress: '',
distance: null
}
}
},
watch: {
'taskForm.startAddress': 'autoCalcDistance',
'taskForm.endAddress': 'autoCalcDistance'
},
methods: {
async autoCalcDistance() {
if (!this.taskForm.startAddress || !this.taskForm.endAddress) {
return
}
try {
uni.showLoading({ title: '计算距离中...' })
// 调用地址计算距离
const distance = await this.calculateDistance(
this.taskForm.startAddress,
this.taskForm.endAddress
)
// 自动填充距离字段
this.taskForm.distance = distance
uni.hideLoading()
uni.showToast({
title: '距离: ' + distance + ' 公里',
icon: 'success'
})
} catch (error) {
uni.hideLoading()
console.error('距离计算失败:', error)
}
},
async calculateDistance(fromAddr, toAddr) {
// 实现同示例2
// ...
}
}
}
| 文件 | 说明 |
|---|---|
ruoyi-common/src/main/java/com/ruoyi/common/config/BaiduMapConfig.java |
百度地图配置类 |
prd/百度地图距离计算接口说明.md |
详细接口文档 |
prd/百度地图距离计算-快速开始.md |
快速开始指南 |
prd/百度地图距离计算接口实现总结.md |
本文档 |
| 文件 | 修改内容 |
|---|---|
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/VehicleGpsController.java |
新增3个百度地图接口 |
app/api/map.js |
新增3个API调用方法 |
ruoyi-admin/src/main/resources/application.yml |
新增百度地图AK配置 |
@Anonymous() 注解控制访问权限建议对相同地址的地理编码结果进行缓存:
// 使用 Redis 缓存地理编码结果
@Cacheable(value = "geocoding", key = "#address + '_' + #city")
public String getGeocodingResult(String address, String city) {
// 调用百度地图API
}
如需计算多个距离,建议使用批量接口减少请求次数。
对于非实时需求,可以使用异步方式计算距离:
@Async
public CompletableFuture<Double> calculateDistanceAsync(String origin, String dest) {
// 异步计算距离
}
⚠️ 如果您的坐标来自其他地图服务,必须进行坐标转换!
百度地图要求格式: 纬度,经度 (lat,lng)
示例: 40.056878,116.30815
status: 4| status | 说明 | 处理建议 |
|---|---|---|
| 0 | 成功 | 正常处理 |
| 1 | 服务器内部错误 | 重试 |
| 2 | 参数非法 | 检查参数格式 |
| 3 | 权限校验失败 | 检查 AK 配置 |
| 4 | 配额不足 | 升级套餐或等待配额恢复 |
| 5 | AK 不存在 | 检查 AK 是否正确 |
// 步行
@GetMapping("/baidu/route/walking")
// 骑行
@GetMapping("/baidu/route/riding")
// 公交
@GetMapping("/baidu/route/transit")
// 返回途经点、路段详情等
public AjaxResult getRouteDetail(String origin, String destination) {
// 返回完整路径信息
}
// BD-09 → GCJ-02
@GetMapping("/coordinate/bd09ToGcj02")
// GCJ-02 → BD-09
@GetMapping("/coordinate/gcj02ToBd09")
// 地址输入提示
@GetMapping("/baidu/place/suggestion")
public AjaxResult placeSuggestion(String query, String region) {
// 调用百度地图POI搜索API
}
本次实现成功为急救转运系统添加了百度地图距离计算功能,主要完成了:
✅ 后端:
- 创建百度地图配置类 BaiduMapConfig
- 在 VehicleGpsController 中新增 3 个接口
- 配置文件中添加百度地图 AK
✅ 前端:
- 在 map.js 中封装 3 个 API 调用方法
- 完整的参数验证和错误处理
✅ 文档:
- 详细的接口说明文档
- 快速开始指南
- 实现总结文档
现在您可以在任务创建、距离统计等场景中使用这些接口,实现自动计算距离的功能! 🎊
实现完成时间: 2025-10-21
文档版本: v1.0
开发者: AI Assistant