From 2c2bffed3d9b8afdd1e1eecdd6d1600d9ee99b5d Mon Sep 17 00:00:00 2001 From: suixy <2277317060@qq.com> Date: Sat, 11 Oct 2025 15:25:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/index.vue | 66 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 8 deletions(-) diff --git a/src/views/index.vue b/src/views/index.vue index c3ff360..0344da4 100644 --- a/src/views/index.vue +++ b/src/views/index.vue @@ -762,6 +762,55 @@ const setItemsId = (e) => { const locationData = ref({}) const currentPosition = ref({}) +function calcRectangleFromPoints(points) { + if (!points || points.length !== 4) throw new Error("必须传入四个点"); + + const R = 111320; + const lat0 = points.reduce((a, b) => a + b.lat, 0) / 4; + const cosLat = Math.cos((lat0 * Math.PI) / 180); + + // 经纬度转平面坐标 + const toXY = p => ({ + name: p.name, + x: p.lon * R * cosLat, + y: p.lat * R + }); + const [p1, p2, p3, p4] = points.map(toXY); + + // 距离函数 + const dist = (a, b) => Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2); + + // 各边长度 + const top = dist(p2, p3); + const bottom = dist(p1, p4); + const left = dist(p4, p3); + const right = dist(p1, p2); + + // 判断梯形:上下边差异超过5% + const trapezoid = Math.abs(top - bottom) / Math.max(top, bottom) > 0.05; + + let width, height; + + if (trapezoid) { + // 使用平均宽高 + width = (top + bottom) / 2; + height = (left + right) / 2; + } else { + width = bottom; + height = right; + } + + // 计算旋转角度(p1→p2) + const angle = Math.atan2(p2.y - p1.y, p2.x - p1.x) * 180 / Math.PI; + + return { + width: width.toFixed(2), + height: height.toFixed(2), + angle: angle.toFixed(2) + }; +} + + function getXYDistanceFromMin(current, minPoint) { const R = 6371000; // 地球半径(米) @@ -1229,15 +1278,16 @@ const saveAreaPoint = () => { }) Promise.all(arr).then(() => { ElMessage.success('保存成功') - let data = getBoundingBoxAndSize(areaPoints.value) + // let data = getBoundingBoxAndSize(areaPoints.value) + let data = calcRectangleFromPoints(areaPoints.value) console.log(data) - areaData.value = data - area = { - "LatMin": data.minLat, - "LatMax": data.maxLat, - "LonMin": data.minLon, - "LonMax": data.maxLon - } + // areaData.value = data + // area = { + // "LatMin": data.minLat, + // "LatMax": data.maxLat, + // "LonMin": data.minLon, + // "LonMax": data.maxLon + // } }).catch((err) => { ElMessage.error(err) })