地理编码服务可以将地址字符串转换为GPS坐标(经纬度),用于自动计算医院之间的转运距离。
GET /system/geocode/address
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| address | String | 是 | 地址字符串,例如:"北京市朝阳区某某医院" |
| city | String | 否 | 城市名称,用于提高解析准确度,例如:"北京市" |
成功响应:json { "code": 200, "msg": "地理编码成功", "data": { "latitude": 39.908722, "longitude": 116.397496, "address": "北京市东城区东长安街1号", "success": true, "errorMessage": null } }
失败响应:json { "code": 500, "msg": "地理编码失败: 地址不存在" }
import { getCoordinatesByAddress } from '@/api/geocode'
// 基本用法
async getHospitalCoordinates() {
try {
const res = await getCoordinatesByAddress('北京协和医院')
if (res.code === 200) {
const { latitude, longitude } = res.data
console.log('GPS坐标:', latitude, longitude)
// 保存坐标到表单
this.taskForm.hospitalOut.latitude = latitude
this.taskForm.hospitalOut.longitude = longitude
}
} catch (error) {
console.error('获取坐标失败', error)
}
}
// 指定城市,提高准确度
async getHospitalCoordinates() {
try {
const res = await getCoordinatesByAddress('协和医院', '北京市')
if (res.code === 200) {
const { latitude, longitude, address } = res.data
console.log('GPS坐标:', latitude, longitude)
console.log('标准化地址:', address)
}
} catch (error) {
console.error('获取坐标失败', error)
}
}
在 create-emergency.vue 中,当用户选择医院后自动获取坐标:
// 选择转出医院
selectHospitalOut(hospital) {
this.taskForm.hospitalOut.name = hospital.hospName
this.taskForm.hospitalOut.address = hospital.hospAddress
this.hospitalOutSearchKeyword = hospital.hospName
this.showHospitalOutResults = false
this.hospitalOutResults = []
// 自动获取GPS坐标
this.getHospitalOutCoordinates(hospital.hospAddress, hospital.hopsCity)
},
// 获取转出医院坐标
async getHospitalOutCoordinates(address, city) {
try {
const res = await getCoordinatesByAddress(address, city)
if (res.code === 200) {
this.taskForm.hospitalOut.latitude = res.data.latitude
this.taskForm.hospitalOut.longitude = res.data.longitude
// 如果转入医院也已选择,自动计算距离
if (this.taskForm.hospitalIn.latitude) {
this.calculateDistance()
}
}
} catch (error) {
console.error('获取转出医院坐标失败', error)
}
},
// 获取转入医院坐标
async getHospitalInCoordinates(address, city) {
try {
const res = await getCoordinatesByAddress(address, city)
if (res.code === 200) {
this.taskForm.hospitalIn.latitude = res.data.latitude
this.taskForm.hospitalIn.longitude = res.data.longitude
// 如果转出医院也已选择,自动计算距离
if (this.taskForm.hospitalOut.latitude) {
this.calculateDistance()
}
}
} catch (error) {
console.error('获取转入医院坐标失败', error)
}
},
// 计算两点之间的距离(使用Haversine公式)
calculateDistance() {
const lat1 = this.taskForm.hospitalOut.latitude
const lon1 = this.taskForm.hospitalOut.longitude
const lat2 = this.taskForm.hospitalIn.latitude
const lon2 = this.taskForm.hospitalIn.longitude
if (!lat1 || !lon1 || !lat2 || !lon2) {
return
}
const R = 6371 // 地球半径(公里)
const dLat = (lat2 - lat1) * Math.PI / 180
const dLon = (lon2 - lon1) * Math.PI / 180
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
Math.sin(dLon / 2) * Math.sin(dLon / 2)
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
const distance = R * c
// 自动填充距离字段(保留2位小数)
this.taskForm.transferDistance = distance.toFixed(2)
uni.showToast({
title: `预估距离: ${distance.toFixed(2)}公里`,
icon: 'none'
})
}
在 application.yml 中配置腾讯地图API密钥:
# 腾讯地图配置
tencent:
map:
key: YOUR_TENCENT_MAP_API_KEY
使用腾讯地图WebService API的地理编码接口:
- API文档:https://lbs.qq.com/service/webService/webServiceGuide/webServiceGcoder
- 免费额配额:10000次/天/Key
- 请求限流:5次/秒/Key