hookehuyr

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

/*
* @Date: 2024-07-31 17:19:25
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2024-12-20 16:09:42
* @LastEditTime: 2024-12-25 15:09:33
* @FilePath: /data-table/src/utils/tools.js
* @Description: 文件描述
*/
......@@ -197,31 +197,44 @@ const isInViewport = (el) => { // 判断元素是否在可视区域
* @param {number} radius - 半径(单位:米)
* @returns {boolean} 是否在范围内
*/
// const isWithinRadius = (currentLat, currentLng, targetLat, targetLng, radius) => {
// const EARTH_RADIUS = 6371000; // 地球半径(单位:米)
// // 将角度转换为弧度
// const toRadians = (degrees) => (degrees * Math.PI) / 180;
// // 当前点和目标点的纬度差和经度差
// const deltaLat = toRadians(targetLat - currentLat);
// const deltaLng = toRadians(targetLng - currentLng);
// // 转换为弧度
// const radCurrentLat = toRadians(currentLat);
// const radTargetLat = toRadians(targetLat);
// // Haversine 公式计算两点间的距离
// const a =
// Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) +
// Math.cos(radCurrentLat) *
// Math.cos(radTargetLat) *
// Math.sin(deltaLng / 2) *
// Math.sin(deltaLng / 2);
// const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
// const distance = EARTH_RADIUS * c;
// // 判断距离是否在范围内
// return distance <= radius;
// }
const isWithinRadius = (currentLat, currentLng, targetLat, targetLng, radius) => {
const EARTH_RADIUS = 6371000; // 地球半径(单位:米)
// 将角度转换为弧度
const toRadians = (degrees) => (degrees * Math.PI) / 180;
// 当前点和目标点的纬度差和经度差
const deltaLat = toRadians(targetLat - currentLat);
const deltaLng = toRadians(targetLng - currentLng);
// 转换为弧度
const radCurrentLat = toRadians(currentLat);
const radTargetLat = toRadians(targetLat);
// Haversine 公式计算两点间的距离
const a =
Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) +
Math.cos(radCurrentLat) *
Math.cos(radTargetLat) *
Math.sin(deltaLng / 2) *
Math.sin(deltaLng / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
const distance = EARTH_RADIUS * c;
// 判断距离是否在范围内
const earthRadius = 6371; // 地球平均半径,单位为千米
const toRadians = (angle) => angle * (Math.PI / 180);
let dLat = toRadians(targetLat - currentLat);
let dLng = toRadians(targetLng - currentLng);
let a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(toRadians(currentLat)) * Math.cos(toRadians(targetLat)) *
Math.sin(dLng / 2) * Math.sin(dLng / 2);
let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
let distance = earthRadius * c;
return distance <= radius;
}
......