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