最新整改后:
uni.getLocaiton,uni.startUpdateLocationBackground在打包输出h5时会强制消耗高德的坐标转换
因此换成h5原生方法
1 getLocation
navigator.geolocation.getCurrentPosition(
position => {
let originLat = position.coords.latitude
let originLong = position.coords.longitude
// 获取经纬度
let [latitude, longitude] = qqMapTransGpsMap(originLat, originLong).map(v => v.toFixed(6))
runRes = {
...position.coords,
longitude
}
},
error => {
console.log('getLocation error', error)
// openSettingLocation()
},
{
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 1500 // 缓存定位结果不要设置0
}
)
2 startUpdateLocationBackground
// 成功回调
position => {
// console.log('位置监听成功')
// 标记首次启动成功
if (!this.locationFirstSuccess) {
this.locationFirstSuccess = true
this.locationRetryCount = 0 // 重置重试计数
// console.log('位置监听首次启动成功')
}
let originLat = position.coords.latitude
let originLong = position.coords.longitude
// 获取经纬度
// qqMapTransGpsMap 是自定义方法转换原生wgs48为高德坐标的 网上有开源的不转换则偏移几百米转换则可能几米到十米内
let [latitude, longitude] = qqMapTransGpsMap(originLat, originLong).map(v => v.toFixed(6))
runRes = {
...position.coords,
latitude,
longitude
}
// this.$toast(position.coords.accuracy + '/' + position.coords.speed)
},
// 错误回调
error => {
// console.error('位置监听错误:', error.message, '错误码:', error.code)
// 根据错误类型处理
let errorMessage = ''
// console.log(error,error.code)
// switch (error.code) {
// case error.PERMISSION_DENIED:
// errorMessage = '位置权限被拒绝'
// break
// case error.POSITION_UNAVAILABLE:
// errorMessage = '位置信息不可用'
// break
// case error.TIMEOUT:
// errorMessage = '位置获取超时'
// break
// default:
// errorMessage = '位置监听错误'
// }
// 处理重试逻辑
this.handleLocationError(errorMessage)
},
// 配置选项(可选)
{
enableHighAccuracy: true, // 是否高精度
timeout: 5000, // 超时时间(毫秒)
maximumAge: 1500 // 缓存位置的最大年龄(毫秒)
}
)
3 关闭
if (this.watchId) {
navigator.geolocation.clearWatch(this.watchId)
this.watchId = null
}