JavaScript: 查找经纬度数组中相邻节点
// 使用穷举法,即遍历所有的经纬度数组,对比每一个点与其他点的距离,如果距离小于某个设定的值,则将该点标记为相邻节点。
// 下面是一个实现的例子:
// 定义相邻距离 const MAX_DISTANCE = 0.5;
// 经纬度数组 const lonLatArray = [ [39.9072, 116.3974], [39.9072, 116.41], [39.9072, 116.4], [39.9072, 116.4], [39.9072, 116.4], [39.9072, 116.4], [39.9072, 116.4], [39.9072, 116.4], [39.9072, 116.4], [39.9072, 116.4] ];
// 对比相邻距离 const adjacentNodes = []; for (let i = 0; i < lonLatArray.length; i++) { for (let j = i + 1; j < lonLatArray.length; j++) { const dis = getDistance(lonLatArray[i], lonLatArray[j]); if (dis < MAX_DISTANCE) { adjacentNodes.push([i, j]); } } }
// 计算距离 function getDistance(p1, p2) { const radLat1 = (Math.PI * p1[0]) / 180.0; const radLat2 = (Math.PI * p2[0]) / 180.0; const a = radLat1 - radLat2; const b = (Math.PI * p1[1]) / 180.0 - (Math.PI * p2[1]) / 180.0; let dis = 2 * Math.asin( Math.sqrt( Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2) ) ); return dis; }
原文地址: https://www.cveoy.top/t/topic/lfRs 著作权归作者所有。请勿转载和采集!