图标定位修改

This commit is contained in:
cwchen 2026-01-14 16:13:23 +08:00
parent 0ca54d558d
commit 8e34a4a5a7
1 changed files with 89 additions and 30 deletions

View File

@ -418,6 +418,9 @@
headingHistorySize: 5, // 历史记录大小5个样本的移动平均 headingHistorySize: 5, // 历史记录大小5个样本的移动平均
lastOrientationUpdateTime: 0, // 上次方向更新时间,用于节流 lastOrientationUpdateTime: 0, // 上次方向更新时间,用于节流
headingOffset: 0, // 方向偏移量用于校正方向偏差如果需要反转方向可以设置为180 headingOffset: 0, // 方向偏移量用于校正方向偏差如果需要反转方向可以设置为180
lastLocationTime: 0, // 上次位置更新时间
lastLocationPoint: null, // 上次位置点,用于计算移动速度
isMoving: false, // 是否在移动状态
elements: { elements: {
panel: null, panel: null,
tree: null, tree: null,
@ -717,7 +720,28 @@
} }
if (shouldUpdate) { if (shouldUpdate) {
console.log('精确位置更新 - 经度:', point.lng, '纬度:', point.lat, '精度:', accuracy, '米'); // 检测移动状态(通过位置变化速度)
const now = Date.now();
if (this.lastLocationTime > 0 && this.lastLocationPoint && this.state.currentLocation) {
const distance = calculateDistance(
this.state.currentLocation.lng,
this.state.currentLocation.lat,
point.lng,
point.lat
);
const timeDiff = (now - this.lastLocationTime) / 1000; // 秒
if (timeDiff > 0) {
const speed = distance / timeDiff; // 米/秒
// 如果速度超过0.3米/秒约1公里/小时),认为在移动
this.isMoving = speed > 0.3;
}
}
this.lastLocationTime = now;
this.lastLocationPoint = { lng: point.lng, lat: point.lat };
console.log('精确位置更新 - 经度:', point.lng, '纬度:', point.lat, '精度:', accuracy, '米',
this.isMoving ? '(移动中)' : '(静止)');
this.state.currentLocation = { this.state.currentLocation = {
lng: point.lng, lng: point.lng,
lat: point.lat, lat: point.lat,
@ -778,9 +802,24 @@
oldLocation.lng, oldLocation.lat, point.lng, point.lat oldLocation.lng, oldLocation.lat, point.lng, point.lat
) : 0; ) : 0;
// 检测移动状态(通过位置变化速度)
const now = Date.now();
if (this.lastLocationTime > 0 && this.lastLocationPoint) {
const timeDiff = (now - this.lastLocationTime) / 1000; // 秒
if (timeDiff > 0) {
const speed = distance / timeDiff; // 米/秒
// 如果速度超过0.3米/秒约1公里/小时),认为在移动
this.isMoving = speed > 0.3;
}
}
this.lastLocationTime = now;
this.lastLocationPoint = { lng: point.lng, lat: point.lat };
console.log('位置更新 - 经度:', point.lng, '纬度:', point.lat, console.log('位置更新 - 经度:', point.lng, '纬度:', point.lat,
'精度:', accuracy, '米', '精度:', accuracy, '米',
oldLocation ? `移动距离: ${distance.toFixed(2)}米` : ''); oldLocation ? `移动距离: ${distance.toFixed(2)}米` : '',
this.isMoving ? '(移动中)' : '(静止)');
this.state.currentLocation = { this.state.currentLocation = {
lng: point.lng, lng: point.lng,
@ -1353,7 +1392,9 @@
} }
// 实时更新方向使用角度差值计算考虑0-360度边界 // 实时更新方向使用角度差值计算考虑0-360度边界
const headingChanged = !this.lastHeading || Math.abs(this.angleDifference(this.state.currentHeading, this.lastHeading)) >= 1; // 移动时提高阈值2度减少抖动静止时降低阈值1度保持响应性
const headingThreshold = this.isMoving ? 2 : 1;
const headingChanged = !this.lastHeading || Math.abs(this.angleDifference(this.state.currentHeading, this.lastHeading)) >= headingThreshold;
if (headingChanged) { if (headingChanged) {
const oldHeading = this.lastHeading; const oldHeading = this.lastHeading;
@ -1558,14 +1599,22 @@
/** /**
* 移动平均滤波(减少抖动) * 移动平均滤波(减少抖动)
* 移动时使用更强的平滑,静止时使用较弱的平滑
*/ */
smoothHeading(rawHeading) { smoothHeading(rawHeading) {
// 初始化历史记录数组和大小(如果不存在) // 初始化历史记录数组和大小(如果不存在)
if (!this.headingHistory) { if (!this.headingHistory) {
this.headingHistory = []; this.headingHistory = [];
} }
// 根据移动状态动态调整历史记录大小
// 移动时使用更多样本10个进行更强的平滑静止时使用较少样本5个保持响应性
const targetHistorySize = this.isMoving ? 10 : 5;
if (!this.headingHistorySize) { if (!this.headingHistorySize) {
this.headingHistorySize = 5; // 默认5个样本的移动平均 this.headingHistorySize = targetHistorySize;
} else {
// 动态调整历史记录大小
this.headingHistorySize = targetHistorySize;
} }
// 添加到历史记录 // 添加到历史记录
@ -1575,7 +1624,8 @@
} }
// 如果历史记录不足,直接返回原始值 // 如果历史记录不足,直接返回原始值
if (this.headingHistory.length < 3) { const minHistorySize = this.isMoving ? 5 : 3; // 移动时需要更多样本
if (this.headingHistory.length < minHistorySize) {
return rawHeading; return rawHeading;
} }
@ -1738,8 +1788,13 @@
const oldHeading = this.state.currentHeading; const oldHeading = this.state.currentHeading;
const diff = Math.abs(this.angleDifference(newHeading, oldHeading)); const diff = Math.abs(this.angleDifference(newHeading, oldHeading));
if (diff >= 1 || oldHeading === 0) { // 移动时提高阈值2度减少抖动静止时降低阈值1度保持响应性
const threshold = this.isMoving ? 2 : 1;
if (diff >= threshold || oldHeading === 0) {
this.state.currentHeading = newHeading; this.state.currentHeading = newHeading;
console.log('方向更新:', newHeading.toFixed(1), '度', '变化:', diff.toFixed(1), '度', this.isMoving ? '(移动中)' : '(静止)');
if (this.state.currentLocation) { if (this.state.currentLocation) {
requestAnimationFrame(() => { requestAnimationFrame(() => {
this.updateLocationMarker(); this.updateLocationMarker();
@ -1911,6 +1966,10 @@
if (this.headingHistory) { if (this.headingHistory) {
this.headingHistory = []; this.headingHistory = [];
} }
// 重置移动状态相关变量
this.isMoving = false;
this.lastLocationTime = 0;
this.lastLocationPoint = null;
this.lastOrientationUpdateTime = 0; this.lastOrientationUpdateTime = 0;
// 移除定位标记 // 移除定位标记