地图优化

This commit is contained in:
cwchen 2026-01-14 13:42:03 +08:00
parent 82b6327b17
commit ba2e9d1235
1 changed files with 61 additions and 49 deletions

View File

@ -618,73 +618,85 @@
console.log('已开启百度地图SDK辅助定位'); console.log('已开启百度地图SDK辅助定位');
// 获取当前位置增加超时时间等待GPS定位 // 先快速获取位置(使用较短的超时时间,优先显示位置)
let retryCount = 0; let isFirstLocation = true;
const maxRetries = 3;
const tryGetPosition = () => { const getQuickPosition = () => {
geolocation.getCurrentPosition((result) => { geolocation.getCurrentPosition((result) => {
const status = geolocation.getStatus(); const status = geolocation.getStatus();
console.log('百度地图定位状态:', status, '结果:', result, '重试次数:', retryCount); console.log('百度地图定位状态:', status, '结果:', result);
// 百度地图定位成功状态码是0 // 百度地图定位成功状态码是0
if (status === 0 && result && result.point) { if (status === 0 && result && result.point) {
const point = result.point; const point = result.point;
const accuracy = result.accuracy || 999; // 如果没有精度信息,假设精度较差 const accuracy = result.accuracy || 999;
console.log('百度地图SDK定位成功 - 经度:', point.lng, '纬度:', point.lat, '精度:', accuracy, '米');
// 验证坐标有效性 // 验证坐标有效性
if (point.lng && point.lat && !isNaN(point.lng) && !isNaN(point.lat) && if (point.lng && point.lat && !isNaN(point.lng) && !isNaN(point.lat) &&
Math.abs(point.lng) <= 180 && Math.abs(point.lat) <= 90) { Math.abs(point.lng) <= 180 && Math.abs(point.lat) <= 90) {
// 如果精度太差大于50米且还有重试次数继续重试 // 如果是第一次获取位置,立即显示(不管精度如何)
if (accuracy > 50 && retryCount < maxRetries) { if (isFirstLocation) {
retryCount++; console.log('首次定位成功 - 经度:', point.lng, '纬度:', point.lat, '精度:', accuracy, '米');
console.log(`精度较差(${accuracy}米),重试获取更精确位置 (${retryCount}/${maxRetries})`); this.state.currentLocation = {
setTimeout(tryGetPosition, 2000); lng: point.lng,
return; lat: point.lat,
} accuracy: accuracy
};
this.state.currentLocation = { this.updateLocationMarker();
lng: point.lng, isFirstLocation = false;
lat: point.lat,
accuracy: accuracy // 如果精度较差,继续获取更精确的位置(后台优化)
}; if (accuracy > 50) {
console.log('精度较差,后台继续优化定位...');
// 使用逆地理编码验证位置(可选,用于调试) setTimeout(() => getPrecisePosition(), 1000);
this.verifyLocationWithGeocoder(point.lng, point.lat); }
this.updateLocationMarker();
} else {
console.error('百度定位返回无效坐标:', point);
// 如果失败且还有重试次数,继续重试
if (retryCount < maxRetries) {
retryCount++;
console.log(`坐标无效,重试 (${retryCount}/${maxRetries})`);
setTimeout(tryGetPosition, 2000);
return;
} }
} }
} else { } else {
// 如果失败且还有重试次数,继续重试 console.warn('快速定位失败,状态:', status);
if (retryCount < maxRetries) {
retryCount++;
console.log(`定位失败,重试 (${retryCount}/${maxRetries})`);
setTimeout(tryGetPosition, 2000);
return;
}
console.error('百度地图定位失败,状态:', status, '已达到最大重试次数');
} }
}, { }, {
enableHighAccuracy: true, enableHighAccuracy: false, // 先使用快速定位(网络定位)
timeout: 30000 // 30秒超时等待GPS定位 timeout: 5000 // 5秒超时快速获取位置
}); });
}; };
tryGetPosition(); // 获取精确位置GPS定位
const getPrecisePosition = () => {
geolocation.getCurrentPosition((result) => {
const status = geolocation.getStatus();
if (status === 0 && result && result.point) {
const point = result.point;
const accuracy = result.accuracy || 999;
// 验证坐标有效性
if (point.lng && point.lat && !isNaN(point.lng) && !isNaN(point.lat) &&
Math.abs(point.lng) <= 180 && Math.abs(point.lat) <= 90) {
// 只更新精度更好的位置
if (!this.state.currentLocation || accuracy < this.state.currentLocation.accuracy) {
console.log('精确位置更新 - 经度:', point.lng, '纬度:', point.lat, '精度:', accuracy, '米');
this.state.currentLocation = {
lng: point.lng,
lat: point.lat,
accuracy: accuracy
};
this.updateLocationMarker();
}
}
}
}, {
enableHighAccuracy: true, // 使用GPS精确定位
timeout: 10000 // 10秒超时
});
};
// 先快速获取位置
getQuickPosition();
// 持续监听位置变化(使用定时器定期获取) // 持续监听位置变化(使用定时器定期获取,优化精度
this.state.watchLocationTimer = setInterval(() => { this.state.watchLocationTimer = setInterval(() => {
geolocation.getCurrentPosition((result) => { geolocation.getCurrentPosition((result) => {
const status = geolocation.getStatus(); const status = geolocation.getStatus();
@ -713,10 +725,10 @@
} }
} }
}, { }, {
enableHighAccuracy: true, enableHighAccuracy: true, // 使用GPS精确定位
timeout: 30000 timeout: 10000 // 10秒超时
}); });
}, 5000); // 每5秒更新一次给GPS更多时间稳定 }, 5000); // 每5秒更新一次优化精度
// 保存geolocation实例以便后续使用 // 保存geolocation实例以便后续使用
this.baiduGeolocation = geolocation; this.baiduGeolocation = geolocation;