设备端百度定位 SDK 以 bd09ll 上报、后端原样存储,原先按 WGS84 转换会二次偏移。utils/coord.ts 新增 bd09ToGcj02,首页与地图详情改用它;AGENTS.md 同步坐标约定。
139 lines
3.6 KiB
TypeScript
139 lines
3.6 KiB
TypeScript
import type { DeviceLocation } from '../../utils/api'
|
|
import { getDeviceLocation } from '../../utils/api'
|
|
import { getDeviceSn } from '../../utils/token'
|
|
import { bd09ToGcj02 } from '../../utils/coord'
|
|
|
|
interface MapMarker {
|
|
id: number
|
|
latitude: number
|
|
longitude: number
|
|
iconPath: string
|
|
width: number
|
|
height: number
|
|
}
|
|
|
|
/** 后端时间为 ISO 字符串(如 2026-11-26T20:31:27),展示到分钟 */
|
|
function formatTime(value?: string): string {
|
|
if (!value) return ''
|
|
return value.replace('T', ' ').slice(0, 16)
|
|
}
|
|
|
|
/** 设备端原始错误文案(如「未知定位错误: 162」)不直接展示 */
|
|
function friendlyError(mapError?: string): string {
|
|
if (!mapError || mapError === '-') return '设备暂未上报有效定位'
|
|
if (mapError.includes('162')) return '定位失败,请检查设备定位权限与网络'
|
|
return '定位暂不可用'
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
loading: true,
|
|
located: false,
|
|
latitude: 0,
|
|
longitude: 0,
|
|
address: '',
|
|
detail: '',
|
|
deviceSn: '',
|
|
lastTime: '',
|
|
markers: [] as MapMarker[],
|
|
reason: ''
|
|
},
|
|
|
|
onLoad() {
|
|
this.loadLocation()
|
|
},
|
|
|
|
async loadLocation() {
|
|
this.setData({ loading: true })
|
|
|
|
const sn = getDeviceSn()
|
|
if (!sn) {
|
|
this.setData({ loading: false, reason: '请先在首页选择设备' })
|
|
return
|
|
}
|
|
|
|
try {
|
|
const location: DeviceLocation = await getDeviceLocation(sn)
|
|
const latitude = Number(location.latitude)
|
|
const longitude = Number(location.longitude)
|
|
const valid =
|
|
Number.isFinite(latitude) &&
|
|
Number.isFinite(longitude) &&
|
|
(latitude !== 0 || longitude !== 0)
|
|
|
|
if (!valid) {
|
|
this.setData({ loading: false, located: false, reason: friendlyError(location.mapError) })
|
|
return
|
|
}
|
|
|
|
// 设备端百度定位上报 BD-09,微信 map/openLocation 只认 GCJ-02
|
|
const gcj = bd09ToGcj02(longitude, latitude)
|
|
const detail = [
|
|
location.province,
|
|
location.city,
|
|
location.district,
|
|
location.street,
|
|
location.streetNumber
|
|
]
|
|
.filter((part): part is string => !!part)
|
|
.join('')
|
|
|
|
this.setData({
|
|
loading: false,
|
|
located: true,
|
|
latitude: gcj.latitude,
|
|
longitude: gcj.longitude,
|
|
address: location.address || location.locationDescribe || '未知位置',
|
|
detail,
|
|
deviceSn: sn,
|
|
lastTime: formatTime(location.lastSuccessfulTime),
|
|
markers: [
|
|
{
|
|
id: 1,
|
|
latitude: gcj.latitude,
|
|
longitude: gcj.longitude,
|
|
iconPath: '/assets/marker.png',
|
|
width: 32,
|
|
height: 32
|
|
}
|
|
]
|
|
})
|
|
} catch (err) {
|
|
this.setData({
|
|
loading: false,
|
|
located: false,
|
|
reason: (err as Error).message || '定位加载失败,请稍后重试'
|
|
})
|
|
}
|
|
},
|
|
|
|
onRefresh() {
|
|
this.loadLocation()
|
|
},
|
|
|
|
/** 微信内置地图支持路线规划与导航,入参必须是 GCJ-02 */
|
|
onOpenNavigation() {
|
|
if (!this.data.located) return
|
|
wx.openLocation({
|
|
latitude: this.data.latitude,
|
|
longitude: this.data.longitude,
|
|
name: this.data.address,
|
|
address: this.data.detail || this.data.address,
|
|
scale: 18
|
|
})
|
|
},
|
|
|
|
onCopyAddress() {
|
|
const text = this.data.address || this.data.detail
|
|
if (!text) return
|
|
wx.setClipboardData({
|
|
data: text,
|
|
success: () => wx.showToast({ title: '地址已复制', icon: 'none' })
|
|
})
|
|
},
|
|
|
|
onCalloutTap() {
|
|
wx.showToast({ title: this.data.address || '设备位置', icon: 'none' })
|
|
}
|
|
})
|