From f73966d6be7d8e51bd86bb84257b80827b20267c Mon Sep 17 00:00:00 2001 From: cwchen <1048842385@qq.com> Date: Mon, 12 Jan 2026 10:13:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=B0=E5=9B=BE=E9=A1=B5=E9=9D=A2=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E7=A3=81=E5=81=8F=E8=A7=92=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useGeomagnetism.js | 58 +++++++++++++++++++++++++++---- src/pages/projectSelect/index.vue | 11 +++++- src/static/map.html | 3 +- 3 files changed, 64 insertions(+), 8 deletions(-) diff --git a/src/hooks/useGeomagnetism.js b/src/hooks/useGeomagnetism.js index 7abe834..52e1988 100644 --- a/src/hooks/useGeomagnetism.js +++ b/src/hooks/useGeomagnetism.js @@ -13,6 +13,48 @@ export function useGeomagnetism() { }; }; + /** + * 将度数转换为度分格式 + * @param {number} degrees - 度数(如 -5.333) + * @returns {string} 度分格式字符串(如 "-5° 20'") + */ + const formatDegreesToDMS = (degrees) => { + if (typeof degrees !== 'number' || isNaN(degrees)) { + return '0° 0\''; + } + + const absDegrees = Math.abs(degrees); + const sign = degrees < 0 ? '-' : ''; + + // 获取度数部分(整数) + const deg = Math.floor(absDegrees); + + // 获取分数部分(小数部分 * 60,四舍五入到整数) + const minutes = Math.round((absDegrees - deg) * 60); + + return `${sign}${deg}° ${minutes}'`; + }; + + /** + * 将度数转换为东西方向格式 + * @param {number} degrees - 度数(如 -5.333) + * @param {number} decimals - 保留小数位数,默认2位 + * @returns {string} 格式字符串(如 "5.33° W" 或 "3.45° E") + */ + const formatDegreesToDirection = (degrees, decimals = 2) => { + if (typeof degrees !== 'number' || isNaN(degrees)) { + return '0.00°'; + } + + const absDegrees = Math.abs(degrees); + const direction = degrees < 0 ? 'W' : degrees > 0 ? 'E' : ''; + + // 保留指定小数位数 + const formattedDegrees = absDegrees.toFixed(decimals); + + return direction ? `${formattedDegrees}° ${direction}` : `${formattedDegrees}°`; + }; + const calculate = (latitude, longitude, altitude = 0) => { try { // 强制转换 @@ -21,7 +63,7 @@ export function useGeomagnetism() { if (isNaN(numLat) || isNaN(numLng)) { console.warn('磁偏角计算: 无效的经纬度', { latitude, longitude }); - return { declination: 0, success: false }; + return { declination: 0, declinationFormatted: '0.00°', success: false }; } const wgs = bd09toWgs84(numLat, numLng); @@ -31,7 +73,7 @@ export function useGeomagnetism() { if (!model || typeof model.point !== 'function') { console.error("无法加载地磁模型或模型没有point方法"); - return { declination: 0, success: false }; + return { declination: 0, declinationFormatted: '0.00°', success: false }; } // point() 方法接受数组参数 [lat, lon, altitude] @@ -40,7 +82,7 @@ export function useGeomagnetism() { if (!info) { console.error('model.point() 返回 null 或 undefined'); - return { declination: 0, success: false }; + return { declination: 0, declinationFormatted: '0.00°', success: false }; } // 根据 geomagnetism 库的类型定义,返回值属性名是 decl(不是 declination) @@ -48,19 +90,23 @@ export function useGeomagnetism() { if (typeof decl !== 'number' || isNaN(decl)) { console.warn('磁偏角值无效:', decl, 'info对象:', info); - return { declination: 0, success: false }; + return { declination: 0, declinationFormatted: '0.00°', success: false }; } + // 转换为东西方向格式(如 "5.33° W") + const declinationFormatted = formatDegreesToDirection(decl); + return { declination: decl, + declinationFormatted: declinationFormatted, success: true }; } catch (e) { console.error('磁偏角计算过程出错:', e); console.error('错误堆栈:', e.stack); - return { declination: 0, success: false }; + return { declination: 0, declinationFormatted: '0.00°', success: false }; } }; - return { calculate }; + return { calculate, formatDegreesToDMS, formatDegreesToDirection }; } \ No newline at end of file diff --git a/src/pages/projectSelect/index.vue b/src/pages/projectSelect/index.vue index c4f6734..f5b1ee0 100644 --- a/src/pages/projectSelect/index.vue +++ b/src/pages/projectSelect/index.vue @@ -10,6 +10,7 @@ import { ref, onMounted } from 'vue' import { useMemberStore } from '@/stores/modules/member' import { onLoad } from '@dcloudio/uni-app' import { getAllProjectListApi, getProjectModelListApi } from '@/services/projectSelect' +import { useGeomagnetism } from '@/hooks/useGeomagnetism'; const baiduMap = ref(null) const projectList = ref([]) @@ -40,14 +41,22 @@ const handleWebViewMessage = (event) => { }) } +// 计算磁偏角 +const { calculate } = useGeomagnetism(); + + onLoad(() => { getProjectList().then(() => { if (projectList.value.length > 0) { const projectInfo = projectList.value.map((item) => { + const res = calculate(item.latitude, item.longitude); + // 使用格式化后的度分格式,如果没有则使用原始数值 + const declVal = res?.declinationFormatted || (res?.declination !== undefined ? `${res.declination.toFixed(2)}°` : '0° 0\''); return { lat: item.latitude, + declination: declVal, lng: item.longitude, - proName: item.proName, + proName: item.proName, chargePerson: item.chargePerson, location: item.location, proId: item.proId, diff --git a/src/static/map.html b/src/static/map.html index dbafe8e..8d5459d 100644 --- a/src/static/map.html +++ b/src/static/map.html @@ -56,6 +56,7 @@ } function handleProjectInfoOnMap(map, projectInfo) { + // 示例1: 如果项目信息包含经纬度,移动地图到该位置 if (projectInfo.lng && projectInfo.lat) { @@ -79,7 +80,7 @@ //

所在地: ${projectInfo.location}

// `); - const label = new BMapGL.Label(projectInfo.proName, { + const label = new BMapGL.Label(`${projectInfo.proName} (${projectInfo.declination})`, { position: projectPoint, offset: new BMapGL.Size(0, 0), // 调整偏移量,使文字在marker正下方 })