定位信息修改
This commit is contained in:
parent
a9c2b1dae0
commit
82b6327b17
|
|
@ -532,22 +532,19 @@
|
|||
* 初始化定位跟踪
|
||||
*/
|
||||
initLocationTracking() {
|
||||
// 检查浏览器是否支持定位
|
||||
if (!navigator.geolocation) {
|
||||
console.warn('浏览器不支持地理定位');
|
||||
return;
|
||||
}
|
||||
|
||||
// 等待地图和Convertor库加载完成
|
||||
if (!this.map) {
|
||||
// 等待地图加载完成
|
||||
if (!this.map || !this.state.mapLoaded) {
|
||||
console.log('等待地图加载完成...');
|
||||
setTimeout(() => this.initLocationTracking(), 200);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('地图已加载完成,开始初始化定位功能');
|
||||
|
||||
// 创建定位标记图标(带方向的箭头)
|
||||
this.createLocationIcon();
|
||||
|
||||
// 开始监听位置变化
|
||||
// 开始监听位置变化(异步获取位置,不会立即有位置信息)
|
||||
this.startWatchingPosition();
|
||||
|
||||
// 开始监听设备方向变化
|
||||
|
|
@ -595,19 +592,12 @@
|
|||
* 开始监听位置变化
|
||||
*/
|
||||
startWatchingPosition() {
|
||||
// 优先使用百度地图SDK定位(直接返回BD09坐标,最准确)
|
||||
// 只使用百度地图SDK定位
|
||||
if (typeof BMapGL !== 'undefined' && BMapGL.Geolocation) {
|
||||
console.log('使用百度地图SDK辅助定位API(优先)');
|
||||
console.log('使用百度地图SDK辅助定位API');
|
||||
this.startBaiduGeolocation();
|
||||
} else if (typeof uni !== 'undefined' && uni.getLocation) {
|
||||
console.log('使用uni-app GPS定位API(WGS84)');
|
||||
this.startUniAppGPSGeolocation();
|
||||
} else if (typeof plus !== 'undefined' && plus.geolocation) {
|
||||
console.log('使用5+定位API');
|
||||
this.startPlusGeolocation();
|
||||
} else {
|
||||
console.log('使用浏览器定位API');
|
||||
this.startBrowserGeolocation();
|
||||
console.error('百度地图定位API不可用,请检查BMapGL.Geolocation是否已加载');
|
||||
}
|
||||
},
|
||||
|
||||
|
|
@ -655,13 +645,6 @@
|
|||
return;
|
||||
}
|
||||
|
||||
// 如果精度仍然很差(大于100米),切换到GPS定位
|
||||
if (accuracy > 100 && typeof uni !== 'undefined' && uni.getLocation) {
|
||||
console.warn('百度定位精度较差(' + accuracy + '米),切换到GPS定位');
|
||||
this.startUniAppGPSGeolocation();
|
||||
return;
|
||||
}
|
||||
|
||||
this.state.currentLocation = {
|
||||
lng: point.lng,
|
||||
lat: point.lat,
|
||||
|
|
@ -674,9 +657,12 @@
|
|||
this.updateLocationMarker();
|
||||
} else {
|
||||
console.error('百度定位返回无效坐标:', point);
|
||||
// 降级到uni-app定位
|
||||
if (typeof uni !== 'undefined' && uni.getLocation) {
|
||||
this.startUniAppGPSGeolocation();
|
||||
// 如果失败且还有重试次数,继续重试
|
||||
if (retryCount < maxRetries) {
|
||||
retryCount++;
|
||||
console.log(`坐标无效,重试 (${retryCount}/${maxRetries})`);
|
||||
setTimeout(tryGetPosition, 2000);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
@ -688,13 +674,7 @@
|
|||
return;
|
||||
}
|
||||
|
||||
console.warn('百度地图定位失败,状态:', status, '尝试备用方案');
|
||||
// 降级到uni-app定位
|
||||
if (typeof uni !== 'undefined' && uni.getLocation) {
|
||||
this.startUniAppGPSGeolocation();
|
||||
} else {
|
||||
this.startBrowserGeolocation();
|
||||
}
|
||||
console.error('百度地图定位失败,状态:', status, '已达到最大重试次数');
|
||||
}
|
||||
}, {
|
||||
enableHighAccuracy: true,
|
||||
|
|
@ -716,14 +696,6 @@
|
|||
if (point.lng && point.lat && !isNaN(point.lng) && !isNaN(point.lat) &&
|
||||
Math.abs(point.lng) <= 180 && Math.abs(point.lat) <= 90) {
|
||||
|
||||
// 如果精度太差(大于100米),切换到GPS定位
|
||||
if (accuracy > 100 && typeof uni !== 'undefined' && uni.getLocation) {
|
||||
console.warn('百度定位精度持续较差(' + accuracy + '米),切换到GPS定位');
|
||||
clearInterval(this.state.watchLocationTimer);
|
||||
this.startUniAppGPSGeolocation();
|
||||
return;
|
||||
}
|
||||
|
||||
// 只更新精度更好的位置(精度提升超过10米,或者精度小于50米)
|
||||
if (!this.state.currentLocation ||
|
||||
(accuracy < this.state.currentLocation.accuracy - 10) ||
|
||||
|
|
@ -1247,14 +1219,15 @@
|
|||
* 更新定位标记
|
||||
*/
|
||||
updateLocationMarker() {
|
||||
// 确保地图已加载完成且有位置信息
|
||||
// 确保地图已加载完成
|
||||
if (!this.map || !this.state.mapLoaded) {
|
||||
console.warn('无法更新定位标记:地图未加载完成');
|
||||
// 静默返回,不输出警告(地图可能还在加载中)
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果没有位置信息,静默返回(定位是异步的,需要时间获取)
|
||||
if (!this.state.currentLocation) {
|
||||
console.warn('无法更新定位标记:位置信息缺失');
|
||||
// 不输出警告,因为定位是异步过程,这是正常情况
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -1297,41 +1270,9 @@
|
|||
return;
|
||||
}
|
||||
|
||||
// 如果没有当前位置,尝试获取一次
|
||||
if (navigator.geolocation) {
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
(position) => {
|
||||
const { latitude, longitude } = position.coords;
|
||||
|
||||
// 将WGS84坐标转换为百度BD09坐标
|
||||
this.wgs84ToBd09(longitude, latitude, (bd09) => {
|
||||
const point = new BMapGL.Point(bd09.lng, bd09.lat);
|
||||
this.map.centerAndZoom(point, 18);
|
||||
|
||||
// 更新当前位置(使用转换后的坐标)
|
||||
this.state.currentLocation = {
|
||||
lat: bd09.lat,
|
||||
lng: bd09.lng,
|
||||
accuracy: position.coords.accuracy || 0
|
||||
};
|
||||
|
||||
// 更新标记
|
||||
this.updateLocationMarker();
|
||||
});
|
||||
},
|
||||
(error) => {
|
||||
console.warn('获取当前位置失败:', error);
|
||||
alert('无法获取当前位置,请检查定位权限设置');
|
||||
},
|
||||
{
|
||||
enableHighAccuracy: true,
|
||||
timeout: 10000,
|
||||
maximumAge: 0
|
||||
}
|
||||
);
|
||||
} else {
|
||||
alert('浏览器不支持定位功能');
|
||||
}
|
||||
// 如果没有当前位置,提示用户等待定位
|
||||
console.warn('当前位置尚未获取,请等待定位完成');
|
||||
alert('正在获取当前位置,请稍候...');
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
@ -1460,7 +1401,10 @@
|
|||
if (Math.abs(newHeading - this.state.currentHeading) > 1) {
|
||||
this.state.currentHeading = newHeading;
|
||||
console.log('方向更新(来自Compass):', newHeading);
|
||||
this.updateLocationMarker();
|
||||
// 只有在有位置信息时才更新标记
|
||||
if (this.state.currentLocation) {
|
||||
this.updateLocationMarker();
|
||||
}
|
||||
}
|
||||
},
|
||||
(error) => console.error('获取方向失败:', error),
|
||||
|
|
@ -1493,7 +1437,10 @@
|
|||
if (Math.abs(newHeading - oldHeading) > 1 || oldHeading === 0) {
|
||||
this.state.currentHeading = newHeading;
|
||||
console.log('方向更新:', newHeading, '度');
|
||||
this.updateLocationMarker();
|
||||
// 只有在有位置信息时才更新标记
|
||||
if (this.state.currentLocation) {
|
||||
this.updateLocationMarker();
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in New Issue