From 87323e6cc4e0db42009aebdadcf112a07b3606a4 Mon Sep 17 00:00:00 2001 From: duanxiaohai <1812246227@qq.com> Date: Wed, 7 Aug 2024 11:19:31 +0800 Subject: [PATCH] gx --- src/view/sy_mapcopy.vue | 132 +++++++++++++++++++++++++++++----------- 1 file changed, 97 insertions(+), 35 deletions(-) diff --git a/src/view/sy_mapcopy.vue b/src/view/sy_mapcopy.vue index 13b89ce..efcb5c3 100644 --- a/src/view/sy_mapcopy.vue +++ b/src/view/sy_mapcopy.vue @@ -610,9 +610,10 @@ const tsbqTotal = ref([ ]); const data = reactive({ serviceCircleList: [], - fwqCoordinates: "", //服务圈中心坐标 - fwqCoordinates1: [], //服务圈中心坐标 - fwqCoordinates2: [], //服务圈中心坐标 + fwqCoordinates: [], //服务圈经度 + fwqCoordinates1: [], //服务圈经度 + fwqCoordinates2: [], //服务圈纬度 + zoomLevel: null, //服务圈缩放级别 }); //基本公共服务圈 const jbfwqTotal = ref([ @@ -1942,16 +1943,16 @@ const person_detail = (age) => { const getFwq = () => { http.get(`/api/ggfwyth/ysyzt/listXzjdAndCsq`).then((res) => { if (res.code == 200) { - res.data.forEach((item,index) => { + res.data.forEach((item, index) => { jbfwqTotal.value.push({ - name:item.xzjd, - child:[], - }) + name: item.xzjd, + child: [], + }); item.csqs.forEach((item2) => { jbfwqTotal.value[index].child.push({ - name:item2.csq, - jc:item2.jc - }) + name: item2.csq, + jc: item2.jc, + }); }); }); } @@ -1983,7 +1984,7 @@ const changeFwq = async (id, name, names) => { map.clearOverlays(); //清空地图上所有的覆盖物 addggfwq(); //创建公共服务圈 setTimeout(() => { - goMapCenter([data.fwqCoordinates1, data.fwqCoordinates2], 16); //中心点缩放 + goMapCenter([data.fwqCoordinates1, data.fwqCoordinates2], data.zoomLevel); //中心点缩放 }, 500); } else { ElMessage.warning("无数据"); @@ -2003,48 +2004,109 @@ const getServiceCircle = async (communityName, townName) => { console.error("Error fetching data:", error); } }; +// 计算中心点 +const calculateCenterPoint = (coordinates) => { + if (coordinates.length === 0) { + return null; + } + + let x = 0.0; + let y = 0.0; + let z = 0.0; + + coordinates.forEach((coord) => { + const lat = (parseFloat(coord[1]) * Math.PI) / 180; + const lon = (parseFloat(coord[0]) * Math.PI) / 180; + + x += Math.cos(lat) * Math.cos(lon); + y += Math.cos(lat) * Math.sin(lon); + z += Math.sin(lat); + }); + + const total = coordinates.length; + x /= total; + y /= total; + z /= total; + + const centralLon = Math.atan2(y, x); + const centralSquareRoot = Math.sqrt(x * x + y * y); + const centralLat = Math.atan2(z, centralSquareRoot); + + return [ + ((centralLon * 180) / Math.PI).toFixed(6), // 经度 + ((centralLat * 180) / Math.PI).toFixed(6), // 纬度 + ]; +}; +const calculateAverage = (coordinates) => { + // const numericCoordinates = coordinates.map(Number); // 将字符串转换为数字 + const numericCoordinates = coordinates.map((coord) => { + // 使用正则表达式去除非数字字符(保留小数点和负号) + const cleanedCoord = coord.replace(/[^0-9.-]/g, ""); + return Number(cleanedCoord); // 将清理后的字符串转换为数字 + }); + const sum = numericCoordinates.reduce((acc, val) => acc + val, 0); // 计算总和 + const average = sum / numericCoordinates.length; // 计算平均值 + // console.log(average, "计算平均值"); + + return parseFloat(average.toFixed(5)); // 保留5位小数并转换为数字 +}; +// 根据最大距离计算合适的缩放级别 +const calculateZoomLevel = (maxDistance) => { + // 示例实现,您可以根据需要调整计算逻辑 + if (maxDistance < 151) return 18; + if (maxDistance < 400) return 17; + if (maxDistance < 600) return 17.5; + if (maxDistance < 1000) return 16; + if (maxDistance < 2000) return 15; + if (maxDistance < 3000) return 14; + if (maxDistance < 5000) return 13; + return 13; +}; //创建公共服务圈 const addggfwq = () => { if (fwqList.value.length !== 0) { - data.fwqCoordinates1 = []; - data.fwqCoordinates2 = []; + data.fwqCoordinates = []; + // data.fwqCoordinates1 = []; + // data.fwqCoordinates2 = []; fwqList.value.map((item, index) => { - data.fwqCoordinates1.push(item.point[0]); - data.fwqCoordinates2.push(item.point[1]); + data.fwqCoordinates.push(item.point); + // data.fwqCoordinates1.push(item.point[0]); + // data.fwqCoordinates2.push(item.point[1]); createCustomOverlay(item, index); }); - // console.log("经纬度", data.fwqCoordinates1, data.fwqCoordinates2); - function calculateAverage(coordinates) { - // const numericCoordinates = coordinates.map(Number); // 将字符串转换为数字 - const numericCoordinates = coordinates.map((coord) => { - // 使用正则表达式去除非数字字符(保留小数点和负号) - const cleanedCoord = coord.replace(/[^0-9.-]/g, ""); - return Number(cleanedCoord); // 将清理后的字符串转换为数字 - }); - const sum = numericCoordinates.reduce((acc, val) => acc + val, 0); // 计算总和 - const average = sum / numericCoordinates.length; // 计算平均值 - return parseFloat(average.toFixed(5)); // 保留5位小数并转换为数字 - } - data.fwqCoordinates1 = calculateAverage(data.fwqCoordinates1); - data.fwqCoordinates2 = calculateAverage(data.fwqCoordinates2); + const centerPoint = calculateCenterPoint(data.fwqCoordinates); + + data.fwqCoordinates1 = centerPoint[0]; + data.fwqCoordinates2 = centerPoint[1]; + // data.fwqCoordinates1 = calculateAverage(data.fwqCoordinates1); + // data.fwqCoordinates2 = calculateAverage(data.fwqCoordinates2); + // console.log(centerPoint); // 输出中心点的经纬度 + // console.log("经纬度", data.fwqCoordinates1, data.fwqCoordinates2 ); let distances = []; // 初始化距离数组 fwqList.value.forEach((item) => { + // let distance = algorithm( + // (item.point[0] = Number(item.point[0].replace(/[^0-9.-]/g, ""))), + // (item.point[1] = Number(item.point[1].replace(/[^0-9.-]/g, ""))), + // data.fwqCoordinates1, + // data.fwqCoordinates2 + // ); let distance = algorithm( - (item.point[0] = Number(item.point[0].replace(/[^0-9.-]/g, ""))), - (item.point[1] = Number(item.point[1].replace(/[^0-9.-]/g, ""))), - data.fwqCoordinates1, - data.fwqCoordinates2 + Number(item.point[0].replace(/[^0-9.-]/g, "")), + Number(item.point[1].replace(/[^0-9.-]/g, "")), + Number(centerPoint[0]), + Number(centerPoint[1]) ); distances.push(distance); // 将距离存入数组 }); let maxDistance = Math.max(...distances); // 找到最大距离 - maxDistance = Math.ceil(maxDistance * 1.8); // 向上取整 + maxDistance = Math.ceil(maxDistance * 2.2); // 向上取整 if (maxDistance < 100) { maxDistance = 150; // 如果小于 100,则赋值为 150 } - console.log("最大距离:", maxDistance); + data.zoomLevel = calculateZoomLevel(maxDistance); + // console.log("最大距离:", maxDistance,data.zoomLevel); drawACircle(maxDistance); } else { ElMessage.warning("无数据");