hookehuyr

fix 判断当前经纬度是否在目标经纬度的范围内函数修改

1 /* 1 /*
2 * @Date: 2024-07-31 17:19:25 2 * @Date: 2024-07-31 17:19:25
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2024-12-20 16:09:42 4 + * @LastEditTime: 2024-12-25 15:09:33
5 * @FilePath: /data-table/src/utils/tools.js 5 * @FilePath: /data-table/src/utils/tools.js
6 * @Description: 文件描述 6 * @Description: 文件描述
7 */ 7 */
...@@ -197,31 +197,44 @@ const isInViewport = (el) => { // 判断元素是否在可视区域 ...@@ -197,31 +197,44 @@ const isInViewport = (el) => { // 判断元素是否在可视区域
197 * @param {number} radius - 半径(单位:米) 197 * @param {number} radius - 半径(单位:米)
198 * @returns {boolean} 是否在范围内 198 * @returns {boolean} 是否在范围内
199 */ 199 */
200 +// const isWithinRadius = (currentLat, currentLng, targetLat, targetLng, radius) => {
201 +// const EARTH_RADIUS = 6371000; // 地球半径(单位:米)
202 +
203 +// // 将角度转换为弧度
204 +// const toRadians = (degrees) => (degrees * Math.PI) / 180;
205 +
206 +// // 当前点和目标点的纬度差和经度差
207 +// const deltaLat = toRadians(targetLat - currentLat);
208 +// const deltaLng = toRadians(targetLng - currentLng);
209 +
210 +// // 转换为弧度
211 +// const radCurrentLat = toRadians(currentLat);
212 +// const radTargetLat = toRadians(targetLat);
213 +
214 +// // Haversine 公式计算两点间的距离
215 +// const a =
216 +// Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) +
217 +// Math.cos(radCurrentLat) *
218 +// Math.cos(radTargetLat) *
219 +// Math.sin(deltaLng / 2) *
220 +// Math.sin(deltaLng / 2);
221 +// const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
222 +// const distance = EARTH_RADIUS * c;
223 +
224 +// // 判断距离是否在范围内
225 +// return distance <= radius;
226 +// }
227 +
200 const isWithinRadius = (currentLat, currentLng, targetLat, targetLng, radius) => { 228 const isWithinRadius = (currentLat, currentLng, targetLat, targetLng, radius) => {
201 - const EARTH_RADIUS = 6371000; // 地球半径(单位:米) 229 + const earthRadius = 6371; // 地球平均半径,单位为千米
202 - 230 + const toRadians = (angle) => angle * (Math.PI / 180);
203 - // 将角度转换为弧度 231 + let dLat = toRadians(targetLat - currentLat);
204 - const toRadians = (degrees) => (degrees * Math.PI) / 180; 232 + let dLng = toRadians(targetLng - currentLng);
205 - 233 + let a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
206 - // 当前点和目标点的纬度差和经度差 234 + Math.cos(toRadians(currentLat)) * Math.cos(toRadians(targetLat)) *
207 - const deltaLat = toRadians(targetLat - currentLat); 235 + Math.sin(dLng / 2) * Math.sin(dLng / 2);
208 - const deltaLng = toRadians(targetLng - currentLng); 236 + let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
209 - 237 + let distance = earthRadius * c;
210 - // 转换为弧度
211 - const radCurrentLat = toRadians(currentLat);
212 - const radTargetLat = toRadians(targetLat);
213 -
214 - // Haversine 公式计算两点间的距离
215 - const a =
216 - Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) +
217 - Math.cos(radCurrentLat) *
218 - Math.cos(radTargetLat) *
219 - Math.sin(deltaLng / 2) *
220 - Math.sin(deltaLng / 2);
221 - const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
222 - const distance = EARTH_RADIUS * c;
223 -
224 - // 判断距离是否在范围内
225 return distance <= radius; 238 return distance <= radius;
226 } 239 }
227 240
......